blob: ee561954cdef30a970b898c798f94ecfb7721b61 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
daniel@transgaming.com86bdb822012-01-20 18:24:39 +000025#include "libGLESv2/Query.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000026#include "libGLESv2/RenderBuffer.h"
27#include "libGLESv2/Shader.h"
28#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000029#include "libGLESv2/VertexDataManager.h"
30#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
daniel@transgaming.com86487c22010-03-11 19:41:43 +000032#undef near
33#undef far
34
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035namespace gl
36{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000037Context::Context(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000039 ASSERT(robustAccess == false); // Unimplemented
40
daniel@transgaming.comc941e252011-10-26 02:32:31 +000041 mDisplay = NULL;
42 mDevice = NULL;
43
benvanik@google.com1a233342011-04-28 19:44:39 +000044 mFenceHandleAllocator.setBaseHandle(0);
45
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000047
daniel@transgaming.com428d1582010-05-04 03:35:25 +000048 mState.depthClearValue = 1.0f;
49 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050
daniel@transgaming.com428d1582010-05-04 03:35:25 +000051 mState.cullFace = false;
52 mState.cullMode = GL_BACK;
53 mState.frontFace = GL_CCW;
54 mState.depthTest = false;
55 mState.depthFunc = GL_LESS;
56 mState.blend = false;
57 mState.sourceBlendRGB = GL_ONE;
58 mState.sourceBlendAlpha = GL_ONE;
59 mState.destBlendRGB = GL_ZERO;
60 mState.destBlendAlpha = GL_ZERO;
61 mState.blendEquationRGB = GL_FUNC_ADD;
62 mState.blendEquationAlpha = GL_FUNC_ADD;
63 mState.blendColor.red = 0;
64 mState.blendColor.green = 0;
65 mState.blendColor.blue = 0;
66 mState.blendColor.alpha = 0;
67 mState.stencilTest = false;
68 mState.stencilFunc = GL_ALWAYS;
69 mState.stencilRef = 0;
70 mState.stencilMask = -1;
71 mState.stencilWritemask = -1;
72 mState.stencilBackFunc = GL_ALWAYS;
73 mState.stencilBackRef = 0;
74 mState.stencilBackMask = - 1;
75 mState.stencilBackWritemask = -1;
76 mState.stencilFail = GL_KEEP;
77 mState.stencilPassDepthFail = GL_KEEP;
78 mState.stencilPassDepthPass = GL_KEEP;
79 mState.stencilBackFail = GL_KEEP;
80 mState.stencilBackPassDepthFail = GL_KEEP;
81 mState.stencilBackPassDepthPass = GL_KEEP;
82 mState.polygonOffsetFill = false;
83 mState.polygonOffsetFactor = 0.0f;
84 mState.polygonOffsetUnits = 0.0f;
85 mState.sampleAlphaToCoverage = false;
86 mState.sampleCoverage = false;
87 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000088 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.scissorTest = false;
90 mState.dither = true;
91 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000092 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093
daniel@transgaming.com428d1582010-05-04 03:35:25 +000094 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.viewportX = 0;
97 mState.viewportY = 0;
98 mState.viewportWidth = config->mDisplayMode.Width;
99 mState.viewportHeight = config->mDisplayMode.Height;
100 mState.zNear = 0.0f;
101 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000103 mState.scissorX = 0;
104 mState.scissorY = 0;
105 mState.scissorWidth = config->mDisplayMode.Width;
106 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000108 mState.colorMaskRed = true;
109 mState.colorMaskGreen = true;
110 mState.colorMaskBlue = true;
111 mState.colorMaskAlpha = true;
112 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000114 if (shareContext != NULL)
115 {
116 mResourceManager = shareContext->mResourceManager;
117 mResourceManager->addRef();
118 }
119 else
120 {
121 mResourceManager = new ResourceManager();
122 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000123
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124 // [OpenGL ES 2.0.24] section 3.7 page 83:
125 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
126 // and cube map texture state vectors respectively associated with them.
127 // In order that access to these initial textures not be lost, they are treated as texture
128 // objects all of whose names are 0.
129
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000130 mTexture2DZero.set(new Texture2D(0));
131 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000133 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000134 bindArrayBuffer(0);
135 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 bindTextureCubeMap(0);
137 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000138 bindReadFramebuffer(0);
139 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 bindRenderbuffer(0);
141
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000142 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000144 mState.packAlignment = 4;
145 mState.unpackAlignment = 4;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000146 mState.packReverseRowOrder = false;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000147
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000148 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000149 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000150 mBlit = NULL;
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +0000151 mLineLoopIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153 mInvalidEnum = false;
154 mInvalidValue = false;
155 mInvalidOperation = false;
156 mOutOfMemory = false;
157 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000158
159 mHasBeenCurrent = false;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000160 mContextLost = false;
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000161 mResetStatus = GL_NO_ERROR;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000162 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
163 mRobustAccess = robustAccess;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000164
gman@chromium.org50c526d2011-08-10 05:19:44 +0000165 mSupportsDXT1Textures = false;
166 mSupportsDXT3Textures = false;
167 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000168 mSupportsEventQueries = false;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000169 mSupportsOcclusionQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000170 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000171 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000172 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000173 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174}
175
176Context::~Context()
177{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000178 if (mState.currentProgram != 0)
179 {
180 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
181 if (programObject)
182 {
183 programObject->release();
184 }
185 mState.currentProgram = 0;
186 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000188 while (!mFramebufferMap.empty())
189 {
190 deleteFramebuffer(mFramebufferMap.begin()->first);
191 }
192
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000193 while (!mFenceMap.empty())
194 {
195 deleteFence(mFenceMap.begin()->first);
196 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000197
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000198 while (!mQueryMap.empty())
199 {
200 deleteQuery(mQueryMap.begin()->first);
201 }
202
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000203 while (!mMultiSampleSupport.empty())
204 {
205 delete [] mMultiSampleSupport.begin()->second;
206 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
207 }
208
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000209 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000211 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000212 {
213 mState.samplerTexture[type][sampler].set(NULL);
214 }
215 }
216
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000217 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000218 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000219 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000220 }
221
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000222 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
223 {
224 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
225 }
226
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000227 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
228 {
229 mState.activeQuery[i].set(NULL);
230 }
231
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000232 mState.arrayBuffer.set(NULL);
233 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000234 mState.renderbuffer.set(NULL);
235
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000236 mTexture2DZero.set(NULL);
237 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000239 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000240 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000241 delete mBlit;
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +0000242 delete mLineLoopIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000243
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000244 if (mMaskedClearSavedState)
245 {
246 mMaskedClearSavedState->Release();
247 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000248
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000249 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250}
251
252void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
253{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000254 mDisplay = display;
255 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000257 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000258 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000259 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000260
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000261 mVertexDataManager = new VertexDataManager(this, mDevice);
262 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000263 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000264
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +0000265 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000266 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
267 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +0000268 mSupportsInstancing = mDisplay->getInstancingSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000269
270 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
271 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
272 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
273 mMaxRenderbufferDimension = mMaxTextureDimension;
274 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
275 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
276 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
277
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000278 const D3DFORMAT renderBufferFormats[] =
279 {
280 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000281 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000282 D3DFMT_R5G6B5,
283 D3DFMT_D24S8
284 };
285
286 int max = 0;
287 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
288 {
289 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000290 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000291 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
292
293 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
294 {
295 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
296 {
297 max = j;
298 }
299 }
300 }
301
302 mMaxSupportedSamples = max;
303
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000304 mSupportsEventQueries = mDisplay->getEventQuerySupport();
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000305 mSupportsOcclusionQueries = mDisplay->getOcclusionQuerySupport();
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000306 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
307 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
308 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000309 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
310 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000311 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
312 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000313
daniel@transgaming.com83921382011-01-08 05:46:00 +0000314 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
315
gman@chromium.org50c526d2011-08-10 05:19:44 +0000316 mNumCompressedTextureFormats = 0;
317 if (supportsDXT1Textures())
318 {
319 mNumCompressedTextureFormats += 2;
320 }
321 if (supportsDXT3Textures())
322 {
323 mNumCompressedTextureFormats += 1;
324 }
325 if (supportsDXT5Textures())
326 {
327 mNumCompressedTextureFormats += 1;
328 }
329
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000330 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000331 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000332
333 mState.viewportX = 0;
334 mState.viewportY = 0;
335 mState.viewportWidth = surface->getWidth();
336 mState.viewportHeight = surface->getHeight();
337
338 mState.scissorX = 0;
339 mState.scissorY = 0;
340 mState.scissorWidth = surface->getWidth();
341 mState.scissorHeight = surface->getHeight();
342
343 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000344 }
345
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
347 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000348 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000351 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000352 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353
354 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000356 if (defaultRenderTarget)
357 {
358 defaultRenderTarget->Release();
359 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000361 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000363 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000365
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000366 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367}
368
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000369// 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 +0000370void Context::markAllStateDirty()
371{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000372 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
373 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000374 mAppliedTextureSerialPS[t] = 0;
375 }
376
377 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
378 {
379 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000380 }
381
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000382 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000383 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000384 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000385 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000386 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000387 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000388 mViewportInitialized = false;
389 mRenderTargetDescInitialized = false;
390
391 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000392
393 mClearStateDirty = true;
394 mCullStateDirty = true;
395 mDepthStateDirty = true;
396 mMaskStateDirty = true;
397 mBlendStateDirty = true;
398 mStencilStateDirty = true;
399 mPolygonOffsetStateDirty = true;
400 mScissorStateDirty = true;
401 mSampleStateDirty = true;
402 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000403 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000404 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000405 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000406}
407
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000408void Context::markContextLost()
409{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000410 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
411 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000412 mContextLost = true;
413}
414
415bool Context::isContextLost()
416{
417 return mContextLost;
418}
419
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420void Context::setClearColor(float red, float green, float blue, float alpha)
421{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000422 mState.colorClearValue.red = red;
423 mState.colorClearValue.green = green;
424 mState.colorClearValue.blue = blue;
425 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426}
427
428void Context::setClearDepth(float depth)
429{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000430 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431}
432
433void Context::setClearStencil(int stencil)
434{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435 mState.stencilClearValue = stencil;
436}
437
438void Context::setCullFace(bool enabled)
439{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000440 if (mState.cullFace != enabled)
441 {
442 mState.cullFace = enabled;
443 mCullStateDirty = true;
444 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000445}
446
447bool Context::isCullFaceEnabled() const
448{
449 return mState.cullFace;
450}
451
452void Context::setCullMode(GLenum mode)
453{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000454 if (mState.cullMode != mode)
455 {
456 mState.cullMode = mode;
457 mCullStateDirty = true;
458 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000459}
460
461void Context::setFrontFace(GLenum front)
462{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000463 if (mState.frontFace != front)
464 {
465 mState.frontFace = front;
466 mFrontFaceDirty = true;
467 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000468}
469
470void Context::setDepthTest(bool enabled)
471{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000472 if (mState.depthTest != enabled)
473 {
474 mState.depthTest = enabled;
475 mDepthStateDirty = true;
476 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000477}
478
479bool Context::isDepthTestEnabled() const
480{
481 return mState.depthTest;
482}
483
484void Context::setDepthFunc(GLenum depthFunc)
485{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000486 if (mState.depthFunc != depthFunc)
487 {
488 mState.depthFunc = depthFunc;
489 mDepthStateDirty = true;
490 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000491}
492
493void Context::setDepthRange(float zNear, float zFar)
494{
495 mState.zNear = zNear;
496 mState.zFar = zFar;
497}
498
499void Context::setBlend(bool enabled)
500{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000501 if (mState.blend != enabled)
502 {
503 mState.blend = enabled;
504 mBlendStateDirty = true;
505 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000506}
507
508bool Context::isBlendEnabled() const
509{
510 return mState.blend;
511}
512
513void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
514{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000515 if (mState.sourceBlendRGB != sourceRGB ||
516 mState.sourceBlendAlpha != sourceAlpha ||
517 mState.destBlendRGB != destRGB ||
518 mState.destBlendAlpha != destAlpha)
519 {
520 mState.sourceBlendRGB = sourceRGB;
521 mState.destBlendRGB = destRGB;
522 mState.sourceBlendAlpha = sourceAlpha;
523 mState.destBlendAlpha = destAlpha;
524 mBlendStateDirty = true;
525 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000526}
527
528void Context::setBlendColor(float red, float green, float blue, float alpha)
529{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000530 if (mState.blendColor.red != red ||
531 mState.blendColor.green != green ||
532 mState.blendColor.blue != blue ||
533 mState.blendColor.alpha != alpha)
534 {
535 mState.blendColor.red = red;
536 mState.blendColor.green = green;
537 mState.blendColor.blue = blue;
538 mState.blendColor.alpha = alpha;
539 mBlendStateDirty = true;
540 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000541}
542
543void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
544{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000545 if (mState.blendEquationRGB != rgbEquation ||
546 mState.blendEquationAlpha != alphaEquation)
547 {
548 mState.blendEquationRGB = rgbEquation;
549 mState.blendEquationAlpha = alphaEquation;
550 mBlendStateDirty = true;
551 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000552}
553
554void Context::setStencilTest(bool enabled)
555{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000556 if (mState.stencilTest != enabled)
557 {
558 mState.stencilTest = enabled;
559 mStencilStateDirty = true;
560 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000561}
562
563bool Context::isStencilTestEnabled() const
564{
565 return mState.stencilTest;
566}
567
568void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
569{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000570 if (mState.stencilFunc != stencilFunc ||
571 mState.stencilRef != stencilRef ||
572 mState.stencilMask != stencilMask)
573 {
574 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000575 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576 mState.stencilMask = stencilMask;
577 mStencilStateDirty = true;
578 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000579}
580
581void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
582{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000583 if (mState.stencilBackFunc != stencilBackFunc ||
584 mState.stencilBackRef != stencilBackRef ||
585 mState.stencilBackMask != stencilBackMask)
586 {
587 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000588 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000589 mState.stencilBackMask = stencilBackMask;
590 mStencilStateDirty = true;
591 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000592}
593
594void Context::setStencilWritemask(GLuint stencilWritemask)
595{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000596 if (mState.stencilWritemask != stencilWritemask)
597 {
598 mState.stencilWritemask = stencilWritemask;
599 mStencilStateDirty = true;
600 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000601}
602
603void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
604{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000605 if (mState.stencilBackWritemask != stencilBackWritemask)
606 {
607 mState.stencilBackWritemask = stencilBackWritemask;
608 mStencilStateDirty = true;
609 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000610}
611
612void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
613{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000614 if (mState.stencilFail != stencilFail ||
615 mState.stencilPassDepthFail != stencilPassDepthFail ||
616 mState.stencilPassDepthPass != stencilPassDepthPass)
617 {
618 mState.stencilFail = stencilFail;
619 mState.stencilPassDepthFail = stencilPassDepthFail;
620 mState.stencilPassDepthPass = stencilPassDepthPass;
621 mStencilStateDirty = true;
622 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000623}
624
625void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
626{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000627 if (mState.stencilBackFail != stencilBackFail ||
628 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
629 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
630 {
631 mState.stencilBackFail = stencilBackFail;
632 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
633 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
634 mStencilStateDirty = true;
635 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000636}
637
638void Context::setPolygonOffsetFill(bool enabled)
639{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000640 if (mState.polygonOffsetFill != enabled)
641 {
642 mState.polygonOffsetFill = enabled;
643 mPolygonOffsetStateDirty = true;
644 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000645}
646
647bool Context::isPolygonOffsetFillEnabled() const
648{
649 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000650
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000651}
652
653void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
654{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000655 if (mState.polygonOffsetFactor != factor ||
656 mState.polygonOffsetUnits != units)
657 {
658 mState.polygonOffsetFactor = factor;
659 mState.polygonOffsetUnits = units;
660 mPolygonOffsetStateDirty = true;
661 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000662}
663
664void Context::setSampleAlphaToCoverage(bool enabled)
665{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000666 if (mState.sampleAlphaToCoverage != enabled)
667 {
668 mState.sampleAlphaToCoverage = enabled;
669 mSampleStateDirty = true;
670 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000671}
672
673bool Context::isSampleAlphaToCoverageEnabled() const
674{
675 return mState.sampleAlphaToCoverage;
676}
677
678void Context::setSampleCoverage(bool enabled)
679{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000680 if (mState.sampleCoverage != enabled)
681 {
682 mState.sampleCoverage = enabled;
683 mSampleStateDirty = true;
684 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000685}
686
687bool Context::isSampleCoverageEnabled() const
688{
689 return mState.sampleCoverage;
690}
691
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000692void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000693{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000694 if (mState.sampleCoverageValue != value ||
695 mState.sampleCoverageInvert != invert)
696 {
697 mState.sampleCoverageValue = value;
698 mState.sampleCoverageInvert = invert;
699 mSampleStateDirty = true;
700 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000701}
702
703void Context::setScissorTest(bool enabled)
704{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000705 if (mState.scissorTest != enabled)
706 {
707 mState.scissorTest = enabled;
708 mScissorStateDirty = true;
709 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000710}
711
712bool Context::isScissorTestEnabled() const
713{
714 return mState.scissorTest;
715}
716
717void Context::setDither(bool enabled)
718{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000719 if (mState.dither != enabled)
720 {
721 mState.dither = enabled;
722 mDitherStateDirty = true;
723 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000724}
725
726bool Context::isDitherEnabled() const
727{
728 return mState.dither;
729}
730
731void Context::setLineWidth(GLfloat width)
732{
733 mState.lineWidth = width;
734}
735
736void Context::setGenerateMipmapHint(GLenum hint)
737{
738 mState.generateMipmapHint = hint;
739}
740
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000741void Context::setFragmentShaderDerivativeHint(GLenum hint)
742{
743 mState.fragmentShaderDerivativeHint = hint;
744 // TODO: Propagate the hint to shader translator so we can write
745 // ddx, ddx_coarse, or ddx_fine depending on the hint.
746 // Ignore for now. It is valid for implementations to ignore hint.
747}
748
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
750{
751 mState.viewportX = x;
752 mState.viewportY = y;
753 mState.viewportWidth = width;
754 mState.viewportHeight = height;
755}
756
757void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
758{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000759 if (mState.scissorX != x || mState.scissorY != y ||
760 mState.scissorWidth != width || mState.scissorHeight != height)
761 {
762 mState.scissorX = x;
763 mState.scissorY = y;
764 mState.scissorWidth = width;
765 mState.scissorHeight = height;
766 mScissorStateDirty = true;
767 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000768}
769
770void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
771{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000772 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
773 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
774 {
775 mState.colorMaskRed = red;
776 mState.colorMaskGreen = green;
777 mState.colorMaskBlue = blue;
778 mState.colorMaskAlpha = alpha;
779 mMaskStateDirty = true;
780 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000781}
782
783void Context::setDepthMask(bool mask)
784{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000785 if (mState.depthMask != mask)
786 {
787 mState.depthMask = mask;
788 mMaskStateDirty = true;
789 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000790}
791
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000792void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000793{
794 mState.activeSampler = active;
795}
796
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000797GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000798{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000799 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000800}
801
802GLuint Context::getDrawFramebufferHandle() const
803{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000804 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000805}
806
807GLuint Context::getRenderbufferHandle() const
808{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000809 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000810}
811
812GLuint Context::getArrayBufferHandle() const
813{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000814 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000815}
816
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000817GLuint Context::getActiveQuery(GLenum target) const
818{
819 Query *queryObject = NULL;
820
821 switch (target)
822 {
823 case GL_ANY_SAMPLES_PASSED_EXT:
824 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
825 break;
826 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
827 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
828 break;
829 default:
830 ASSERT(false);
831 }
832
833 if (queryObject)
834 {
835 return queryObject->id();
836 }
837 else
838 {
839 return 0;
840 }
841}
842
daniel@transgaming.com83921382011-01-08 05:46:00 +0000843void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000844{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000845 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000846}
847
daniel@transgaming.com83921382011-01-08 05:46:00 +0000848const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000849{
850 return mState.vertexAttribute[attribNum];
851}
852
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000853void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000854 GLsizei stride, const void *pointer)
855{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000856 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000857 mState.vertexAttribute[attribNum].mSize = size;
858 mState.vertexAttribute[attribNum].mType = type;
859 mState.vertexAttribute[attribNum].mNormalized = normalized;
860 mState.vertexAttribute[attribNum].mStride = stride;
861 mState.vertexAttribute[attribNum].mPointer = pointer;
862}
863
864const void *Context::getVertexAttribPointer(unsigned int attribNum) const
865{
866 return mState.vertexAttribute[attribNum].mPointer;
867}
868
daniel@transgaming.com83921382011-01-08 05:46:00 +0000869const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000870{
871 return mState.vertexAttribute;
872}
873
874void Context::setPackAlignment(GLint alignment)
875{
876 mState.packAlignment = alignment;
877}
878
879GLint Context::getPackAlignment() const
880{
881 return mState.packAlignment;
882}
883
884void Context::setUnpackAlignment(GLint alignment)
885{
886 mState.unpackAlignment = alignment;
887}
888
889GLint Context::getUnpackAlignment() const
890{
891 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892}
893
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000894void Context::setPackReverseRowOrder(bool reverseRowOrder)
895{
896 mState.packReverseRowOrder = reverseRowOrder;
897}
898
899bool Context::getPackReverseRowOrder() const
900{
901 return mState.packReverseRowOrder;
902}
903
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904GLuint Context::createBuffer()
905{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000906 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907}
908
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909GLuint Context::createProgram()
910{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000911 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912}
913
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000914GLuint Context::createShader(GLenum type)
915{
916 return mResourceManager->createShader(type);
917}
918
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919GLuint Context::createTexture()
920{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000921 return mResourceManager->createTexture();
922}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000924GLuint Context::createRenderbuffer()
925{
926 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927}
928
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000929// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930GLuint Context::createFramebuffer()
931{
benvanik@google.com1a233342011-04-28 19:44:39 +0000932 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000933
934 mFramebufferMap[handle] = NULL;
935
936 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937}
938
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000939GLuint Context::createFence()
940{
benvanik@google.com1a233342011-04-28 19:44:39 +0000941 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000942
943 mFenceMap[handle] = new Fence;
944
945 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000946}
947
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000948// Returns an unused query name
949GLuint Context::createQuery()
950{
951 GLuint handle = mQueryHandleAllocator.allocate();
952
953 mQueryMap[handle] = NULL;
954
955 return handle;
956}
957
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958void Context::deleteBuffer(GLuint buffer)
959{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000960 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961 {
962 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000964
965 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
968void Context::deleteShader(GLuint shader)
969{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000970 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971}
972
973void Context::deleteProgram(GLuint program)
974{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000975 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000976 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
979void Context::deleteTexture(GLuint texture)
980{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000981 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982 {
983 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000985
986 mResourceManager->deleteTexture(texture);
987}
988
989void Context::deleteRenderbuffer(GLuint renderbuffer)
990{
991 if (mResourceManager->getRenderbuffer(renderbuffer))
992 {
993 detachRenderbuffer(renderbuffer);
994 }
995
996 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
999void Context::deleteFramebuffer(GLuint framebuffer)
1000{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001001 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
1002
1003 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004 {
1005 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +00001006
benvanik@google.com1a233342011-04-28 19:44:39 +00001007 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001008 delete framebufferObject->second;
1009 mFramebufferMap.erase(framebufferObject);
1010 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011}
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001012
1013void Context::deleteFence(GLuint fence)
1014{
1015 FenceMap::iterator fenceObject = mFenceMap.find(fence);
1016
1017 if (fenceObject != mFenceMap.end())
1018 {
benvanik@google.com1a233342011-04-28 19:44:39 +00001019 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001020 delete fenceObject->second;
1021 mFenceMap.erase(fenceObject);
1022 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001023}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001025void Context::deleteQuery(GLuint query)
1026{
1027 QueryMap::iterator queryObject = mQueryMap.find(query);
1028 if (queryObject != mQueryMap.end())
1029 {
1030 mQueryHandleAllocator.release(queryObject->first);
1031 if (queryObject->second)
1032 {
1033 queryObject->second->release();
1034 }
1035 mQueryMap.erase(queryObject);
1036 }
1037}
1038
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001039Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001041 return mResourceManager->getBuffer(handle);
1042}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001044Shader *Context::getShader(GLuint handle)
1045{
1046 return mResourceManager->getShader(handle);
1047}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001049Program *Context::getProgram(GLuint handle)
1050{
1051 return mResourceManager->getProgram(handle);
1052}
1053
1054Texture *Context::getTexture(GLuint handle)
1055{
1056 return mResourceManager->getTexture(handle);
1057}
1058
1059Renderbuffer *Context::getRenderbuffer(GLuint handle)
1060{
1061 return mResourceManager->getRenderbuffer(handle);
1062}
1063
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001064Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001065{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001066 return getFramebuffer(mState.readFramebuffer);
1067}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001068
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001069Framebuffer *Context::getDrawFramebuffer()
1070{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001071 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072}
1073
1074void Context::bindArrayBuffer(unsigned int buffer)
1075{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001076 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001078 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079}
1080
1081void Context::bindElementArrayBuffer(unsigned int buffer)
1082{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001083 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001085 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
1088void Context::bindTexture2D(GLuint texture)
1089{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001090 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001092 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093}
1094
1095void Context::bindTextureCubeMap(GLuint texture)
1096{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001097 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001099 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100}
1101
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001102void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001104 if (!getFramebuffer(framebuffer))
1105 {
1106 mFramebufferMap[framebuffer] = new Framebuffer();
1107 }
1108
1109 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001110}
1111
1112void Context::bindDrawFramebuffer(GLuint framebuffer)
1113{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001114 if (!getFramebuffer(framebuffer))
1115 {
1116 mFramebufferMap[framebuffer] = new Framebuffer();
1117 }
1118
1119 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001120
1121 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122}
1123
1124void Context::bindRenderbuffer(GLuint renderbuffer)
1125{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001126 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1127
1128 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129}
1130
1131void Context::useProgram(GLuint program)
1132{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001133 GLuint priorProgram = mState.currentProgram;
1134 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001135
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001136 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001137 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001138 Program *newProgram = mResourceManager->getProgram(program);
1139 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001140 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001141 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001142
1143 if (newProgram)
1144 {
1145 newProgram->addRef();
1146 }
1147
1148 if (oldProgram)
1149 {
1150 oldProgram->release();
1151 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153}
1154
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001155void Context::beginQuery(GLenum target, GLuint query)
1156{
1157 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1158 // of zero, if the active query object name for <target> is non-zero (for the
1159 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1160 // the active query for either target is non-zero), if <id> is the name of an
1161 // existing query object whose type does not match <target>, or if <id> is the
1162 // active query object name for any query type, the error INVALID_OPERATION is
1163 // generated.
1164
1165 // Ensure no other queries are active
1166 // NOTE: If other queries than occlusion are supported, we will need to check
1167 // separately that:
1168 // a) The query ID passed is not the current active query for any target/type
1169 // b) There are no active queries for the requested target (and in the case
1170 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1171 // no query may be active for either if glBeginQuery targets either.
1172 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1173 {
1174 if (mState.activeQuery[i].get() != NULL)
1175 {
1176 return error(GL_INVALID_OPERATION);
1177 }
1178 }
1179
1180 QueryType qType;
1181 switch (target)
1182 {
1183 case GL_ANY_SAMPLES_PASSED_EXT:
1184 qType = QUERY_ANY_SAMPLES_PASSED;
1185 break;
1186 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1187 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1188 break;
1189 default:
1190 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001191 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001192 }
1193
1194 Query *queryObject = getQuery(query, true, target);
1195
1196 // check that name was obtained with glGenQueries
1197 if (!queryObject)
1198 {
1199 return error(GL_INVALID_OPERATION);
1200 }
1201
1202 // check for type mismatch
1203 if (queryObject->getType() != target)
1204 {
1205 return error(GL_INVALID_OPERATION);
1206 }
1207
1208 // set query as active for specified target
1209 mState.activeQuery[qType].set(queryObject);
1210
1211 // begin query
1212 queryObject->begin();
1213}
1214
1215void Context::endQuery(GLenum target)
1216{
1217 QueryType qType;
1218
1219 switch (target)
1220 {
1221 case GL_ANY_SAMPLES_PASSED_EXT:
1222 qType = QUERY_ANY_SAMPLES_PASSED;
1223 break;
1224 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1225 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1226 break;
1227 default:
1228 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001229 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001230 }
1231
1232 Query *queryObject = mState.activeQuery[qType].get();
1233
1234 if (queryObject == NULL)
1235 {
1236 return error(GL_INVALID_OPERATION);
1237 }
1238
1239 queryObject->end();
1240
1241 mState.activeQuery[qType].set(NULL);
1242}
1243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001244void Context::setFramebufferZero(Framebuffer *buffer)
1245{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001246 delete mFramebufferMap[0];
1247 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001248 if (mState.drawFramebuffer == 0)
1249 {
1250 mBoundDrawFramebuffer = buffer;
1251 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001252}
1253
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001254void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001255{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001256 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1257 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001258}
1259
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001260Framebuffer *Context::getFramebuffer(unsigned int handle)
1261{
1262 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1263
1264 if (framebuffer == mFramebufferMap.end())
1265 {
1266 return NULL;
1267 }
1268 else
1269 {
1270 return framebuffer->second;
1271 }
1272}
1273
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001274Fence *Context::getFence(unsigned int handle)
1275{
1276 FenceMap::iterator fence = mFenceMap.find(handle);
1277
1278 if (fence == mFenceMap.end())
1279 {
1280 return NULL;
1281 }
1282 else
1283 {
1284 return fence->second;
1285 }
1286}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001287
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001288Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1289{
1290 QueryMap::iterator query = mQueryMap.find(handle);
1291
1292 if (query == mQueryMap.end())
1293 {
1294 return NULL;
1295 }
1296 else
1297 {
1298 if (!query->second && create)
1299 {
1300 query->second = new Query(handle, type);
1301 query->second->addRef();
1302 }
1303 return query->second;
1304 }
1305}
1306
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001307Buffer *Context::getArrayBuffer()
1308{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001309 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001310}
1311
1312Buffer *Context::getElementArrayBuffer()
1313{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001314 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001315}
1316
1317Program *Context::getCurrentProgram()
1318{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001319 if (!mCachedCurrentProgram)
1320 {
1321 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1322 }
1323 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001324}
1325
1326Texture2D *Context::getTexture2D()
1327{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001328 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001329}
1330
1331TextureCubeMap *Context::getTextureCubeMap()
1332{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001333 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334}
1335
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001336Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001337{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001338 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001339
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001340 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001341 {
1342 switch (type)
1343 {
1344 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001345 case TEXTURE_2D: return mTexture2DZero.get();
1346 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001347 }
1348 }
1349
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001350 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001351}
1352
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001353bool Context::getBooleanv(GLenum pname, GLboolean *params)
1354{
1355 switch (pname)
1356 {
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001357 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1358 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1359 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001360 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001361 params[0] = mState.colorMaskRed;
1362 params[1] = mState.colorMaskGreen;
1363 params[2] = mState.colorMaskBlue;
1364 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001365 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001366 case GL_CULL_FACE: *params = mState.cullFace; break;
1367 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1368 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1369 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1370 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1371 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1372 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1373 case GL_BLEND: *params = mState.blend; break;
1374 case GL_DITHER: *params = mState.dither; break;
1375 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001376 default:
1377 return false;
1378 }
1379
1380 return true;
1381}
1382
1383bool Context::getFloatv(GLenum pname, GLfloat *params)
1384{
1385 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1386 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1387 // GetIntegerv as its native query function. As it would require conversion in any
1388 // case, this should make no difference to the calling application.
1389 switch (pname)
1390 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001391 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1392 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1393 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1394 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1395 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001396 case GL_ALIASED_LINE_WIDTH_RANGE:
1397 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1398 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1399 break;
1400 case GL_ALIASED_POINT_SIZE_RANGE:
1401 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001402 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 +00001403 break;
1404 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001405 params[0] = mState.zNear;
1406 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001407 break;
1408 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001409 params[0] = mState.colorClearValue.red;
1410 params[1] = mState.colorClearValue.green;
1411 params[2] = mState.colorClearValue.blue;
1412 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001413 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001414 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001415 params[0] = mState.blendColor.red;
1416 params[1] = mState.blendColor.green;
1417 params[2] = mState.blendColor.blue;
1418 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001419 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001420 default:
1421 return false;
1422 }
1423
1424 return true;
1425}
1426
1427bool Context::getIntegerv(GLenum pname, GLint *params)
1428{
1429 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1430 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1431 // GetIntegerv as its native query function. As it would require conversion in any
1432 // case, this should make no difference to the calling application. You may find it in
1433 // Context::getFloatv.
1434 switch (pname)
1435 {
1436 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1437 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001438 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001439 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1440 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001441 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001442 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001443 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001444 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001445 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001446 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1447 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001448 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1449 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1450 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001451 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001452 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1453 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001454 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001455 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1456 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001457 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001458 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1459 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1460 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1461 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1462 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1463 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1464 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1465 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1466 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1467 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1468 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1469 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1470 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1471 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1472 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1473 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1474 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1475 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1476 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1477 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1478 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1479 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1480 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1481 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001482 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1483 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001484 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001485 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001486 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001487 case GL_MAX_SAMPLES_ANGLE:
1488 {
1489 GLsizei maxSamples = getMaxSupportedSamples();
1490 if (maxSamples != 0)
1491 {
1492 *params = maxSamples;
1493 }
1494 else
1495 {
1496 return false;
1497 }
1498
1499 break;
1500 }
1501 case GL_SAMPLE_BUFFERS:
1502 case GL_SAMPLES:
1503 {
1504 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1505 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1506 {
1507 switch (pname)
1508 {
1509 case GL_SAMPLE_BUFFERS:
1510 if (framebuffer->getSamples() != 0)
1511 {
1512 *params = 1;
1513 }
1514 else
1515 {
1516 *params = 0;
1517 }
1518 break;
1519 case GL_SAMPLES:
1520 *params = framebuffer->getSamples();
1521 break;
1522 }
1523 }
1524 else
1525 {
1526 *params = 0;
1527 }
1528 }
1529 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001530 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1531 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1532 case GL_MAX_VIEWPORT_DIMS:
1533 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001534 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001535 params[0] = maxDimension;
1536 params[1] = maxDimension;
1537 }
1538 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001539 case GL_COMPRESSED_TEXTURE_FORMATS:
1540 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001541 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001542 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001543 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1544 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1545 }
1546 if (supportsDXT3Textures())
1547 {
1548 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1549 }
1550 if (supportsDXT5Textures())
1551 {
1552 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001553 }
1554 }
1555 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001556 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001557 params[0] = mState.viewportX;
1558 params[1] = mState.viewportY;
1559 params[2] = mState.viewportWidth;
1560 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001561 break;
1562 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001563 params[0] = mState.scissorX;
1564 params[1] = mState.scissorY;
1565 params[2] = mState.scissorWidth;
1566 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001567 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001568 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1569 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001570 case GL_RED_BITS:
1571 case GL_GREEN_BITS:
1572 case GL_BLUE_BITS:
1573 case GL_ALPHA_BITS:
1574 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001575 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001576 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001577
1578 if (colorbuffer)
1579 {
1580 switch (pname)
1581 {
1582 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1583 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1584 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1585 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1586 }
1587 }
1588 else
1589 {
1590 *params = 0;
1591 }
1592 }
1593 break;
1594 case GL_DEPTH_BITS:
1595 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001596 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001597 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001598
1599 if (depthbuffer)
1600 {
1601 *params = depthbuffer->getDepthSize();
1602 }
1603 else
1604 {
1605 *params = 0;
1606 }
1607 }
1608 break;
1609 case GL_STENCIL_BITS:
1610 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001611 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001612 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001613
1614 if (stencilbuffer)
1615 {
1616 *params = stencilbuffer->getStencilSize();
1617 }
1618 else
1619 {
1620 *params = 0;
1621 }
1622 }
1623 break;
1624 case GL_TEXTURE_BINDING_2D:
1625 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001626 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001627 {
1628 error(GL_INVALID_OPERATION);
1629 return false;
1630 }
1631
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001632 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001633 }
1634 break;
1635 case GL_TEXTURE_BINDING_CUBE_MAP:
1636 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001637 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001638 {
1639 error(GL_INVALID_OPERATION);
1640 return false;
1641 }
1642
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001643 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001644 }
1645 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001646 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1647 *params = mResetStrategy;
1648 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001649 default:
1650 return false;
1651 }
1652
1653 return true;
1654}
1655
1656bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1657{
1658 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1659 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1660 // to the fact that it is stored internally as a float, and so would require conversion
1661 // if returned from Context::getIntegerv. Since this conversion is already implemented
1662 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1663 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1664 // application.
1665 switch (pname)
1666 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001667 case GL_COMPRESSED_TEXTURE_FORMATS:
1668 {
1669 *type = GL_INT;
1670 *numParams = mNumCompressedTextureFormats;
1671 }
1672 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001673 case GL_SHADER_BINARY_FORMATS:
1674 {
1675 *type = GL_INT;
1676 *numParams = 0;
1677 }
1678 break;
1679 case GL_MAX_VERTEX_ATTRIBS:
1680 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1681 case GL_MAX_VARYING_VECTORS:
1682 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1683 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1684 case GL_MAX_TEXTURE_IMAGE_UNITS:
1685 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1686 case GL_MAX_RENDERBUFFER_SIZE:
1687 case GL_NUM_SHADER_BINARY_FORMATS:
1688 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1689 case GL_ARRAY_BUFFER_BINDING:
1690 case GL_FRAMEBUFFER_BINDING:
1691 case GL_RENDERBUFFER_BINDING:
1692 case GL_CURRENT_PROGRAM:
1693 case GL_PACK_ALIGNMENT:
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001694 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001695 case GL_UNPACK_ALIGNMENT:
1696 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001697 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001698 case GL_RED_BITS:
1699 case GL_GREEN_BITS:
1700 case GL_BLUE_BITS:
1701 case GL_ALPHA_BITS:
1702 case GL_DEPTH_BITS:
1703 case GL_STENCIL_BITS:
1704 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1705 case GL_CULL_FACE_MODE:
1706 case GL_FRONT_FACE:
1707 case GL_ACTIVE_TEXTURE:
1708 case GL_STENCIL_FUNC:
1709 case GL_STENCIL_VALUE_MASK:
1710 case GL_STENCIL_REF:
1711 case GL_STENCIL_FAIL:
1712 case GL_STENCIL_PASS_DEPTH_FAIL:
1713 case GL_STENCIL_PASS_DEPTH_PASS:
1714 case GL_STENCIL_BACK_FUNC:
1715 case GL_STENCIL_BACK_VALUE_MASK:
1716 case GL_STENCIL_BACK_REF:
1717 case GL_STENCIL_BACK_FAIL:
1718 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1719 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1720 case GL_DEPTH_FUNC:
1721 case GL_BLEND_SRC_RGB:
1722 case GL_BLEND_SRC_ALPHA:
1723 case GL_BLEND_DST_RGB:
1724 case GL_BLEND_DST_ALPHA:
1725 case GL_BLEND_EQUATION_RGB:
1726 case GL_BLEND_EQUATION_ALPHA:
1727 case GL_STENCIL_WRITEMASK:
1728 case GL_STENCIL_BACK_WRITEMASK:
1729 case GL_STENCIL_CLEAR_VALUE:
1730 case GL_SUBPIXEL_BITS:
1731 case GL_MAX_TEXTURE_SIZE:
1732 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1733 case GL_SAMPLE_BUFFERS:
1734 case GL_SAMPLES:
1735 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1736 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1737 case GL_TEXTURE_BINDING_2D:
1738 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001739 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001740 {
1741 *type = GL_INT;
1742 *numParams = 1;
1743 }
1744 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001745 case GL_MAX_SAMPLES_ANGLE:
1746 {
1747 if (getMaxSupportedSamples() != 0)
1748 {
1749 *type = GL_INT;
1750 *numParams = 1;
1751 }
1752 else
1753 {
1754 return false;
1755 }
1756 }
1757 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001758 case GL_MAX_VIEWPORT_DIMS:
1759 {
1760 *type = GL_INT;
1761 *numParams = 2;
1762 }
1763 break;
1764 case GL_VIEWPORT:
1765 case GL_SCISSOR_BOX:
1766 {
1767 *type = GL_INT;
1768 *numParams = 4;
1769 }
1770 break;
1771 case GL_SHADER_COMPILER:
1772 case GL_SAMPLE_COVERAGE_INVERT:
1773 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001774 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1775 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1776 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1777 case GL_SAMPLE_COVERAGE:
1778 case GL_SCISSOR_TEST:
1779 case GL_STENCIL_TEST:
1780 case GL_DEPTH_TEST:
1781 case GL_BLEND:
1782 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001783 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001784 {
1785 *type = GL_BOOL;
1786 *numParams = 1;
1787 }
1788 break;
1789 case GL_COLOR_WRITEMASK:
1790 {
1791 *type = GL_BOOL;
1792 *numParams = 4;
1793 }
1794 break;
1795 case GL_POLYGON_OFFSET_FACTOR:
1796 case GL_POLYGON_OFFSET_UNITS:
1797 case GL_SAMPLE_COVERAGE_VALUE:
1798 case GL_DEPTH_CLEAR_VALUE:
1799 case GL_LINE_WIDTH:
1800 {
1801 *type = GL_FLOAT;
1802 *numParams = 1;
1803 }
1804 break;
1805 case GL_ALIASED_LINE_WIDTH_RANGE:
1806 case GL_ALIASED_POINT_SIZE_RANGE:
1807 case GL_DEPTH_RANGE:
1808 {
1809 *type = GL_FLOAT;
1810 *numParams = 2;
1811 }
1812 break;
1813 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001814 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001815 {
1816 *type = GL_FLOAT;
1817 *numParams = 4;
1818 }
1819 break;
1820 default:
1821 return false;
1822 }
1823
1824 return true;
1825}
1826
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827// Applies the render target surface, depth stencil surface, viewport rectangle and
1828// scissor rectangle to the Direct3D 9 device
1829bool Context::applyRenderTarget(bool ignoreViewport)
1830{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001831 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832
1833 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1834 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001835 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836 }
1837
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001838 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001839 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1840 if (renderTargetSerial != mAppliedRenderTargetSerial)
1841 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001842 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001843 if (!renderTarget)
1844 {
1845 return false; // Context must be lost
1846 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001847 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001848 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001849 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 +00001850 renderTargetChanged = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001851 renderTarget->Release();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001852 }
1853
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001854 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001855 unsigned int depthbufferSerial = 0;
1856 unsigned int stencilbufferSerial = 0;
1857 if (framebufferObject->getDepthbufferType() != GL_NONE)
1858 {
1859 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001860 if (!depthStencil)
1861 {
1862 ERR("Depth stencil pointer unexpectedly null.");
1863 return false;
1864 }
1865
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001866 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1867 }
1868 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1869 {
1870 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001871 if (!depthStencil)
1872 {
1873 ERR("Depth stencil pointer unexpectedly null.");
1874 return false;
1875 }
1876
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001877 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1878 }
1879
1880 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001881 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001882 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001883 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001884 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001885 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001886 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001887 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001888 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001890 if (!mRenderTargetDescInitialized || renderTargetChanged)
1891 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001892 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001893 if (!renderTarget)
1894 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001895 return false; // Context must be lost
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001896 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001897 renderTarget->GetDesc(&mRenderTargetDesc);
1898 mRenderTargetDescInitialized = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001899 renderTarget->Release();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001900 }
1901
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001902 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001904 float zNear = clamp01(mState.zNear);
1905 float zFar = clamp01(mState.zFar);
1906
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907 if (ignoreViewport)
1908 {
1909 viewport.X = 0;
1910 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001911 viewport.Width = mRenderTargetDesc.Width;
1912 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913 viewport.MinZ = 0.0f;
1914 viewport.MaxZ = 1.0f;
1915 }
1916 else
1917 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001918 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1919 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1920 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1921 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1922 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 +00001923 viewport.MinZ = zNear;
1924 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925 }
1926
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001927 if (viewport.Width <= 0 || viewport.Height <= 0)
1928 {
1929 return false; // Nothing to render
1930 }
1931
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001932 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001933 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001934 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001935 mSetViewport = viewport;
1936 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001937 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001940 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001942 if (mState.scissorTest)
1943 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001944 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1945 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1946 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1947 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1948 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001949 mDevice->SetScissorRect(&rect);
1950 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001951 }
1952 else
1953 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001954 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001955 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001956
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001957 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001958 }
1959
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001960 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001961 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 Program *programObject = getCurrentProgram();
1963
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001964 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001965 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001966 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001967
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001968 // These values are used for computing gl_FragCoord in Program::linkVaryings(). The approach depends on Shader Model 3.0 support.
1969 GLint coord = programObject->getDxCoordLocation();
1970 float h = mSupportsShaderModel3 ? mRenderTargetDesc.Height : mState.viewportHeight / 2.0f;
1971 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, h,
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001972 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1973 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001974 programObject->setUniform4fv(coord, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001975
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001976 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001977 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001978 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001979
daniel@transgaming.com31754962010-11-28 02:02:52 +00001980 GLint depthRange = programObject->getDxDepthRangeLocation();
1981 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1982 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001983 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001984 }
1985
1986 return true;
1987}
1988
1989// 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 +00001990void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001992 Program *programObject = getCurrentProgram();
1993
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001994 Framebuffer *framebufferObject = getDrawFramebuffer();
1995
1996 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1997
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001998 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001999 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002000 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002001
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002002 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002003 GLint alwaysFront = !isTriangleMode(drawMode);
2004 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
2005
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002006 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002007 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2008 // Apparently some ATI cards have a bug where a draw with a zero color
2009 // write mask can cause later draws to have incorrect results. Instead,
2010 // set a nonzero color write mask but modify the blend state so that no
2011 // drawing is done.
2012 // http://code.google.com/p/angleproject/issues/detail?id=169
2013
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002014 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002016 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002017 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002018 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019 }
2020 else
2021 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002022 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023 }
2024
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002025 mCullStateDirty = false;
2026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002028 if (mDepthStateDirty)
2029 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00002030 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002032 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2033 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034 }
2035 else
2036 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002037 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002039
2040 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002041 }
2042
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002043 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2044 {
2045 mBlendStateDirty = true;
2046 mMaskStateDirty = true;
2047 }
2048
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002049 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002051 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002052 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002053 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002054
2055 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2056 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2057 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002058 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002059 }
2060 else
2061 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002062 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002063 unorm<8>(mState.blendColor.alpha),
2064 unorm<8>(mState.blendColor.alpha),
2065 unorm<8>(mState.blendColor.alpha)));
2066 }
2067
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002068 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2069 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2070 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002071
2072 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2073 mState.destBlendRGB != mState.destBlendAlpha ||
2074 mState.blendEquationRGB != mState.blendEquationAlpha)
2075 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002076 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002077
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002078 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2079 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2080 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002081 }
2082 else
2083 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002084 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002085 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002086 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002087 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002088 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002089 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002090 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002091
2092 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 }
2094
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002095 if (mStencilStateDirty || mFrontFaceDirty)
2096 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002097 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002098 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002099 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2100 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002101
2102 // FIXME: Unsupported by D3D9
2103 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2104 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2105 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2106 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2107 mState.stencilRef != mState.stencilBackRef ||
2108 mState.stencilMask != mState.stencilBackMask)
2109 {
2110 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2111 return error(GL_INVALID_OPERATION);
2112 }
2113
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002114 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002115 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002116 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2117
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002118 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2119 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002120 es2dx::ConvertComparison(mState.stencilFunc));
2121
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002122 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2123 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002124
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002125 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002126 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002127 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002128 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002129 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002130 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2131
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002132 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2133 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002134 es2dx::ConvertComparison(mState.stencilBackFunc));
2135
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002136 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2137 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002138
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002139 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002140 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002141 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002142 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002143 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002144 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2145 }
2146 else
2147 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002148 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002149 }
2150
2151 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002152 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002153 }
2154
2155 if (mMaskStateDirty)
2156 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002157 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2158 mState.colorMaskBlue, mState.colorMaskAlpha);
2159 if (colorMask == 0 && !zeroColorMaskAllowed)
2160 {
2161 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002162 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2163 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002164
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002165 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2166 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2167 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002168 }
2169 else
2170 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002171 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002172 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002173 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002174
2175 mMaskStateDirty = false;
2176 }
2177
2178 if (mPolygonOffsetStateDirty)
2179 {
2180 if (mState.polygonOffsetFill)
2181 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002182 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002183 if (depthbuffer)
2184 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002185 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002186 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002187 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002188 }
2189 }
2190 else
2191 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002192 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2193 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002194 }
2195
2196 mPolygonOffsetStateDirty = false;
2197 }
2198
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002199 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002200 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002201 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002202 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002203 FIXME("Sample alpha to coverage is unimplemented.");
2204 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002205
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002206 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002207 if (mState.sampleCoverage)
2208 {
2209 unsigned int mask = 0;
2210 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002211 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002212 float threshold = 0.5f;
2213
2214 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002215 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002216 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002217
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002218 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002219 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002220 threshold += 1.0f;
2221 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002222 }
2223 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002224 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002225
2226 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002227 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002228 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002229 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002230
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002231 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002232 }
2233 else
2234 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002235 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002236 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002237
2238 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002239 }
2240
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002241 if (mDitherStateDirty)
2242 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002243 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002244
2245 mDitherStateDirty = false;
2246 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247}
2248
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002249GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002250{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002251 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002252
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +00002253 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002254 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002255 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002256 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002257 }
2258
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002259 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram(), instances, repeatDraw);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002260}
2261
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002262// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00002263GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002264{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002265 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002266
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002267 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002268 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002269 if (indexInfo->serial != mAppliedIBSerial)
2270 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002271 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002272 mAppliedIBSerial = indexInfo->serial;
2273 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002274 }
2275
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002276 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277}
2278
2279// Applies the shaders and shader constants to the Direct3D 9 device
2280void Context::applyShaders()
2281{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002283 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002284 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002285 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2286 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2287
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002288 mDevice->SetPixelShader(pixelShader);
2289 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002290 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002291 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002292 }
2293
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 programObject->applyUniforms();
2295}
2296
2297// Applies the textures and sampler states to the Direct3D 9 device
2298void Context::applyTextures()
2299{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002300 applyTextures(SAMPLER_PIXEL);
2301
2302 if (mSupportsVertexTexture)
2303 {
2304 applyTextures(SAMPLER_VERTEX);
2305 }
2306}
2307
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002308// For each Direct3D 9 sampler of either the pixel or vertex stage,
2309// looks up the corresponding OpenGL texture image unit and texture type,
2310// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002311void Context::applyTextures(SamplerType type)
2312{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313 Program *programObject = getCurrentProgram();
2314
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002315 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 +00002316 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2317 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002318 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002319
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002320 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002322 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002323 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002324
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002325 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002327 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002328
2329 Texture *texture = getSamplerTexture(textureUnit, textureType);
2330
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002331 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002332 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002333 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2334
2335 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002336 {
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002337 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002338 {
2339 GLenum wrapS = texture->getWrapS();
2340 GLenum wrapT = texture->getWrapT();
2341 GLenum minFilter = texture->getMinFilter();
2342 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002343
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002344 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2345 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002346
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002347 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002348 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2349 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002350 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2351 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002352 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002354 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002355 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002356 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002357 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002358 }
2359 else
2360 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002361 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002362 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002363
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002364 appliedTextureSerial[samplerIndex] = texture->getTextureSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002365 texture->resetDirty();
2366 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002367 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002368 else
2369 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002370 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002371 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002372 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002373 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002374 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002375 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002377
2378 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2379 {
2380 if (appliedTextureSerial[samplerIndex] != 0)
2381 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002382 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002383 appliedTextureSerial[samplerIndex] = 0;
2384 }
2385 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386}
2387
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002388void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2389 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002390{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002391 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002392
2393 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2394 {
2395 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2396 }
2397
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002398 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2399 {
2400 return error(GL_INVALID_OPERATION);
2401 }
2402
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002403 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2404 // sized query sanity check
2405 if (bufSize)
2406 {
2407 int requiredSize = outputPitch * height;
2408 if (requiredSize > *bufSize)
2409 {
2410 return error(GL_INVALID_OPERATION);
2411 }
2412 }
2413
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002415 if (!renderTarget)
2416 {
2417 return; // Context must be lost, return silently
2418 }
2419
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420 D3DSURFACE_DESC desc;
2421 renderTarget->GetDesc(&desc);
2422
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2424 {
2425 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002426 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002427 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 }
2429
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002430 HRESULT result;
2431 IDirect3DSurface9 *systemSurface = NULL;
2432 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002433 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002434 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2435 if (directToPixels)
2436 {
2437 // Use the pixels ptr as a shared handle to write directly into client's memory
2438 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2439 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2440 if (FAILED(result))
2441 {
2442 // Try again without the shared handle
2443 directToPixels = false;
2444 }
2445 }
2446
2447 if (!directToPixels)
2448 {
2449 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2450 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2451 if (FAILED(result))
2452 {
2453 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2454 return error(GL_OUT_OF_MEMORY);
2455 }
2456 }
2457
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002458 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002459 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002460 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002461
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 if (FAILED(result))
2463 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464 systemSurface->Release();
2465
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002466 // It turns out that D3D will sometimes produce more error
2467 // codes than those documented.
2468 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002469 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002470 else
2471 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002472 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002473 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002474 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002475
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 }
2477
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002478 if (directToPixels)
2479 {
2480 systemSurface->Release();
2481 return;
2482 }
2483
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002485 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2486 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2487 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2488 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2489 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490
2491 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2492
2493 if (FAILED(result))
2494 {
2495 UNREACHABLE();
2496 systemSurface->Release();
2497
2498 return; // No sensible error to generate
2499 }
2500
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002502 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002503
2504 unsigned char *source;
2505 int inputPitch;
2506 if (getPackReverseRowOrder())
2507 {
2508 source = (unsigned char*)lock.pBits;
2509 inputPitch = lock.Pitch;
2510 }
2511 else
2512 {
2513 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2514 inputPitch = -lock.Pitch;
2515 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002516
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002517 for (int j = 0; j < rect.bottom - rect.top; j++)
2518 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002519 if (desc.Format == D3DFMT_A8R8G8B8 &&
2520 format == GL_BGRA_EXT &&
2521 type == GL_UNSIGNED_BYTE)
2522 {
2523 // Fast path for EXT_read_format_bgra, given
2524 // an RGBA source buffer. Note that buffers with no
2525 // alpha go through the slow path below.
2526 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002527 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002528 (rect.right - rect.left) * 4);
2529 continue;
2530 }
2531
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532 for (int i = 0; i < rect.right - rect.left; i++)
2533 {
2534 float r;
2535 float g;
2536 float b;
2537 float a;
2538
2539 switch (desc.Format)
2540 {
2541 case D3DFMT_R5G6B5:
2542 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002543 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544
2545 a = 1.0f;
2546 b = (rgb & 0x001F) * (1.0f / 0x001F);
2547 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2548 r = (rgb & 0xF800) * (1.0f / 0xF800);
2549 }
2550 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551 case D3DFMT_A1R5G5B5:
2552 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002553 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002554
2555 a = (argb & 0x8000) ? 1.0f : 0.0f;
2556 b = (argb & 0x001F) * (1.0f / 0x001F);
2557 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2558 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2559 }
2560 break;
2561 case D3DFMT_A8R8G8B8:
2562 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002563 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564
2565 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2566 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2567 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2568 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2569 }
2570 break;
2571 case D3DFMT_X8R8G8B8:
2572 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002573 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002574
2575 a = 1.0f;
2576 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2577 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2578 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2579 }
2580 break;
2581 case D3DFMT_A2R10G10B10:
2582 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002583 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584
2585 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2586 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2587 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2588 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2589 }
2590 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002591 case D3DFMT_A32B32G32R32F:
2592 {
2593 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002594 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2595 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2596 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2597 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002598 }
2599 break;
2600 case D3DFMT_A16B16G16R16F:
2601 {
2602 // float formats in D3D are stored rgba, rather than the other way round
2603 float abgr[4];
2604
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002605 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002606
2607 a = abgr[3];
2608 b = abgr[2];
2609 g = abgr[1];
2610 r = abgr[0];
2611 }
2612 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613 default:
2614 UNIMPLEMENTED(); // FIXME
2615 UNREACHABLE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002616 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617 }
2618
2619 switch (format)
2620 {
2621 case GL_RGBA:
2622 switch (type)
2623 {
2624 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002625 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2626 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2627 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2628 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 break;
2630 default: UNREACHABLE();
2631 }
2632 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002633 case GL_BGRA_EXT:
2634 switch (type)
2635 {
2636 case GL_UNSIGNED_BYTE:
2637 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2638 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2639 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2640 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2641 break;
2642 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2643 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2644 // this type is packed as follows:
2645 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2646 // --------------------------------------------------------------------------------
2647 // | 4th | 3rd | 2nd | 1st component |
2648 // --------------------------------------------------------------------------------
2649 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2650 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2651 ((unsigned short)(15 * a + 0.5f) << 12)|
2652 ((unsigned short)(15 * r + 0.5f) << 8) |
2653 ((unsigned short)(15 * g + 0.5f) << 4) |
2654 ((unsigned short)(15 * b + 0.5f) << 0);
2655 break;
2656 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2657 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2658 // this type is packed as follows:
2659 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2660 // --------------------------------------------------------------------------------
2661 // | 4th | 3rd | 2nd | 1st component |
2662 // --------------------------------------------------------------------------------
2663 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2664 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2665 ((unsigned short)( a + 0.5f) << 15) |
2666 ((unsigned short)(31 * r + 0.5f) << 10) |
2667 ((unsigned short)(31 * g + 0.5f) << 5) |
2668 ((unsigned short)(31 * b + 0.5f) << 0);
2669 break;
2670 default: UNREACHABLE();
2671 }
2672 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002673 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002674 switch (type)
2675 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002676 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002677 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2678 ((unsigned short)(31 * b + 0.5f) << 0) |
2679 ((unsigned short)(63 * g + 0.5f) << 5) |
2680 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 break;
2682 default: UNREACHABLE();
2683 }
2684 break;
2685 default: UNREACHABLE();
2686 }
2687 }
2688 }
2689
2690 systemSurface->UnlockRect();
2691
2692 systemSurface->Release();
2693}
2694
2695void Context::clear(GLbitfield mask)
2696{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002697 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002698
2699 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2700 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002701 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002702 }
2703
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002704 DWORD flags = 0;
2705
2706 if (mask & GL_COLOR_BUFFER_BIT)
2707 {
2708 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002709
2710 if (framebufferObject->getColorbufferType() != GL_NONE)
2711 {
2712 flags |= D3DCLEAR_TARGET;
2713 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714 }
2715
2716 if (mask & GL_DEPTH_BUFFER_BIT)
2717 {
2718 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002719 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720 {
2721 flags |= D3DCLEAR_ZBUFFER;
2722 }
2723 }
2724
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725 GLuint stencilUnmasked = 0x0;
2726
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002727 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002728 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002729 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002730 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002732 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002733 if (!depthStencil)
2734 {
2735 ERR("Depth stencil pointer unexpectedly null.");
2736 return;
2737 }
2738
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002739 D3DSURFACE_DESC desc;
2740 depthStencil->GetDesc(&desc);
2741
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002742 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002743 stencilUnmasked = (0x1 << stencilSize) - 1;
2744
2745 if (stencilUnmasked != 0x0)
2746 {
2747 flags |= D3DCLEAR_STENCIL;
2748 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002749 }
2750 }
2751
2752 if (mask != 0)
2753 {
2754 return error(GL_INVALID_VALUE);
2755 }
2756
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002757 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2758 {
2759 return;
2760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002761
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002762 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002763 unorm<8>(mState.colorClearValue.red),
2764 unorm<8>(mState.colorClearValue.green),
2765 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002766 float depth = clamp01(mState.depthClearValue);
2767 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002769 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002770
2771 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002772 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002773 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002774 !(mState.colorMaskRed && mState.colorMaskGreen &&
2775 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776
2777 if (needMaskedColorClear || needMaskedStencilClear)
2778 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002779 // State which is altered in all paths from this point to the clear call is saved.
2780 // State which is altered in only some paths will be flagged dirty in the case that
2781 // that path is taken.
2782 HRESULT hr;
2783 if (mMaskedClearSavedState == NULL)
2784 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002785 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002786 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2787
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002788 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2789 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2790 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2791 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2792 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2793 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2794 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2795 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2796 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2797 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2798 mDevice->SetPixelShader(NULL);
2799 mDevice->SetVertexShader(NULL);
2800 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2801 mDevice->SetStreamSource(0, NULL, 0, 0);
2802 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2803 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2804 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2805 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2806 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2807 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2808 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002809
2810 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2811 {
2812 mDevice->SetStreamSourceFreq(i, 1);
2813 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002814
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002815 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002816 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2817 }
2818
2819 ASSERT(mMaskedClearSavedState != NULL);
2820
2821 if (mMaskedClearSavedState != NULL)
2822 {
2823 hr = mMaskedClearSavedState->Capture();
2824 ASSERT(SUCCEEDED(hr));
2825 }
2826
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002827 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2828 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2829 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2830 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2831 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2832 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2833 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2834 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002835
2836 if (flags & D3DCLEAR_TARGET)
2837 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002838 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839 }
2840 else
2841 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002842 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002843 }
2844
2845 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2846 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002847 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2848 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2849 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2850 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2851 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2852 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2853 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2854 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002855 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002856 }
2857 else
2858 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002859 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860 }
2861
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002862 mDevice->SetPixelShader(NULL);
2863 mDevice->SetVertexShader(NULL);
2864 mDevice->SetFVF(D3DFVF_XYZRHW);
2865 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2866 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2867 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2868 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2869 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2870 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2871 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002872
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002873 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2874 {
2875 mDevice->SetStreamSourceFreq(i, 1);
2876 }
2877
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002878 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2879 quad[0][0] = -0.5f;
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002880 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002881 quad[0][2] = 0.0f;
2882 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002884 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2885 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002886 quad[1][2] = 0.0f;
2887 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002888
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002889 quad[2][0] = -0.5f;
2890 quad[2][1] = -0.5f;
2891 quad[2][2] = 0.0f;
2892 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002893
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002894 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002895 quad[3][1] = -0.5f;
2896 quad[3][2] = 0.0f;
2897 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002898
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002899 mDisplay->startScene();
2900 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002901
2902 if (flags & D3DCLEAR_ZBUFFER)
2903 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002904 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2905 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2906 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002907 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002908
2909 if (mMaskedClearSavedState != NULL)
2910 {
2911 mMaskedClearSavedState->Apply();
2912 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002913 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002914 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002915 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002916 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002917 }
2918}
2919
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002920void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002921{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002922 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002923 {
2924 return error(GL_INVALID_OPERATION);
2925 }
2926
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002927 D3DPRIMITIVETYPE primitiveType;
2928 int primitiveCount;
2929
2930 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2931 return error(GL_INVALID_ENUM);
2932
2933 if (primitiveCount <= 0)
2934 {
2935 return;
2936 }
2937
2938 if (!applyRenderTarget(false))
2939 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002940 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941 }
2942
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002943 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002944
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002945 GLsizei repeatDraw = 1;
2946 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002947 if (err != GL_NO_ERROR)
2948 {
2949 return error(err);
2950 }
2951
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002952 applyShaders();
2953 applyTextures();
2954
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002955 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002956 {
2957 return error(GL_INVALID_OPERATION);
2958 }
2959
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002960 if (!cullSkipsDraw(mode))
2961 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002962 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002963
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002964 if (mode == GL_LINE_LOOP)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002965 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002966 drawLineLoop(count, GL_NONE, NULL, 0);
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002967 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002968 else if (instances > 0)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002969 {
2970 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
2971 if (countingIB)
2972 {
2973 if (mAppliedIBSerial != countingIB->getSerial())
2974 {
2975 mDevice->SetIndices(countingIB->getBuffer());
2976 mAppliedIBSerial = countingIB->getSerial();
2977 }
2978
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002979 for (int i = 0; i < repeatDraw; i++)
2980 {
2981 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
2982 }
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002983 }
2984 else
2985 {
2986 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
2987 return error(GL_OUT_OF_MEMORY);
2988 }
2989 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002990 else // Regular case
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002991 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002992 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002993 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002994 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002995}
2996
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002997void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002998{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002999 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000 {
3001 return error(GL_INVALID_OPERATION);
3002 }
3003
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003004 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003005 {
3006 return error(GL_INVALID_OPERATION);
3007 }
3008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003009 D3DPRIMITIVETYPE primitiveType;
3010 int primitiveCount;
3011
3012 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3013 return error(GL_INVALID_ENUM);
3014
3015 if (primitiveCount <= 0)
3016 {
3017 return;
3018 }
3019
3020 if (!applyRenderTarget(false))
3021 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00003022 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003023 }
3024
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003025 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00003026
3027 TranslatedIndexData indexInfo;
3028 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3029 if (err != GL_NO_ERROR)
3030 {
3031 return error(err);
3032 }
3033
daniel@transgaming.com83921382011-01-08 05:46:00 +00003034 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003035 GLsizei repeatDraw = 1;
3036 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00003037 if (err != GL_NO_ERROR)
3038 {
3039 return error(err);
3040 }
3041
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003042 applyShaders();
3043 applyTextures();
3044
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00003045 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003046 {
3047 return error(GL_INVALID_OPERATION);
3048 }
3049
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003050 if (!cullSkipsDraw(mode))
3051 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003052 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003053
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003054 if (mode == GL_LINE_LOOP)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003055 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003056 drawLineLoop(count, type, indices, indexInfo.minIndex);
3057 }
3058 else
3059 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003060 for (int i = 0; i < repeatDraw; i++)
3061 {
3062 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3063 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003064 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003065 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003066}
3067
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00003068// Implements glFlush when block is false, glFinish when block is true
3069void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003070{
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +00003071 mDisplay->sync(block);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003072}
3073
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003074void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003075{
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003076 // Get the raw indices for an indexed draw
3077 if (type != GL_NONE && mState.elementArrayBuffer.get())
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003078 {
3079 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3080 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3081 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3082 }
3083
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003084 UINT startIndex = 0;
3085 bool succeeded = false;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003086
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003087 if (supports32bitIndices())
3088 {
3089 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3090
3091 if (!mLineLoopIB)
3092 {
3093 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3094 }
3095
3096 if (mLineLoopIB)
3097 {
3098 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3099
3100 UINT offset = 0;
3101 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3102 startIndex = offset / 4;
3103
3104 if (data)
3105 {
3106 switch (type)
3107 {
3108 case GL_NONE: // Non-indexed draw
3109 for (int i = 0; i < count; i++)
3110 {
3111 data[i] = i;
3112 }
3113 data[count] = 0;
3114 break;
3115 case GL_UNSIGNED_BYTE:
3116 for (int i = 0; i < count; i++)
3117 {
3118 data[i] = static_cast<const GLubyte*>(indices)[i];
3119 }
3120 data[count] = static_cast<const GLubyte*>(indices)[0];
3121 break;
3122 case GL_UNSIGNED_SHORT:
3123 for (int i = 0; i < count; i++)
3124 {
3125 data[i] = static_cast<const GLushort*>(indices)[i];
3126 }
3127 data[count] = static_cast<const GLushort*>(indices)[0];
3128 break;
3129 case GL_UNSIGNED_INT:
3130 for (int i = 0; i < count; i++)
3131 {
3132 data[i] = static_cast<const GLuint*>(indices)[i];
3133 }
3134 data[count] = static_cast<const GLuint*>(indices)[0];
3135 break;
3136 default: UNREACHABLE();
3137 }
3138
3139 mLineLoopIB->unmap();
3140 succeeded = true;
3141 }
3142 }
3143 }
3144 else
3145 {
3146 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3147
3148 if (!mLineLoopIB)
3149 {
3150 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3151 }
3152
3153 if (mLineLoopIB)
3154 {
3155 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3156
3157 UINT offset = 0;
3158 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3159 startIndex = offset / 2;
3160
3161 if (data)
3162 {
3163 switch (type)
3164 {
3165 case GL_NONE: // Non-indexed draw
3166 for (int i = 0; i < count; i++)
3167 {
3168 data[i] = i;
3169 }
3170 data[count] = 0;
3171 break;
3172 case GL_UNSIGNED_BYTE:
3173 for (int i = 0; i < count; i++)
3174 {
3175 data[i] = static_cast<const GLubyte*>(indices)[i];
3176 }
3177 data[count] = static_cast<const GLubyte*>(indices)[0];
3178 break;
3179 case GL_UNSIGNED_SHORT:
3180 for (int i = 0; i < count; i++)
3181 {
3182 data[i] = static_cast<const GLushort*>(indices)[i];
3183 }
3184 data[count] = static_cast<const GLushort*>(indices)[0];
3185 break;
3186 case GL_UNSIGNED_INT:
3187 for (int i = 0; i < count; i++)
3188 {
3189 data[i] = static_cast<const GLuint*>(indices)[i];
3190 }
3191 data[count] = static_cast<const GLuint*>(indices)[0];
3192 break;
3193 default: UNREACHABLE();
3194 }
3195
3196 mLineLoopIB->unmap();
3197 succeeded = true;
3198 }
3199 }
3200 }
3201
3202 if (succeeded)
3203 {
3204 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3205 {
3206 mDevice->SetIndices(mLineLoopIB->getBuffer());
3207 mAppliedIBSerial = mLineLoopIB->getSerial();
3208 }
3209
3210 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3211 }
3212 else
3213 {
3214 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3215 return error(GL_OUT_OF_MEMORY);
3216 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003217}
3218
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003219void Context::recordInvalidEnum()
3220{
3221 mInvalidEnum = true;
3222}
3223
3224void Context::recordInvalidValue()
3225{
3226 mInvalidValue = true;
3227}
3228
3229void Context::recordInvalidOperation()
3230{
3231 mInvalidOperation = true;
3232}
3233
3234void Context::recordOutOfMemory()
3235{
3236 mOutOfMemory = true;
3237}
3238
3239void Context::recordInvalidFramebufferOperation()
3240{
3241 mInvalidFramebufferOperation = true;
3242}
3243
3244// Get one of the recorded errors and clear its flag, if any.
3245// [OpenGL ES 2.0.24] section 2.5 page 13.
3246GLenum Context::getError()
3247{
3248 if (mInvalidEnum)
3249 {
3250 mInvalidEnum = false;
3251
3252 return GL_INVALID_ENUM;
3253 }
3254
3255 if (mInvalidValue)
3256 {
3257 mInvalidValue = false;
3258
3259 return GL_INVALID_VALUE;
3260 }
3261
3262 if (mInvalidOperation)
3263 {
3264 mInvalidOperation = false;
3265
3266 return GL_INVALID_OPERATION;
3267 }
3268
3269 if (mOutOfMemory)
3270 {
3271 mOutOfMemory = false;
3272
3273 return GL_OUT_OF_MEMORY;
3274 }
3275
3276 if (mInvalidFramebufferOperation)
3277 {
3278 mInvalidFramebufferOperation = false;
3279
3280 return GL_INVALID_FRAMEBUFFER_OPERATION;
3281 }
3282
3283 return GL_NO_ERROR;
3284}
3285
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003286GLenum Context::getResetStatus()
3287{
3288 if (mResetStatus == GL_NO_ERROR)
3289 {
3290 bool lost = mDisplay->testDeviceLost();
3291
3292 if (lost)
3293 {
3294 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3295 }
3296 }
3297
3298 GLenum status = mResetStatus;
3299
3300 if (mResetStatus != GL_NO_ERROR)
3301 {
3302 if (mDisplay->testDeviceResettable())
3303 {
3304 mResetStatus = GL_NO_ERROR;
3305 }
3306 }
3307
3308 return status;
3309}
3310
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003311bool Context::isResetNotificationEnabled()
3312{
3313 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3314}
3315
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003316bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003317{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003318 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003319}
3320
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003321int Context::getMaximumVaryingVectors() const
3322{
3323 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3324}
3325
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003326unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003327{
3328 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3329}
3330
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003331unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003332{
3333 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3334}
3335
daniel@transgaming.com458da142010-11-28 02:03:02 +00003336int Context::getMaximumFragmentUniformVectors() const
3337{
3338 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3339}
3340
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003341int Context::getMaxSupportedSamples() const
3342{
3343 return mMaxSupportedSamples;
3344}
3345
3346int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3347{
3348 if (requested == 0)
3349 {
3350 return requested;
3351 }
3352
3353 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3354 if (itr == mMultiSampleSupport.end())
3355 {
3356 return -1;
3357 }
3358
3359 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3360 {
3361 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3362 {
3363 return i;
3364 }
3365 }
3366
3367 return -1;
3368}
3369
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003370bool Context::supportsEventQueries() const
3371{
3372 return mSupportsEventQueries;
3373}
3374
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003375bool Context::supportsOcclusionQueries() const
3376{
3377 return mSupportsOcclusionQueries;
3378}
3379
gman@chromium.org50c526d2011-08-10 05:19:44 +00003380bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003381{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003382 return mSupportsDXT1Textures;
3383}
3384
3385bool Context::supportsDXT3Textures() const
3386{
3387 return mSupportsDXT3Textures;
3388}
3389
3390bool Context::supportsDXT5Textures() const
3391{
3392 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003393}
3394
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003395bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003396{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003397 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003398}
3399
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003400bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003401{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003402 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003403}
3404
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003405bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003406{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003407 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003408}
3409
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003410bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003411{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003412 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003413}
3414
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003415bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003416{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003417 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003418}
3419
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003420bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003421{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003422 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003423}
3424
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003425int Context::getMaximumRenderbufferDimension() const
3426{
3427 return mMaxRenderbufferDimension;
3428}
3429
3430int Context::getMaximumTextureDimension() const
3431{
3432 return mMaxTextureDimension;
3433}
3434
3435int Context::getMaximumCubeTextureDimension() const
3436{
3437 return mMaxCubeTextureDimension;
3438}
3439
3440int Context::getMaximumTextureLevel() const
3441{
3442 return mMaxTextureLevel;
3443}
3444
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003445bool Context::supportsLuminanceTextures() const
3446{
3447 return mSupportsLuminanceTextures;
3448}
3449
3450bool Context::supportsLuminanceAlphaTextures() const
3451{
3452 return mSupportsLuminanceAlphaTextures;
3453}
3454
daniel@transgaming.com83921382011-01-08 05:46:00 +00003455bool Context::supports32bitIndices() const
3456{
3457 return mSupports32bitIndices;
3458}
3459
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003460bool Context::supportsNonPower2Texture() const
3461{
3462 return mSupportsNonPower2Texture;
3463}
3464
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003465bool Context::supportsInstancing() const
3466{
3467 return mSupportsInstancing;
3468}
3469
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003470void Context::detachBuffer(GLuint buffer)
3471{
3472 // [OpenGL ES 2.0.24] section 2.9 page 22:
3473 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3474 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3475
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003476 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003477 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003478 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003479 }
3480
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003481 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003482 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003483 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003484 }
3485
3486 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3487 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003488 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003489 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003490 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003491 }
3492 }
3493}
3494
3495void Context::detachTexture(GLuint texture)
3496{
3497 // [OpenGL ES 2.0.24] section 3.8 page 84:
3498 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3499 // rebound to texture object zero
3500
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003501 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003502 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003503 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003504 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003505 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003506 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003507 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003508 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003509 }
3510 }
3511
3512 // [OpenGL ES 2.0.24] section 4.4 page 112:
3513 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3514 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3515 // image was attached in the currently bound framebuffer.
3516
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003517 Framebuffer *readFramebuffer = getReadFramebuffer();
3518 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003519
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003520 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003521 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003522 readFramebuffer->detachTexture(texture);
3523 }
3524
3525 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3526 {
3527 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003528 }
3529}
3530
3531void Context::detachFramebuffer(GLuint framebuffer)
3532{
3533 // [OpenGL ES 2.0.24] section 4.4 page 107:
3534 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3535 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3536
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003537 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003538 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003539 bindReadFramebuffer(0);
3540 }
3541
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003542 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003543 {
3544 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003545 }
3546}
3547
3548void Context::detachRenderbuffer(GLuint renderbuffer)
3549{
3550 // [OpenGL ES 2.0.24] section 4.4 page 109:
3551 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3552 // had been executed with the target RENDERBUFFER and name of zero.
3553
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003554 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003555 {
3556 bindRenderbuffer(0);
3557 }
3558
3559 // [OpenGL ES 2.0.24] section 4.4 page 111:
3560 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3561 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3562 // point to which this image was attached in the currently bound framebuffer.
3563
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003564 Framebuffer *readFramebuffer = getReadFramebuffer();
3565 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003566
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003567 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003568 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003569 readFramebuffer->detachRenderbuffer(renderbuffer);
3570 }
3571
3572 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3573 {
3574 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003575 }
3576}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003577
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003578Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003579{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003580 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003581
3582 if (t == NULL)
3583 {
3584 static const GLubyte color[] = { 0, 0, 0, 255 };
3585
3586 switch (type)
3587 {
3588 default:
3589 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003590 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003591
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003592 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003593 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003594 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003595 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003596 t = incomplete2d;
3597 }
3598 break;
3599
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003600 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003601 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003602 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003603
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003604 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3605 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3606 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3607 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3608 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3609 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003610
3611 t = incompleteCube;
3612 }
3613 break;
3614 }
3615
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003616 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003617 }
3618
3619 return t;
3620}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003621
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003622bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003623{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003624 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003625}
3626
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003627bool Context::isTriangleMode(GLenum drawMode)
3628{
3629 switch (drawMode)
3630 {
3631 case GL_TRIANGLES:
3632 case GL_TRIANGLE_FAN:
3633 case GL_TRIANGLE_STRIP:
3634 return true;
3635 case GL_POINTS:
3636 case GL_LINES:
3637 case GL_LINE_LOOP:
3638 case GL_LINE_STRIP:
3639 return false;
3640 default: UNREACHABLE();
3641 }
3642
3643 return false;
3644}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003645
3646void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3647{
3648 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3649
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003650 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3651 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3652 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3653 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003654
daniel@transgaming.com83921382011-01-08 05:46:00 +00003655 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003656}
3657
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00003658void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3659{
3660 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3661
3662 mState.vertexAttribute[index].mDivisor = divisor;
3663}
3664
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003665// keep list sorted in following order
3666// OES extensions
3667// EXT extensions
3668// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003669void Context::initExtensionString()
3670{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003671 mExtensionString = "";
3672
3673 // OES extensions
3674 if (supports32bitIndices())
3675 {
3676 mExtensionString += "GL_OES_element_index_uint ";
3677 }
3678
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003679 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003680 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003681 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003682
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003683 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003684 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003685 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003686 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003687 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003688 {
3689 mExtensionString += "GL_OES_texture_half_float_linear ";
3690 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003691 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003692 {
3693 mExtensionString += "GL_OES_texture_float ";
3694 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003695 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003696 {
3697 mExtensionString += "GL_OES_texture_float_linear ";
3698 }
3699
3700 if (supportsNonPower2Texture())
3701 {
3702 mExtensionString += "GL_OES_texture_npot ";
3703 }
3704
3705 // Multi-vendor (EXT) extensions
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003706 if (supportsOcclusionQueries())
3707 {
3708 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3709 }
3710
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003711 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003712 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003713
gman@chromium.org50c526d2011-08-10 05:19:44 +00003714 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003715 {
3716 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3717 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003718
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003719 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.comdf363722011-12-16 23:29:53 +00003720 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003721
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003722 // ANGLE-specific extensions
3723 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003724 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003725 {
3726 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3727 }
3728
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003729 if (supportsInstancing())
daniel@transgaming.comdce02fd2012-01-27 15:39:51 +00003730 {
3731 mExtensionString += "GL_ANGLE_instanced_arrays ";
3732 }
3733
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003734 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3735
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003736 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003737 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003738 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3739 }
3740 if (supportsDXT5Textures())
3741 {
3742 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003743 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003744
3745 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003746 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003747
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003748 // Other vendor-specific extensions
3749 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003750 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003751 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003752 }
3753
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003754 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3755 if (end != std::string::npos)
3756 {
3757 mExtensionString.resize(end+1);
3758 }
3759}
3760
3761const char *Context::getExtensionString() const
3762{
3763 return mExtensionString.c_str();
3764}
3765
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003766void Context::initRendererString()
3767{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003768 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003769
3770 mRendererString = "ANGLE (";
3771 mRendererString += identifier->Description;
3772 mRendererString += ")";
3773}
3774
3775const char *Context::getRendererString() const
3776{
3777 return mRendererString.c_str();
3778}
3779
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003780void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3781 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3782 GLbitfield mask)
3783{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003784 Framebuffer *readFramebuffer = getReadFramebuffer();
3785 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3786
3787 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3788 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3789 {
3790 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3791 }
3792
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003793 if (drawFramebuffer->getSamples() != 0)
3794 {
3795 return error(GL_INVALID_OPERATION);
3796 }
3797
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003798 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3799 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3800 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3801 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3802
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003803 RECT sourceRect;
3804 RECT destRect;
3805
3806 if (srcX0 < srcX1)
3807 {
3808 sourceRect.left = srcX0;
3809 sourceRect.right = srcX1;
3810 destRect.left = dstX0;
3811 destRect.right = dstX1;
3812 }
3813 else
3814 {
3815 sourceRect.left = srcX1;
3816 destRect.left = dstX1;
3817 sourceRect.right = srcX0;
3818 destRect.right = dstX0;
3819 }
3820
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003821 if (srcY0 < srcY1)
3822 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003823 sourceRect.top = readBufferHeight - srcY1;
3824 destRect.top = drawBufferHeight - dstY1;
3825 sourceRect.bottom = readBufferHeight - srcY0;
3826 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003827 }
3828 else
3829 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003830 sourceRect.top = readBufferHeight - srcY0;
3831 destRect.top = drawBufferHeight - dstY0;
3832 sourceRect.bottom = readBufferHeight - srcY1;
3833 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003834 }
3835
3836 RECT sourceScissoredRect = sourceRect;
3837 RECT destScissoredRect = destRect;
3838
3839 if (mState.scissorTest)
3840 {
3841 // Only write to parts of the destination framebuffer which pass the scissor test
3842 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3843 // rect will be checked against scissorY, rather than the bottom.
3844 if (destRect.left < mState.scissorX)
3845 {
3846 int xDiff = mState.scissorX - destRect.left;
3847 destScissoredRect.left = mState.scissorX;
3848 sourceScissoredRect.left += xDiff;
3849 }
3850
3851 if (destRect.right > mState.scissorX + mState.scissorWidth)
3852 {
3853 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3854 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3855 sourceScissoredRect.right -= xDiff;
3856 }
3857
3858 if (destRect.top < mState.scissorY)
3859 {
3860 int yDiff = mState.scissorY - destRect.top;
3861 destScissoredRect.top = mState.scissorY;
3862 sourceScissoredRect.top += yDiff;
3863 }
3864
3865 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3866 {
3867 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3868 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3869 sourceScissoredRect.bottom -= yDiff;
3870 }
3871 }
3872
3873 bool blitRenderTarget = false;
3874 bool blitDepthStencil = false;
3875
3876 RECT sourceTrimmedRect = sourceScissoredRect;
3877 RECT destTrimmedRect = destScissoredRect;
3878
3879 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3880 // the actual draw and read surfaces.
3881 if (sourceTrimmedRect.left < 0)
3882 {
3883 int xDiff = 0 - sourceTrimmedRect.left;
3884 sourceTrimmedRect.left = 0;
3885 destTrimmedRect.left += xDiff;
3886 }
3887
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003888 if (sourceTrimmedRect.right > readBufferWidth)
3889 {
3890 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3891 sourceTrimmedRect.right = readBufferWidth;
3892 destTrimmedRect.right -= xDiff;
3893 }
3894
3895 if (sourceTrimmedRect.top < 0)
3896 {
3897 int yDiff = 0 - sourceTrimmedRect.top;
3898 sourceTrimmedRect.top = 0;
3899 destTrimmedRect.top += yDiff;
3900 }
3901
3902 if (sourceTrimmedRect.bottom > readBufferHeight)
3903 {
3904 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3905 sourceTrimmedRect.bottom = readBufferHeight;
3906 destTrimmedRect.bottom -= yDiff;
3907 }
3908
3909 if (destTrimmedRect.left < 0)
3910 {
3911 int xDiff = 0 - destTrimmedRect.left;
3912 destTrimmedRect.left = 0;
3913 sourceTrimmedRect.left += xDiff;
3914 }
3915
3916 if (destTrimmedRect.right > drawBufferWidth)
3917 {
3918 int xDiff = destTrimmedRect.right - drawBufferWidth;
3919 destTrimmedRect.right = drawBufferWidth;
3920 sourceTrimmedRect.right -= xDiff;
3921 }
3922
3923 if (destTrimmedRect.top < 0)
3924 {
3925 int yDiff = 0 - destTrimmedRect.top;
3926 destTrimmedRect.top = 0;
3927 sourceTrimmedRect.top += yDiff;
3928 }
3929
3930 if (destTrimmedRect.bottom > drawBufferHeight)
3931 {
3932 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3933 destTrimmedRect.bottom = drawBufferHeight;
3934 sourceTrimmedRect.bottom -= yDiff;
3935 }
3936
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003937 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003938 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3939 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3940 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3941 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003942 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3943 {
3944 partialBufferCopy = true;
3945 }
3946
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003947 if (mask & GL_COLOR_BUFFER_BIT)
3948 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003949 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3950 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3951 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3952 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3953 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003954 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3955 {
3956 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3957 return error(GL_INVALID_OPERATION);
3958 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003959
3960 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3961 {
3962 return error(GL_INVALID_OPERATION);
3963 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003964
3965 blitRenderTarget = true;
3966
3967 }
3968
3969 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3970 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003971 Renderbuffer *readDSBuffer = NULL;
3972 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003973
3974 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3975 // both a depth and stencil buffer, it will be the same buffer.
3976
3977 if (mask & GL_DEPTH_BUFFER_BIT)
3978 {
3979 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3980 {
3981 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3982 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3983 {
3984 return error(GL_INVALID_OPERATION);
3985 }
3986
3987 blitDepthStencil = true;
3988 readDSBuffer = readFramebuffer->getDepthbuffer();
3989 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3990 }
3991 }
3992
3993 if (mask & GL_STENCIL_BUFFER_BIT)
3994 {
3995 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3996 {
3997 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3998 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3999 {
4000 return error(GL_INVALID_OPERATION);
4001 }
4002
4003 blitDepthStencil = true;
4004 readDSBuffer = readFramebuffer->getStencilbuffer();
4005 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4006 }
4007 }
4008
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004009 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004010 {
4011 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4012 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4013 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004014
daniel@transgaming.com97446d22010-08-24 19:20:54 +00004015 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4016 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004017 {
4018 return error(GL_INVALID_OPERATION);
4019 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004020 }
4021
4022 if (blitRenderTarget || blitDepthStencil)
4023 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004024 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004025
4026 if (blitRenderTarget)
4027 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00004028 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4029 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4030
4031 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4032 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4033
4034 readRenderTarget->Release();
4035 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004036
4037 if (FAILED(result))
4038 {
4039 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4040 return;
4041 }
4042 }
4043
4044 if (blitDepthStencil)
4045 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004046 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004047
4048 if (FAILED(result))
4049 {
4050 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4051 return;
4052 }
4053 }
4054 }
4055}
4056
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004057VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4058{
4059 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4060 {
4061 mVertexDeclCache[i].vertexDeclaration = NULL;
4062 mVertexDeclCache[i].lruCount = 0;
4063 }
4064}
4065
4066VertexDeclarationCache::~VertexDeclarationCache()
4067{
4068 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4069 {
4070 if (mVertexDeclCache[i].vertexDeclaration)
4071 {
4072 mVertexDeclCache[i].vertexDeclaration->Release();
4073 }
4074 }
4075}
4076
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004077GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004078{
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004079 *repeatDraw = 1;
4080
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004081 int indexedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004082 int instancedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004083
4084 if (instances > 0)
4085 {
4086 // Find an indexed attribute to be mapped to D3D stream 0
4087 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4088 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004089 if (attributes[i].active)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004090 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004091 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4092 {
4093 if (attributes[i].divisor == 0)
4094 {
4095 indexedAttribute = i;
4096 }
4097 }
4098 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4099 {
4100 if (attributes[i].divisor != 0)
4101 {
4102 instancedAttribute = i;
4103 }
4104 }
4105 else break; // Found both an indexed and instanced attribute
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004106 }
4107 }
4108
4109 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4110 {
4111 return GL_INVALID_OPERATION;
4112 }
4113 }
4114
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004115 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4116 D3DVERTEXELEMENT9 *element = &elements[0];
4117
4118 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4119 {
4120 if (attributes[i].active)
4121 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004122 int stream = i;
4123
4124 if (instances > 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004125 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004126 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4127 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004128 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004129 *repeatDraw = instances;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004130 }
4131 else
4132 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004133 if (i == indexedAttribute)
4134 {
4135 stream = 0;
4136 }
4137 else if (i == 0)
4138 {
4139 stream = indexedAttribute;
4140 }
4141
4142 UINT frequency = 1;
4143
4144 if (attributes[i].divisor == 0)
4145 {
4146 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4147 }
4148 else
4149 {
4150 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4151 }
4152
4153 device->SetStreamSourceFreq(stream, frequency);
4154 mInstancingEnabled = true;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004155 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004156 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004157
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004158 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4159 mAppliedVBs[stream].stride != attributes[i].stride ||
4160 mAppliedVBs[stream].offset != attributes[i].offset)
4161 {
4162 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4163 mAppliedVBs[stream].serial = attributes[i].serial;
4164 mAppliedVBs[stream].stride = attributes[i].stride;
4165 mAppliedVBs[stream].offset = attributes[i].offset;
4166 }
4167
4168 element->Stream = stream;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004169 element->Offset = 0;
4170 element->Type = attributes[i].type;
4171 element->Method = D3DDECLMETHOD_DEFAULT;
4172 element->Usage = D3DDECLUSAGE_TEXCOORD;
4173 element->UsageIndex = program->getSemanticIndex(i);
4174 element++;
4175 }
4176 }
4177
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004178 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004179 {
4180 if (mInstancingEnabled)
4181 {
4182 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4183 {
4184 device->SetStreamSourceFreq(i, 1);
4185 }
4186
4187 mInstancingEnabled = false;
4188 }
4189 }
4190
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004191 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4192 *(element++) = end;
4193
4194 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4195 {
4196 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4197 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4198 {
4199 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004200 if(entry->vertexDeclaration != mLastSetVDecl)
4201 {
4202 device->SetVertexDeclaration(entry->vertexDeclaration);
4203 mLastSetVDecl = entry->vertexDeclaration;
4204 }
4205
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004206 return GL_NO_ERROR;
4207 }
4208 }
4209
4210 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4211
4212 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4213 {
4214 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4215 {
4216 lastCache = &mVertexDeclCache[i];
4217 }
4218 }
4219
4220 if (lastCache->vertexDeclaration != NULL)
4221 {
4222 lastCache->vertexDeclaration->Release();
4223 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004224 // mLastSetVDecl is set to the replacement, so we don't have to worry
4225 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004226 }
4227
4228 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4229 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4230 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004231 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004232 lastCache->lruCount = ++mMaxLru;
4233
4234 return GL_NO_ERROR;
4235}
4236
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004237void VertexDeclarationCache::markStateDirty()
4238{
4239 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4240 {
4241 mAppliedVBs[i].serial = 0;
4242 }
4243
4244 mLastSetVDecl = NULL;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004245 mInstancingEnabled = true; // Forces it to be disabled when not used
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004246}
4247
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004248}
4249
4250extern "C"
4251{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004252gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004253{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004254 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004255}
4256
4257void glDestroyContext(gl::Context *context)
4258{
4259 delete context;
4260
4261 if (context == gl::getContext())
4262 {
4263 gl::makeCurrent(NULL, NULL, NULL);
4264 }
4265}
4266
4267void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4268{
4269 gl::makeCurrent(context, display, surface);
4270}
4271
4272gl::Context *glGetCurrentContext()
4273{
4274 return gl::getContext();
4275}
4276}