blob: 800ac22eb1053ad9a57396b5d3d7b714519dff09 [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.com11399d52012-04-28 00:35:14 +0000408void Context::markDxUniformsDirty()
409{
410 mDxUniformsDirty = true;
411}
412
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000413void Context::markContextLost()
414{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000415 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
416 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000417 mContextLost = true;
418}
419
420bool Context::isContextLost()
421{
422 return mContextLost;
423}
424
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425void Context::setClearColor(float red, float green, float blue, float alpha)
426{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000427 mState.colorClearValue.red = red;
428 mState.colorClearValue.green = green;
429 mState.colorClearValue.blue = blue;
430 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431}
432
433void Context::setClearDepth(float depth)
434{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436}
437
438void Context::setClearStencil(int stencil)
439{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000440 mState.stencilClearValue = stencil;
441}
442
443void Context::setCullFace(bool enabled)
444{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000445 if (mState.cullFace != enabled)
446 {
447 mState.cullFace = enabled;
448 mCullStateDirty = true;
449 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000450}
451
452bool Context::isCullFaceEnabled() const
453{
454 return mState.cullFace;
455}
456
457void Context::setCullMode(GLenum mode)
458{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000459 if (mState.cullMode != mode)
460 {
461 mState.cullMode = mode;
462 mCullStateDirty = true;
463 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000464}
465
466void Context::setFrontFace(GLenum front)
467{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000468 if (mState.frontFace != front)
469 {
470 mState.frontFace = front;
471 mFrontFaceDirty = true;
472 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000473}
474
475void Context::setDepthTest(bool enabled)
476{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000477 if (mState.depthTest != enabled)
478 {
479 mState.depthTest = enabled;
480 mDepthStateDirty = true;
481 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000482}
483
484bool Context::isDepthTestEnabled() const
485{
486 return mState.depthTest;
487}
488
489void Context::setDepthFunc(GLenum depthFunc)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.depthFunc != depthFunc)
492 {
493 mState.depthFunc = depthFunc;
494 mDepthStateDirty = true;
495 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000496}
497
498void Context::setDepthRange(float zNear, float zFar)
499{
500 mState.zNear = zNear;
501 mState.zFar = zFar;
502}
503
504void Context::setBlend(bool enabled)
505{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000506 if (mState.blend != enabled)
507 {
508 mState.blend = enabled;
509 mBlendStateDirty = true;
510 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000511}
512
513bool Context::isBlendEnabled() const
514{
515 return mState.blend;
516}
517
518void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
519{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000520 if (mState.sourceBlendRGB != sourceRGB ||
521 mState.sourceBlendAlpha != sourceAlpha ||
522 mState.destBlendRGB != destRGB ||
523 mState.destBlendAlpha != destAlpha)
524 {
525 mState.sourceBlendRGB = sourceRGB;
526 mState.destBlendRGB = destRGB;
527 mState.sourceBlendAlpha = sourceAlpha;
528 mState.destBlendAlpha = destAlpha;
529 mBlendStateDirty = true;
530 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000531}
532
533void Context::setBlendColor(float red, float green, float blue, float alpha)
534{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000535 if (mState.blendColor.red != red ||
536 mState.blendColor.green != green ||
537 mState.blendColor.blue != blue ||
538 mState.blendColor.alpha != alpha)
539 {
540 mState.blendColor.red = red;
541 mState.blendColor.green = green;
542 mState.blendColor.blue = blue;
543 mState.blendColor.alpha = alpha;
544 mBlendStateDirty = true;
545 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000546}
547
548void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
549{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000550 if (mState.blendEquationRGB != rgbEquation ||
551 mState.blendEquationAlpha != alphaEquation)
552 {
553 mState.blendEquationRGB = rgbEquation;
554 mState.blendEquationAlpha = alphaEquation;
555 mBlendStateDirty = true;
556 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000557}
558
559void Context::setStencilTest(bool enabled)
560{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000561 if (mState.stencilTest != enabled)
562 {
563 mState.stencilTest = enabled;
564 mStencilStateDirty = true;
565 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000566}
567
568bool Context::isStencilTestEnabled() const
569{
570 return mState.stencilTest;
571}
572
573void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
574{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000575 if (mState.stencilFunc != stencilFunc ||
576 mState.stencilRef != stencilRef ||
577 mState.stencilMask != stencilMask)
578 {
579 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000580 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000581 mState.stencilMask = stencilMask;
582 mStencilStateDirty = true;
583 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000584}
585
586void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
587{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000588 if (mState.stencilBackFunc != stencilBackFunc ||
589 mState.stencilBackRef != stencilBackRef ||
590 mState.stencilBackMask != stencilBackMask)
591 {
592 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000593 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000594 mState.stencilBackMask = stencilBackMask;
595 mStencilStateDirty = true;
596 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000597}
598
599void Context::setStencilWritemask(GLuint stencilWritemask)
600{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000601 if (mState.stencilWritemask != stencilWritemask)
602 {
603 mState.stencilWritemask = stencilWritemask;
604 mStencilStateDirty = true;
605 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000606}
607
608void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
609{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000610 if (mState.stencilBackWritemask != stencilBackWritemask)
611 {
612 mState.stencilBackWritemask = stencilBackWritemask;
613 mStencilStateDirty = true;
614 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000615}
616
617void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
618{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000619 if (mState.stencilFail != stencilFail ||
620 mState.stencilPassDepthFail != stencilPassDepthFail ||
621 mState.stencilPassDepthPass != stencilPassDepthPass)
622 {
623 mState.stencilFail = stencilFail;
624 mState.stencilPassDepthFail = stencilPassDepthFail;
625 mState.stencilPassDepthPass = stencilPassDepthPass;
626 mStencilStateDirty = true;
627 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000628}
629
630void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
631{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000632 if (mState.stencilBackFail != stencilBackFail ||
633 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
634 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
635 {
636 mState.stencilBackFail = stencilBackFail;
637 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
638 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
639 mStencilStateDirty = true;
640 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000641}
642
643void Context::setPolygonOffsetFill(bool enabled)
644{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000645 if (mState.polygonOffsetFill != enabled)
646 {
647 mState.polygonOffsetFill = enabled;
648 mPolygonOffsetStateDirty = true;
649 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000650}
651
652bool Context::isPolygonOffsetFillEnabled() const
653{
654 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000655
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000656}
657
658void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
659{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000660 if (mState.polygonOffsetFactor != factor ||
661 mState.polygonOffsetUnits != units)
662 {
663 mState.polygonOffsetFactor = factor;
664 mState.polygonOffsetUnits = units;
665 mPolygonOffsetStateDirty = true;
666 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000667}
668
669void Context::setSampleAlphaToCoverage(bool enabled)
670{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000671 if (mState.sampleAlphaToCoverage != enabled)
672 {
673 mState.sampleAlphaToCoverage = enabled;
674 mSampleStateDirty = true;
675 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000676}
677
678bool Context::isSampleAlphaToCoverageEnabled() const
679{
680 return mState.sampleAlphaToCoverage;
681}
682
683void Context::setSampleCoverage(bool enabled)
684{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000685 if (mState.sampleCoverage != enabled)
686 {
687 mState.sampleCoverage = enabled;
688 mSampleStateDirty = true;
689 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000690}
691
692bool Context::isSampleCoverageEnabled() const
693{
694 return mState.sampleCoverage;
695}
696
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000697void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000698{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000699 if (mState.sampleCoverageValue != value ||
700 mState.sampleCoverageInvert != invert)
701 {
702 mState.sampleCoverageValue = value;
703 mState.sampleCoverageInvert = invert;
704 mSampleStateDirty = true;
705 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000706}
707
708void Context::setScissorTest(bool enabled)
709{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000710 if (mState.scissorTest != enabled)
711 {
712 mState.scissorTest = enabled;
713 mScissorStateDirty = true;
714 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000715}
716
717bool Context::isScissorTestEnabled() const
718{
719 return mState.scissorTest;
720}
721
722void Context::setDither(bool enabled)
723{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000724 if (mState.dither != enabled)
725 {
726 mState.dither = enabled;
727 mDitherStateDirty = true;
728 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000729}
730
731bool Context::isDitherEnabled() const
732{
733 return mState.dither;
734}
735
736void Context::setLineWidth(GLfloat width)
737{
738 mState.lineWidth = width;
739}
740
741void Context::setGenerateMipmapHint(GLenum hint)
742{
743 mState.generateMipmapHint = hint;
744}
745
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000746void Context::setFragmentShaderDerivativeHint(GLenum hint)
747{
748 mState.fragmentShaderDerivativeHint = hint;
749 // TODO: Propagate the hint to shader translator so we can write
750 // ddx, ddx_coarse, or ddx_fine depending on the hint.
751 // Ignore for now. It is valid for implementations to ignore hint.
752}
753
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000754void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
755{
756 mState.viewportX = x;
757 mState.viewportY = y;
758 mState.viewportWidth = width;
759 mState.viewportHeight = height;
760}
761
762void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
763{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000764 if (mState.scissorX != x || mState.scissorY != y ||
765 mState.scissorWidth != width || mState.scissorHeight != height)
766 {
767 mState.scissorX = x;
768 mState.scissorY = y;
769 mState.scissorWidth = width;
770 mState.scissorHeight = height;
771 mScissorStateDirty = true;
772 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000773}
774
775void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
776{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000777 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
778 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
779 {
780 mState.colorMaskRed = red;
781 mState.colorMaskGreen = green;
782 mState.colorMaskBlue = blue;
783 mState.colorMaskAlpha = alpha;
784 mMaskStateDirty = true;
785 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000786}
787
788void Context::setDepthMask(bool mask)
789{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000790 if (mState.depthMask != mask)
791 {
792 mState.depthMask = mask;
793 mMaskStateDirty = true;
794 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000795}
796
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000797void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000798{
799 mState.activeSampler = active;
800}
801
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000802GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000803{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000804 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000805}
806
807GLuint Context::getDrawFramebufferHandle() const
808{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000809 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000810}
811
812GLuint Context::getRenderbufferHandle() const
813{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000814 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000815}
816
817GLuint Context::getArrayBufferHandle() const
818{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000819 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000820}
821
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000822GLuint Context::getActiveQuery(GLenum target) const
823{
824 Query *queryObject = NULL;
825
826 switch (target)
827 {
828 case GL_ANY_SAMPLES_PASSED_EXT:
829 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
830 break;
831 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
832 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
833 break;
834 default:
835 ASSERT(false);
836 }
837
838 if (queryObject)
839 {
840 return queryObject->id();
841 }
842 else
843 {
844 return 0;
845 }
846}
847
daniel@transgaming.com83921382011-01-08 05:46:00 +0000848void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000849{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000850 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000851}
852
daniel@transgaming.com83921382011-01-08 05:46:00 +0000853const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000854{
855 return mState.vertexAttribute[attribNum];
856}
857
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000858void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000859 GLsizei stride, const void *pointer)
860{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000861 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000862 mState.vertexAttribute[attribNum].mSize = size;
863 mState.vertexAttribute[attribNum].mType = type;
864 mState.vertexAttribute[attribNum].mNormalized = normalized;
865 mState.vertexAttribute[attribNum].mStride = stride;
866 mState.vertexAttribute[attribNum].mPointer = pointer;
867}
868
869const void *Context::getVertexAttribPointer(unsigned int attribNum) const
870{
871 return mState.vertexAttribute[attribNum].mPointer;
872}
873
daniel@transgaming.com83921382011-01-08 05:46:00 +0000874const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000875{
876 return mState.vertexAttribute;
877}
878
879void Context::setPackAlignment(GLint alignment)
880{
881 mState.packAlignment = alignment;
882}
883
884GLint Context::getPackAlignment() const
885{
886 return mState.packAlignment;
887}
888
889void Context::setUnpackAlignment(GLint alignment)
890{
891 mState.unpackAlignment = alignment;
892}
893
894GLint Context::getUnpackAlignment() const
895{
896 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
898
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000899void Context::setPackReverseRowOrder(bool reverseRowOrder)
900{
901 mState.packReverseRowOrder = reverseRowOrder;
902}
903
904bool Context::getPackReverseRowOrder() const
905{
906 return mState.packReverseRowOrder;
907}
908
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909GLuint Context::createBuffer()
910{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000911 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912}
913
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914GLuint Context::createProgram()
915{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000916 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917}
918
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000919GLuint Context::createShader(GLenum type)
920{
921 return mResourceManager->createShader(type);
922}
923
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924GLuint Context::createTexture()
925{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000926 return mResourceManager->createTexture();
927}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000929GLuint Context::createRenderbuffer()
930{
931 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932}
933
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000934// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935GLuint Context::createFramebuffer()
936{
benvanik@google.com1a233342011-04-28 19:44:39 +0000937 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000938
939 mFramebufferMap[handle] = NULL;
940
941 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942}
943
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000944GLuint Context::createFence()
945{
benvanik@google.com1a233342011-04-28 19:44:39 +0000946 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000947
apatrick@chromium.org563c0a52012-03-23 21:18:42 +0000948 mFenceMap[handle] = new Fence(mDisplay);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000949
950 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000951}
952
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000953// Returns an unused query name
954GLuint Context::createQuery()
955{
956 GLuint handle = mQueryHandleAllocator.allocate();
957
958 mQueryMap[handle] = NULL;
959
960 return handle;
961}
962
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963void Context::deleteBuffer(GLuint buffer)
964{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000965 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966 {
967 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000969
970 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971}
972
973void Context::deleteShader(GLuint shader)
974{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000975 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976}
977
978void Context::deleteProgram(GLuint program)
979{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000980 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000981 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982}
983
984void Context::deleteTexture(GLuint texture)
985{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000986 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987 {
988 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000990
991 mResourceManager->deleteTexture(texture);
992}
993
994void Context::deleteRenderbuffer(GLuint renderbuffer)
995{
996 if (mResourceManager->getRenderbuffer(renderbuffer))
997 {
998 detachRenderbuffer(renderbuffer);
999 }
1000
1001 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002}
1003
1004void Context::deleteFramebuffer(GLuint framebuffer)
1005{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001006 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
1007
1008 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009 {
1010 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +00001011
benvanik@google.com1a233342011-04-28 19:44:39 +00001012 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001013 delete framebufferObject->second;
1014 mFramebufferMap.erase(framebufferObject);
1015 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016}
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001017
1018void Context::deleteFence(GLuint fence)
1019{
1020 FenceMap::iterator fenceObject = mFenceMap.find(fence);
1021
1022 if (fenceObject != mFenceMap.end())
1023 {
benvanik@google.com1a233342011-04-28 19:44:39 +00001024 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001025 delete fenceObject->second;
1026 mFenceMap.erase(fenceObject);
1027 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001028}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001030void Context::deleteQuery(GLuint query)
1031{
1032 QueryMap::iterator queryObject = mQueryMap.find(query);
1033 if (queryObject != mQueryMap.end())
1034 {
1035 mQueryHandleAllocator.release(queryObject->first);
1036 if (queryObject->second)
1037 {
1038 queryObject->second->release();
1039 }
1040 mQueryMap.erase(queryObject);
1041 }
1042}
1043
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001044Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001046 return mResourceManager->getBuffer(handle);
1047}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001049Shader *Context::getShader(GLuint handle)
1050{
1051 return mResourceManager->getShader(handle);
1052}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001053
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001054Program *Context::getProgram(GLuint handle)
1055{
1056 return mResourceManager->getProgram(handle);
1057}
1058
1059Texture *Context::getTexture(GLuint handle)
1060{
1061 return mResourceManager->getTexture(handle);
1062}
1063
1064Renderbuffer *Context::getRenderbuffer(GLuint handle)
1065{
1066 return mResourceManager->getRenderbuffer(handle);
1067}
1068
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001069Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001070{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001071 return getFramebuffer(mState.readFramebuffer);
1072}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001073
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001074Framebuffer *Context::getDrawFramebuffer()
1075{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001076 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
1079void Context::bindArrayBuffer(unsigned int buffer)
1080{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001081 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001083 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084}
1085
1086void Context::bindElementArrayBuffer(unsigned int buffer)
1087{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001088 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001090 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091}
1092
1093void Context::bindTexture2D(GLuint texture)
1094{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001095 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001097 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098}
1099
1100void Context::bindTextureCubeMap(GLuint texture)
1101{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001102 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001104 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105}
1106
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001107void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001109 if (!getFramebuffer(framebuffer))
1110 {
1111 mFramebufferMap[framebuffer] = new Framebuffer();
1112 }
1113
1114 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001115}
1116
1117void Context::bindDrawFramebuffer(GLuint framebuffer)
1118{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001119 if (!getFramebuffer(framebuffer))
1120 {
1121 mFramebufferMap[framebuffer] = new Framebuffer();
1122 }
1123
1124 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001125
1126 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001127}
1128
1129void Context::bindRenderbuffer(GLuint renderbuffer)
1130{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001131 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1132
1133 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134}
1135
1136void Context::useProgram(GLuint program)
1137{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001138 GLuint priorProgram = mState.currentProgram;
1139 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001140
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001141 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001142 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001143 Program *newProgram = mResourceManager->getProgram(program);
1144 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001145 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001146 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001147
1148 if (newProgram)
1149 {
1150 newProgram->addRef();
1151 }
1152
1153 if (oldProgram)
1154 {
1155 oldProgram->release();
1156 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001158}
1159
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001160void Context::beginQuery(GLenum target, GLuint query)
1161{
1162 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1163 // of zero, if the active query object name for <target> is non-zero (for the
1164 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1165 // the active query for either target is non-zero), if <id> is the name of an
1166 // existing query object whose type does not match <target>, or if <id> is the
1167 // active query object name for any query type, the error INVALID_OPERATION is
1168 // generated.
1169
1170 // Ensure no other queries are active
1171 // NOTE: If other queries than occlusion are supported, we will need to check
1172 // separately that:
1173 // a) The query ID passed is not the current active query for any target/type
1174 // b) There are no active queries for the requested target (and in the case
1175 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1176 // no query may be active for either if glBeginQuery targets either.
1177 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1178 {
1179 if (mState.activeQuery[i].get() != NULL)
1180 {
1181 return error(GL_INVALID_OPERATION);
1182 }
1183 }
1184
1185 QueryType qType;
1186 switch (target)
1187 {
1188 case GL_ANY_SAMPLES_PASSED_EXT:
1189 qType = QUERY_ANY_SAMPLES_PASSED;
1190 break;
1191 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1192 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1193 break;
1194 default:
1195 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001196 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001197 }
1198
1199 Query *queryObject = getQuery(query, true, target);
1200
1201 // check that name was obtained with glGenQueries
1202 if (!queryObject)
1203 {
1204 return error(GL_INVALID_OPERATION);
1205 }
1206
1207 // check for type mismatch
1208 if (queryObject->getType() != target)
1209 {
1210 return error(GL_INVALID_OPERATION);
1211 }
1212
1213 // set query as active for specified target
1214 mState.activeQuery[qType].set(queryObject);
1215
1216 // begin query
1217 queryObject->begin();
1218}
1219
1220void Context::endQuery(GLenum target)
1221{
1222 QueryType qType;
1223
1224 switch (target)
1225 {
1226 case GL_ANY_SAMPLES_PASSED_EXT:
1227 qType = QUERY_ANY_SAMPLES_PASSED;
1228 break;
1229 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1230 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1231 break;
1232 default:
1233 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001234 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001235 }
1236
1237 Query *queryObject = mState.activeQuery[qType].get();
1238
1239 if (queryObject == NULL)
1240 {
1241 return error(GL_INVALID_OPERATION);
1242 }
1243
1244 queryObject->end();
1245
1246 mState.activeQuery[qType].set(NULL);
1247}
1248
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001249void Context::setFramebufferZero(Framebuffer *buffer)
1250{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001251 delete mFramebufferMap[0];
1252 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001253 if (mState.drawFramebuffer == 0)
1254 {
1255 mBoundDrawFramebuffer = buffer;
1256 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001257}
1258
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001259void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001260{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001261 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1262 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001263}
1264
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001265Framebuffer *Context::getFramebuffer(unsigned int handle)
1266{
1267 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1268
1269 if (framebuffer == mFramebufferMap.end())
1270 {
1271 return NULL;
1272 }
1273 else
1274 {
1275 return framebuffer->second;
1276 }
1277}
1278
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001279Fence *Context::getFence(unsigned int handle)
1280{
1281 FenceMap::iterator fence = mFenceMap.find(handle);
1282
1283 if (fence == mFenceMap.end())
1284 {
1285 return NULL;
1286 }
1287 else
1288 {
1289 return fence->second;
1290 }
1291}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001292
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001293Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1294{
1295 QueryMap::iterator query = mQueryMap.find(handle);
1296
1297 if (query == mQueryMap.end())
1298 {
1299 return NULL;
1300 }
1301 else
1302 {
1303 if (!query->second && create)
1304 {
1305 query->second = new Query(handle, type);
1306 query->second->addRef();
1307 }
1308 return query->second;
1309 }
1310}
1311
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001312Buffer *Context::getArrayBuffer()
1313{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001314 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001315}
1316
1317Buffer *Context::getElementArrayBuffer()
1318{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001319 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001320}
1321
1322Program *Context::getCurrentProgram()
1323{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001324 if (!mCachedCurrentProgram)
1325 {
1326 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1327 }
1328 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001329}
1330
1331Texture2D *Context::getTexture2D()
1332{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001333 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334}
1335
1336TextureCubeMap *Context::getTextureCubeMap()
1337{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001338 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001339}
1340
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001341Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001342{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001343 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001344
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001345 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001346 {
1347 switch (type)
1348 {
1349 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001350 case TEXTURE_2D: return mTexture2DZero.get();
1351 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001352 }
1353 }
1354
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001355 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001356}
1357
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001358bool Context::getBooleanv(GLenum pname, GLboolean *params)
1359{
1360 switch (pname)
1361 {
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001362 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1363 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1364 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001365 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001366 params[0] = mState.colorMaskRed;
1367 params[1] = mState.colorMaskGreen;
1368 params[2] = mState.colorMaskBlue;
1369 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001370 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001371 case GL_CULL_FACE: *params = mState.cullFace; break;
1372 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1373 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1374 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1375 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1376 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1377 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1378 case GL_BLEND: *params = mState.blend; break;
1379 case GL_DITHER: *params = mState.dither; break;
1380 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001381 default:
1382 return false;
1383 }
1384
1385 return true;
1386}
1387
1388bool Context::getFloatv(GLenum pname, GLfloat *params)
1389{
1390 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1391 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1392 // GetIntegerv as its native query function. As it would require conversion in any
1393 // case, this should make no difference to the calling application.
1394 switch (pname)
1395 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001396 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1397 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1398 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1399 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1400 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001401 case GL_ALIASED_LINE_WIDTH_RANGE:
1402 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1403 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1404 break;
1405 case GL_ALIASED_POINT_SIZE_RANGE:
1406 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001407 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 +00001408 break;
1409 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001410 params[0] = mState.zNear;
1411 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001412 break;
1413 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001414 params[0] = mState.colorClearValue.red;
1415 params[1] = mState.colorClearValue.green;
1416 params[2] = mState.colorClearValue.blue;
1417 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001418 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001419 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001420 params[0] = mState.blendColor.red;
1421 params[1] = mState.blendColor.green;
1422 params[2] = mState.blendColor.blue;
1423 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001424 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001425 default:
1426 return false;
1427 }
1428
1429 return true;
1430}
1431
1432bool Context::getIntegerv(GLenum pname, GLint *params)
1433{
1434 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1435 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1436 // GetIntegerv as its native query function. As it would require conversion in any
1437 // case, this should make no difference to the calling application. You may find it in
1438 // Context::getFloatv.
1439 switch (pname)
1440 {
1441 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1442 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001443 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001444 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1445 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001446 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001447 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001448 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001449 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001450 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001451 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1452 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001453 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1454 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1455 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001456 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001457 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1458 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001459 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001460 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1461 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001462 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001463 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1464 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1465 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1466 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1467 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1468 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1469 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1470 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1471 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1472 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1473 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1474 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1475 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1476 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1477 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1478 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1479 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1480 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1481 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1482 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1483 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1484 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1485 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1486 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001487 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1488 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001489 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001490 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001491 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001492 case GL_MAX_SAMPLES_ANGLE:
1493 {
1494 GLsizei maxSamples = getMaxSupportedSamples();
1495 if (maxSamples != 0)
1496 {
1497 *params = maxSamples;
1498 }
1499 else
1500 {
1501 return false;
1502 }
1503
1504 break;
1505 }
1506 case GL_SAMPLE_BUFFERS:
1507 case GL_SAMPLES:
1508 {
1509 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1510 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1511 {
1512 switch (pname)
1513 {
1514 case GL_SAMPLE_BUFFERS:
1515 if (framebuffer->getSamples() != 0)
1516 {
1517 *params = 1;
1518 }
1519 else
1520 {
1521 *params = 0;
1522 }
1523 break;
1524 case GL_SAMPLES:
1525 *params = framebuffer->getSamples();
1526 break;
1527 }
1528 }
1529 else
1530 {
1531 *params = 0;
1532 }
1533 }
1534 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001535 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1536 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1537 case GL_MAX_VIEWPORT_DIMS:
1538 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001539 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001540 params[0] = maxDimension;
1541 params[1] = maxDimension;
1542 }
1543 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001544 case GL_COMPRESSED_TEXTURE_FORMATS:
1545 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001546 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001547 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001548 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1549 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1550 }
1551 if (supportsDXT3Textures())
1552 {
1553 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1554 }
1555 if (supportsDXT5Textures())
1556 {
1557 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001558 }
1559 }
1560 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001561 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001562 params[0] = mState.viewportX;
1563 params[1] = mState.viewportY;
1564 params[2] = mState.viewportWidth;
1565 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001566 break;
1567 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001568 params[0] = mState.scissorX;
1569 params[1] = mState.scissorY;
1570 params[2] = mState.scissorWidth;
1571 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001572 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001573 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1574 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001575 case GL_RED_BITS:
1576 case GL_GREEN_BITS:
1577 case GL_BLUE_BITS:
1578 case GL_ALPHA_BITS:
1579 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001580 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001581 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001582
1583 if (colorbuffer)
1584 {
1585 switch (pname)
1586 {
1587 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1588 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1589 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1590 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1591 }
1592 }
1593 else
1594 {
1595 *params = 0;
1596 }
1597 }
1598 break;
1599 case GL_DEPTH_BITS:
1600 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001601 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001602 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001603
1604 if (depthbuffer)
1605 {
1606 *params = depthbuffer->getDepthSize();
1607 }
1608 else
1609 {
1610 *params = 0;
1611 }
1612 }
1613 break;
1614 case GL_STENCIL_BITS:
1615 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001616 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001617 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001618
1619 if (stencilbuffer)
1620 {
1621 *params = stencilbuffer->getStencilSize();
1622 }
1623 else
1624 {
1625 *params = 0;
1626 }
1627 }
1628 break;
1629 case GL_TEXTURE_BINDING_2D:
1630 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001631 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001632 {
1633 error(GL_INVALID_OPERATION);
1634 return false;
1635 }
1636
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001637 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001638 }
1639 break;
1640 case GL_TEXTURE_BINDING_CUBE_MAP:
1641 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001642 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001643 {
1644 error(GL_INVALID_OPERATION);
1645 return false;
1646 }
1647
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001648 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001649 }
1650 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001651 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1652 *params = mResetStrategy;
1653 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001654 default:
1655 return false;
1656 }
1657
1658 return true;
1659}
1660
1661bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1662{
1663 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1664 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1665 // to the fact that it is stored internally as a float, and so would require conversion
1666 // if returned from Context::getIntegerv. Since this conversion is already implemented
1667 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1668 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1669 // application.
1670 switch (pname)
1671 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001672 case GL_COMPRESSED_TEXTURE_FORMATS:
1673 {
1674 *type = GL_INT;
1675 *numParams = mNumCompressedTextureFormats;
1676 }
1677 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001678 case GL_SHADER_BINARY_FORMATS:
1679 {
1680 *type = GL_INT;
1681 *numParams = 0;
1682 }
1683 break;
1684 case GL_MAX_VERTEX_ATTRIBS:
1685 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1686 case GL_MAX_VARYING_VECTORS:
1687 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1688 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1689 case GL_MAX_TEXTURE_IMAGE_UNITS:
1690 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1691 case GL_MAX_RENDERBUFFER_SIZE:
1692 case GL_NUM_SHADER_BINARY_FORMATS:
1693 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1694 case GL_ARRAY_BUFFER_BINDING:
1695 case GL_FRAMEBUFFER_BINDING:
1696 case GL_RENDERBUFFER_BINDING:
1697 case GL_CURRENT_PROGRAM:
1698 case GL_PACK_ALIGNMENT:
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001699 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001700 case GL_UNPACK_ALIGNMENT:
1701 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001702 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001703 case GL_RED_BITS:
1704 case GL_GREEN_BITS:
1705 case GL_BLUE_BITS:
1706 case GL_ALPHA_BITS:
1707 case GL_DEPTH_BITS:
1708 case GL_STENCIL_BITS:
1709 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1710 case GL_CULL_FACE_MODE:
1711 case GL_FRONT_FACE:
1712 case GL_ACTIVE_TEXTURE:
1713 case GL_STENCIL_FUNC:
1714 case GL_STENCIL_VALUE_MASK:
1715 case GL_STENCIL_REF:
1716 case GL_STENCIL_FAIL:
1717 case GL_STENCIL_PASS_DEPTH_FAIL:
1718 case GL_STENCIL_PASS_DEPTH_PASS:
1719 case GL_STENCIL_BACK_FUNC:
1720 case GL_STENCIL_BACK_VALUE_MASK:
1721 case GL_STENCIL_BACK_REF:
1722 case GL_STENCIL_BACK_FAIL:
1723 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1724 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1725 case GL_DEPTH_FUNC:
1726 case GL_BLEND_SRC_RGB:
1727 case GL_BLEND_SRC_ALPHA:
1728 case GL_BLEND_DST_RGB:
1729 case GL_BLEND_DST_ALPHA:
1730 case GL_BLEND_EQUATION_RGB:
1731 case GL_BLEND_EQUATION_ALPHA:
1732 case GL_STENCIL_WRITEMASK:
1733 case GL_STENCIL_BACK_WRITEMASK:
1734 case GL_STENCIL_CLEAR_VALUE:
1735 case GL_SUBPIXEL_BITS:
1736 case GL_MAX_TEXTURE_SIZE:
1737 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1738 case GL_SAMPLE_BUFFERS:
1739 case GL_SAMPLES:
1740 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1741 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1742 case GL_TEXTURE_BINDING_2D:
1743 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001744 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001745 {
1746 *type = GL_INT;
1747 *numParams = 1;
1748 }
1749 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001750 case GL_MAX_SAMPLES_ANGLE:
1751 {
1752 if (getMaxSupportedSamples() != 0)
1753 {
1754 *type = GL_INT;
1755 *numParams = 1;
1756 }
1757 else
1758 {
1759 return false;
1760 }
1761 }
1762 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001763 case GL_MAX_VIEWPORT_DIMS:
1764 {
1765 *type = GL_INT;
1766 *numParams = 2;
1767 }
1768 break;
1769 case GL_VIEWPORT:
1770 case GL_SCISSOR_BOX:
1771 {
1772 *type = GL_INT;
1773 *numParams = 4;
1774 }
1775 break;
1776 case GL_SHADER_COMPILER:
1777 case GL_SAMPLE_COVERAGE_INVERT:
1778 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001779 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1780 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1781 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1782 case GL_SAMPLE_COVERAGE:
1783 case GL_SCISSOR_TEST:
1784 case GL_STENCIL_TEST:
1785 case GL_DEPTH_TEST:
1786 case GL_BLEND:
1787 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001788 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001789 {
1790 *type = GL_BOOL;
1791 *numParams = 1;
1792 }
1793 break;
1794 case GL_COLOR_WRITEMASK:
1795 {
1796 *type = GL_BOOL;
1797 *numParams = 4;
1798 }
1799 break;
1800 case GL_POLYGON_OFFSET_FACTOR:
1801 case GL_POLYGON_OFFSET_UNITS:
1802 case GL_SAMPLE_COVERAGE_VALUE:
1803 case GL_DEPTH_CLEAR_VALUE:
1804 case GL_LINE_WIDTH:
1805 {
1806 *type = GL_FLOAT;
1807 *numParams = 1;
1808 }
1809 break;
1810 case GL_ALIASED_LINE_WIDTH_RANGE:
1811 case GL_ALIASED_POINT_SIZE_RANGE:
1812 case GL_DEPTH_RANGE:
1813 {
1814 *type = GL_FLOAT;
1815 *numParams = 2;
1816 }
1817 break;
1818 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001819 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001820 {
1821 *type = GL_FLOAT;
1822 *numParams = 4;
1823 }
1824 break;
1825 default:
1826 return false;
1827 }
1828
1829 return true;
1830}
1831
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832// Applies the render target surface, depth stencil surface, viewport rectangle and
1833// scissor rectangle to the Direct3D 9 device
1834bool Context::applyRenderTarget(bool ignoreViewport)
1835{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001836 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837
1838 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1839 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001840 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841 }
1842
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001843 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001844 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1845 if (renderTargetSerial != mAppliedRenderTargetSerial)
1846 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001847 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001848 if (!renderTarget)
1849 {
1850 return false; // Context must be lost
1851 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001852 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001853 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001854 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 +00001855 renderTargetChanged = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001856 renderTarget->Release();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001857 }
1858
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001859 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001860 unsigned int depthbufferSerial = 0;
1861 unsigned int stencilbufferSerial = 0;
1862 if (framebufferObject->getDepthbufferType() != GL_NONE)
1863 {
1864 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001865 if (!depthStencil)
1866 {
1867 ERR("Depth stencil pointer unexpectedly null.");
1868 return false;
1869 }
1870
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001871 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1872 }
1873 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1874 {
1875 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001876 if (!depthStencil)
1877 {
1878 ERR("Depth stencil pointer unexpectedly null.");
1879 return false;
1880 }
1881
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001882 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1883 }
1884
1885 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001886 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001887 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001888 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001889 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001890 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001891 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001892 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001895 if (!mRenderTargetDescInitialized || renderTargetChanged)
1896 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001897 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001898 if (!renderTarget)
1899 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001900 return false; // Context must be lost
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001901 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001902 renderTarget->GetDesc(&mRenderTargetDesc);
1903 mRenderTargetDescInitialized = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001904 renderTarget->Release();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001905 }
1906
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001909 float zNear = clamp01(mState.zNear);
1910 float zFar = clamp01(mState.zFar);
1911
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912 if (ignoreViewport)
1913 {
1914 viewport.X = 0;
1915 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001916 viewport.Width = mRenderTargetDesc.Width;
1917 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 viewport.MinZ = 0.0f;
1919 viewport.MaxZ = 1.0f;
1920 }
1921 else
1922 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001923 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1924 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1925 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1926 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1927 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 +00001928 viewport.MinZ = zNear;
1929 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001930 }
1931
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001932 if (viewport.Width <= 0 || viewport.Height <= 0)
1933 {
1934 return false; // Nothing to render
1935 }
1936
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001937 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001938 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001939 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001940 mSetViewport = viewport;
1941 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001942 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001943 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001945 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001947 if (mState.scissorTest)
1948 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001949 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1950 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1951 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1952 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1953 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001954 mDevice->SetScissorRect(&rect);
1955 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001956 }
1957 else
1958 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001959 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001960 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001961
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001962 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001963 }
1964
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001965 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001966 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967 Program *programObject = getCurrentProgram();
1968
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001969 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001970 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001971 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001972
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001973 // These values are used for computing gl_FragCoord in Program::linkVaryings(). The approach depends on Shader Model 3.0 support.
1974 GLint coord = programObject->getDxCoordLocation();
1975 float h = mSupportsShaderModel3 ? mRenderTargetDesc.Height : mState.viewportHeight / 2.0f;
1976 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, h,
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001977 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1978 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001979 programObject->setUniform4fv(coord, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001980
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001981 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001982 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001983 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001984
daniel@transgaming.com31754962010-11-28 02:02:52 +00001985 GLint depthRange = programObject->getDxDepthRangeLocation();
1986 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1987 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001988 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989 }
1990
1991 return true;
1992}
1993
1994// 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 +00001995void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001996{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001997 Program *programObject = getCurrentProgram();
1998
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001999 Framebuffer *framebufferObject = getDrawFramebuffer();
2000
2001 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
2002
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002003 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002004 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002005 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002007 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002008 GLint alwaysFront = !isTriangleMode(drawMode);
2009 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
2010
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002011 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002012 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2013 // Apparently some ATI cards have a bug where a draw with a zero color
2014 // write mask can cause later draws to have incorrect results. Instead,
2015 // set a nonzero color write mask but modify the blend state so that no
2016 // drawing is done.
2017 // http://code.google.com/p/angleproject/issues/detail?id=169
2018
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002019 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002020 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002021 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002023 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024 }
2025 else
2026 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002027 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028 }
2029
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002030 mCullStateDirty = false;
2031 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002033 if (mDepthStateDirty)
2034 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00002035 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002036 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002037 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2038 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002039 }
2040 else
2041 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002042 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002043 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002044
2045 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046 }
2047
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002048 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2049 {
2050 mBlendStateDirty = true;
2051 mMaskStateDirty = true;
2052 }
2053
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002054 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002056 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002057 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002058 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002059
2060 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2061 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2062 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002063 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002064 }
2065 else
2066 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002067 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002068 unorm<8>(mState.blendColor.alpha),
2069 unorm<8>(mState.blendColor.alpha),
2070 unorm<8>(mState.blendColor.alpha)));
2071 }
2072
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002073 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2074 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2075 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002076
2077 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2078 mState.destBlendRGB != mState.destBlendAlpha ||
2079 mState.blendEquationRGB != mState.blendEquationAlpha)
2080 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002081 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002082
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002083 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2084 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2085 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002086 }
2087 else
2088 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002089 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002090 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002091 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002092 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002093 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002094 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002095 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002096
2097 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098 }
2099
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002100 if (mStencilStateDirty || mFrontFaceDirty)
2101 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002102 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002103 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002104 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2105 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002106
2107 // FIXME: Unsupported by D3D9
2108 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2109 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2110 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2111 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2112 mState.stencilRef != mState.stencilBackRef ||
2113 mState.stencilMask != mState.stencilBackMask)
2114 {
2115 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2116 return error(GL_INVALID_OPERATION);
2117 }
2118
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002119 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002120 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002121 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2122
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002123 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2124 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002125 es2dx::ConvertComparison(mState.stencilFunc));
2126
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002127 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2128 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002129
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002130 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002131 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002132 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002133 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002134 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002135 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2136
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002137 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2138 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002139 es2dx::ConvertComparison(mState.stencilBackFunc));
2140
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002141 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2142 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002143
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002144 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002145 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002146 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002147 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002148 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002149 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2150 }
2151 else
2152 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002153 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002154 }
2155
2156 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002157 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002158 }
2159
2160 if (mMaskStateDirty)
2161 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002162 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2163 mState.colorMaskBlue, mState.colorMaskAlpha);
2164 if (colorMask == 0 && !zeroColorMaskAllowed)
2165 {
2166 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002167 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2168 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002169
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002170 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2171 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2172 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002173 }
2174 else
2175 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002176 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002177 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002178 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002179
2180 mMaskStateDirty = false;
2181 }
2182
2183 if (mPolygonOffsetStateDirty)
2184 {
2185 if (mState.polygonOffsetFill)
2186 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002187 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002188 if (depthbuffer)
2189 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002190 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002191 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002192 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002193 }
2194 }
2195 else
2196 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002197 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2198 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002199 }
2200
2201 mPolygonOffsetStateDirty = false;
2202 }
2203
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002204 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002206 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002207 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002208 FIXME("Sample alpha to coverage is unimplemented.");
2209 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002210
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002211 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002212 if (mState.sampleCoverage)
2213 {
2214 unsigned int mask = 0;
2215 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002216 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002217 float threshold = 0.5f;
2218
2219 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002220 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002221 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002222
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002223 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002224 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002225 threshold += 1.0f;
2226 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002227 }
2228 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002229 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002230
2231 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002232 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002233 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002234 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002235
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002236 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002237 }
2238 else
2239 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002240 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002241 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002242
2243 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 }
2245
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002246 if (mDitherStateDirty)
2247 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002248 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002249
2250 mDitherStateDirty = false;
2251 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252}
2253
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002254GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002255{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002256 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002257
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +00002258 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002259 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002260 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002261 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002262 }
2263
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002264 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram(), instances, repeatDraw);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002265}
2266
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002267// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00002268GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002269{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002270 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002271
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002272 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002273 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002274 if (indexInfo->serial != mAppliedIBSerial)
2275 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002276 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002277 mAppliedIBSerial = indexInfo->serial;
2278 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002279 }
2280
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002281 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282}
2283
2284// Applies the shaders and shader constants to the Direct3D 9 device
2285void Context::applyShaders()
2286{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002288 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002289 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002290 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2291 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2292
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002293 mDevice->SetPixelShader(pixelShader);
2294 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002295 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002296 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002297 }
2298
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 programObject->applyUniforms();
2300}
2301
2302// Applies the textures and sampler states to the Direct3D 9 device
2303void Context::applyTextures()
2304{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002305 applyTextures(SAMPLER_PIXEL);
2306
2307 if (mSupportsVertexTexture)
2308 {
2309 applyTextures(SAMPLER_VERTEX);
2310 }
2311}
2312
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002313// For each Direct3D 9 sampler of either the pixel or vertex stage,
2314// looks up the corresponding OpenGL texture image unit and texture type,
2315// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002316void Context::applyTextures(SamplerType type)
2317{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 Program *programObject = getCurrentProgram();
2319
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002320 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 +00002321 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2322 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002323 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002324
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002325 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002327 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002328 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002329
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002330 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002332 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002333
2334 Texture *texture = getSamplerTexture(textureUnit, textureType);
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002335 unsigned int texSerial = texture->getTextureSerial();
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002336
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002337 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002338 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002339 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2340
2341 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002342 {
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002343 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002344 {
2345 GLenum wrapS = texture->getWrapS();
2346 GLenum wrapT = texture->getWrapT();
2347 GLenum minFilter = texture->getMinFilter();
2348 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002350 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2351 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002353 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002354 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2355 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002356 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2357 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002358 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002360 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002361 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002362 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002363 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002364 }
2365 else
2366 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002367 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002368 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002369
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002370 appliedTextureSerial[samplerIndex] = texSerial;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002371 texture->resetDirty();
2372 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002374 else
2375 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002376 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002377 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002378 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002379 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002380 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002381 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002382 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002383
2384 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2385 {
2386 if (appliedTextureSerial[samplerIndex] != 0)
2387 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002388 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002389 appliedTextureSerial[samplerIndex] = 0;
2390 }
2391 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392}
2393
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002394void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2395 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002397 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002398
2399 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2400 {
2401 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2402 }
2403
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002404 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2405 {
2406 return error(GL_INVALID_OPERATION);
2407 }
2408
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002409 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2410 // sized query sanity check
2411 if (bufSize)
2412 {
2413 int requiredSize = outputPitch * height;
2414 if (requiredSize > *bufSize)
2415 {
2416 return error(GL_INVALID_OPERATION);
2417 }
2418 }
2419
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002421 if (!renderTarget)
2422 {
2423 return; // Context must be lost, return silently
2424 }
2425
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 D3DSURFACE_DESC desc;
2427 renderTarget->GetDesc(&desc);
2428
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2430 {
2431 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002432 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002433 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 }
2435
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002436 HRESULT result;
2437 IDirect3DSurface9 *systemSurface = NULL;
2438 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002439 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002440 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2441 if (directToPixels)
2442 {
2443 // Use the pixels ptr as a shared handle to write directly into client's memory
2444 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2445 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2446 if (FAILED(result))
2447 {
2448 // Try again without the shared handle
2449 directToPixels = false;
2450 }
2451 }
2452
2453 if (!directToPixels)
2454 {
2455 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2456 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2457 if (FAILED(result))
2458 {
2459 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2460 return error(GL_OUT_OF_MEMORY);
2461 }
2462 }
2463
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002464 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002465 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002466 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002467
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468 if (FAILED(result))
2469 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002470 systemSurface->Release();
2471
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002472 // It turns out that D3D will sometimes produce more error
2473 // codes than those documented.
2474 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002475 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002476 else
2477 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002478 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002479 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002480 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002481
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002482 }
2483
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002484 if (directToPixels)
2485 {
2486 systemSurface->Release();
2487 return;
2488 }
2489
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002491 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2492 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2493 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2494 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2495 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496
2497 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2498
2499 if (FAILED(result))
2500 {
2501 UNREACHABLE();
2502 systemSurface->Release();
2503
2504 return; // No sensible error to generate
2505 }
2506
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002508 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002509
2510 unsigned char *source;
2511 int inputPitch;
2512 if (getPackReverseRowOrder())
2513 {
2514 source = (unsigned char*)lock.pBits;
2515 inputPitch = lock.Pitch;
2516 }
2517 else
2518 {
2519 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2520 inputPitch = -lock.Pitch;
2521 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002522
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523 for (int j = 0; j < rect.bottom - rect.top; j++)
2524 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002525 if (desc.Format == D3DFMT_A8R8G8B8 &&
2526 format == GL_BGRA_EXT &&
2527 type == GL_UNSIGNED_BYTE)
2528 {
2529 // Fast path for EXT_read_format_bgra, given
2530 // an RGBA source buffer. Note that buffers with no
2531 // alpha go through the slow path below.
2532 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002533 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002534 (rect.right - rect.left) * 4);
2535 continue;
2536 }
2537
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538 for (int i = 0; i < rect.right - rect.left; i++)
2539 {
2540 float r;
2541 float g;
2542 float b;
2543 float a;
2544
2545 switch (desc.Format)
2546 {
2547 case D3DFMT_R5G6B5:
2548 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002549 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550
2551 a = 1.0f;
2552 b = (rgb & 0x001F) * (1.0f / 0x001F);
2553 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2554 r = (rgb & 0xF800) * (1.0f / 0xF800);
2555 }
2556 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557 case D3DFMT_A1R5G5B5:
2558 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002559 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002560
2561 a = (argb & 0x8000) ? 1.0f : 0.0f;
2562 b = (argb & 0x001F) * (1.0f / 0x001F);
2563 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2564 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2565 }
2566 break;
2567 case D3DFMT_A8R8G8B8:
2568 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002569 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570
2571 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2572 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2573 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2574 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2575 }
2576 break;
2577 case D3DFMT_X8R8G8B8:
2578 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002579 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580
2581 a = 1.0f;
2582 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2583 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2584 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2585 }
2586 break;
2587 case D3DFMT_A2R10G10B10:
2588 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002589 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002590
2591 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2592 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2593 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2594 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2595 }
2596 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002597 case D3DFMT_A32B32G32R32F:
2598 {
2599 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002600 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2601 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2602 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2603 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002604 }
2605 break;
2606 case D3DFMT_A16B16G16R16F:
2607 {
2608 // float formats in D3D are stored rgba, rather than the other way round
2609 float abgr[4];
2610
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002611 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002612
2613 a = abgr[3];
2614 b = abgr[2];
2615 g = abgr[1];
2616 r = abgr[0];
2617 }
2618 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619 default:
2620 UNIMPLEMENTED(); // FIXME
2621 UNREACHABLE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002622 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002623 }
2624
2625 switch (format)
2626 {
2627 case GL_RGBA:
2628 switch (type)
2629 {
2630 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002631 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2632 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2633 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2634 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635 break;
2636 default: UNREACHABLE();
2637 }
2638 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002639 case GL_BGRA_EXT:
2640 switch (type)
2641 {
2642 case GL_UNSIGNED_BYTE:
2643 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2644 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2645 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2646 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2647 break;
2648 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2649 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2650 // this type is packed as follows:
2651 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2652 // --------------------------------------------------------------------------------
2653 // | 4th | 3rd | 2nd | 1st component |
2654 // --------------------------------------------------------------------------------
2655 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2656 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2657 ((unsigned short)(15 * a + 0.5f) << 12)|
2658 ((unsigned short)(15 * r + 0.5f) << 8) |
2659 ((unsigned short)(15 * g + 0.5f) << 4) |
2660 ((unsigned short)(15 * b + 0.5f) << 0);
2661 break;
2662 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2663 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2664 // this type is packed as follows:
2665 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2666 // --------------------------------------------------------------------------------
2667 // | 4th | 3rd | 2nd | 1st component |
2668 // --------------------------------------------------------------------------------
2669 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2670 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2671 ((unsigned short)( a + 0.5f) << 15) |
2672 ((unsigned short)(31 * r + 0.5f) << 10) |
2673 ((unsigned short)(31 * g + 0.5f) << 5) |
2674 ((unsigned short)(31 * b + 0.5f) << 0);
2675 break;
2676 default: UNREACHABLE();
2677 }
2678 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002679 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002680 switch (type)
2681 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002682 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002683 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2684 ((unsigned short)(31 * b + 0.5f) << 0) |
2685 ((unsigned short)(63 * g + 0.5f) << 5) |
2686 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002687 break;
2688 default: UNREACHABLE();
2689 }
2690 break;
2691 default: UNREACHABLE();
2692 }
2693 }
2694 }
2695
2696 systemSurface->UnlockRect();
2697
2698 systemSurface->Release();
2699}
2700
2701void Context::clear(GLbitfield mask)
2702{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002703 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002704
2705 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2706 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002707 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002708 }
2709
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710 DWORD flags = 0;
2711
2712 if (mask & GL_COLOR_BUFFER_BIT)
2713 {
2714 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002715
2716 if (framebufferObject->getColorbufferType() != GL_NONE)
2717 {
2718 flags |= D3DCLEAR_TARGET;
2719 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720 }
2721
2722 if (mask & GL_DEPTH_BUFFER_BIT)
2723 {
2724 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002725 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726 {
2727 flags |= D3DCLEAR_ZBUFFER;
2728 }
2729 }
2730
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731 GLuint stencilUnmasked = 0x0;
2732
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002733 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002736 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002738 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002739 if (!depthStencil)
2740 {
2741 ERR("Depth stencil pointer unexpectedly null.");
2742 return;
2743 }
2744
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002745 D3DSURFACE_DESC desc;
2746 depthStencil->GetDesc(&desc);
2747
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002748 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002749 stencilUnmasked = (0x1 << stencilSize) - 1;
2750
2751 if (stencilUnmasked != 0x0)
2752 {
2753 flags |= D3DCLEAR_STENCIL;
2754 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002755 }
2756 }
2757
2758 if (mask != 0)
2759 {
2760 return error(GL_INVALID_VALUE);
2761 }
2762
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002763 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2764 {
2765 return;
2766 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002767
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002768 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002769 unorm<8>(mState.colorClearValue.red),
2770 unorm<8>(mState.colorClearValue.green),
2771 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002772 float depth = clamp01(mState.depthClearValue);
2773 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002774
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002775 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776
2777 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002778 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002779 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002780 !(mState.colorMaskRed && mState.colorMaskGreen &&
2781 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002782
2783 if (needMaskedColorClear || needMaskedStencilClear)
2784 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002785 // State which is altered in all paths from this point to the clear call is saved.
2786 // State which is altered in only some paths will be flagged dirty in the case that
2787 // that path is taken.
2788 HRESULT hr;
2789 if (mMaskedClearSavedState == NULL)
2790 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002791 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002792 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2793
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002794 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2795 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2796 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2797 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2798 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2799 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2800 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2801 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2802 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2803 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2804 mDevice->SetPixelShader(NULL);
2805 mDevice->SetVertexShader(NULL);
2806 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2807 mDevice->SetStreamSource(0, NULL, 0, 0);
2808 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2809 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2810 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2811 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2812 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2813 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2814 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002815
2816 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2817 {
2818 mDevice->SetStreamSourceFreq(i, 1);
2819 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002820
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002821 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002822 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2823 }
2824
2825 ASSERT(mMaskedClearSavedState != NULL);
2826
2827 if (mMaskedClearSavedState != NULL)
2828 {
2829 hr = mMaskedClearSavedState->Capture();
2830 ASSERT(SUCCEEDED(hr));
2831 }
2832
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002833 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2834 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2835 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2836 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2837 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2838 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2839 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2840 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002841
2842 if (flags & D3DCLEAR_TARGET)
2843 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002844 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002845 }
2846 else
2847 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002848 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849 }
2850
2851 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2852 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002853 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2854 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2855 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2856 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2857 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2858 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2859 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2860 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002861 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002862 }
2863 else
2864 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002865 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002866 }
2867
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002868 mDevice->SetPixelShader(NULL);
2869 mDevice->SetVertexShader(NULL);
2870 mDevice->SetFVF(D3DFVF_XYZRHW);
2871 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2872 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2873 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2874 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2875 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2876 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2877 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002878
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002879 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2880 {
2881 mDevice->SetStreamSourceFreq(i, 1);
2882 }
2883
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002884 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2885 quad[0][0] = -0.5f;
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002886 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002887 quad[0][2] = 0.0f;
2888 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002889
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002890 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2891 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002892 quad[1][2] = 0.0f;
2893 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002894
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002895 quad[2][0] = -0.5f;
2896 quad[2][1] = -0.5f;
2897 quad[2][2] = 0.0f;
2898 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002899
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002900 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002901 quad[3][1] = -0.5f;
2902 quad[3][2] = 0.0f;
2903 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002904
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002905 mDisplay->startScene();
2906 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002907
2908 if (flags & D3DCLEAR_ZBUFFER)
2909 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002910 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2911 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2912 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002913 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002914
2915 if (mMaskedClearSavedState != NULL)
2916 {
2917 mMaskedClearSavedState->Apply();
2918 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002919 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002920 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002921 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002922 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002923 }
2924}
2925
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002926void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002927{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002928 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002929 {
2930 return error(GL_INVALID_OPERATION);
2931 }
2932
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002933 D3DPRIMITIVETYPE primitiveType;
2934 int primitiveCount;
2935
2936 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2937 return error(GL_INVALID_ENUM);
2938
2939 if (primitiveCount <= 0)
2940 {
2941 return;
2942 }
2943
2944 if (!applyRenderTarget(false))
2945 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002946 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002947 }
2948
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002949 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002950
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002951 GLsizei repeatDraw = 1;
2952 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002953 if (err != GL_NO_ERROR)
2954 {
2955 return error(err);
2956 }
2957
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002958 applyShaders();
2959 applyTextures();
2960
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002961 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002962 {
2963 return error(GL_INVALID_OPERATION);
2964 }
2965
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002966 if (!cullSkipsDraw(mode))
2967 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002968 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002969
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002970 if (mode == GL_LINE_LOOP)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002971 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002972 drawLineLoop(count, GL_NONE, NULL, 0);
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002973 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002974 else if (instances > 0)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002975 {
2976 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
2977 if (countingIB)
2978 {
2979 if (mAppliedIBSerial != countingIB->getSerial())
2980 {
2981 mDevice->SetIndices(countingIB->getBuffer());
2982 mAppliedIBSerial = countingIB->getSerial();
2983 }
2984
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002985 for (int i = 0; i < repeatDraw; i++)
2986 {
2987 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
2988 }
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002989 }
2990 else
2991 {
2992 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
2993 return error(GL_OUT_OF_MEMORY);
2994 }
2995 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002996 else // Regular case
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002997 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002998 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002999 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003000 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003001}
3002
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00003003void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003004{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003005 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003006 {
3007 return error(GL_INVALID_OPERATION);
3008 }
3009
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003010 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003011 {
3012 return error(GL_INVALID_OPERATION);
3013 }
3014
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003015 D3DPRIMITIVETYPE primitiveType;
3016 int primitiveCount;
3017
3018 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3019 return error(GL_INVALID_ENUM);
3020
3021 if (primitiveCount <= 0)
3022 {
3023 return;
3024 }
3025
3026 if (!applyRenderTarget(false))
3027 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00003028 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003029 }
3030
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003031 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00003032
3033 TranslatedIndexData indexInfo;
3034 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3035 if (err != GL_NO_ERROR)
3036 {
3037 return error(err);
3038 }
3039
daniel@transgaming.com83921382011-01-08 05:46:00 +00003040 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003041 GLsizei repeatDraw = 1;
3042 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00003043 if (err != GL_NO_ERROR)
3044 {
3045 return error(err);
3046 }
3047
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003048 applyShaders();
3049 applyTextures();
3050
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00003051 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003052 {
3053 return error(GL_INVALID_OPERATION);
3054 }
3055
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003056 if (!cullSkipsDraw(mode))
3057 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003058 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003059
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003060 if (mode == GL_LINE_LOOP)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003061 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003062 drawLineLoop(count, type, indices, indexInfo.minIndex);
3063 }
3064 else
3065 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003066 for (int i = 0; i < repeatDraw; i++)
3067 {
3068 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3069 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003070 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003071 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003072}
3073
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00003074// Implements glFlush when block is false, glFinish when block is true
3075void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003076{
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +00003077 mDisplay->sync(block);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003078}
3079
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003080void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003081{
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003082 // Get the raw indices for an indexed draw
3083 if (type != GL_NONE && mState.elementArrayBuffer.get())
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003084 {
3085 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3086 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3087 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3088 }
3089
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003090 UINT startIndex = 0;
3091 bool succeeded = false;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003092
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003093 if (supports32bitIndices())
3094 {
3095 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3096
3097 if (!mLineLoopIB)
3098 {
3099 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3100 }
3101
3102 if (mLineLoopIB)
3103 {
3104 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3105
3106 UINT offset = 0;
3107 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3108 startIndex = offset / 4;
3109
3110 if (data)
3111 {
3112 switch (type)
3113 {
3114 case GL_NONE: // Non-indexed draw
3115 for (int i = 0; i < count; i++)
3116 {
3117 data[i] = i;
3118 }
3119 data[count] = 0;
3120 break;
3121 case GL_UNSIGNED_BYTE:
3122 for (int i = 0; i < count; i++)
3123 {
3124 data[i] = static_cast<const GLubyte*>(indices)[i];
3125 }
3126 data[count] = static_cast<const GLubyte*>(indices)[0];
3127 break;
3128 case GL_UNSIGNED_SHORT:
3129 for (int i = 0; i < count; i++)
3130 {
3131 data[i] = static_cast<const GLushort*>(indices)[i];
3132 }
3133 data[count] = static_cast<const GLushort*>(indices)[0];
3134 break;
3135 case GL_UNSIGNED_INT:
3136 for (int i = 0; i < count; i++)
3137 {
3138 data[i] = static_cast<const GLuint*>(indices)[i];
3139 }
3140 data[count] = static_cast<const GLuint*>(indices)[0];
3141 break;
3142 default: UNREACHABLE();
3143 }
3144
3145 mLineLoopIB->unmap();
3146 succeeded = true;
3147 }
3148 }
3149 }
3150 else
3151 {
3152 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3153
3154 if (!mLineLoopIB)
3155 {
3156 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3157 }
3158
3159 if (mLineLoopIB)
3160 {
3161 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3162
3163 UINT offset = 0;
3164 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3165 startIndex = offset / 2;
3166
3167 if (data)
3168 {
3169 switch (type)
3170 {
3171 case GL_NONE: // Non-indexed draw
3172 for (int i = 0; i < count; i++)
3173 {
3174 data[i] = i;
3175 }
3176 data[count] = 0;
3177 break;
3178 case GL_UNSIGNED_BYTE:
3179 for (int i = 0; i < count; i++)
3180 {
3181 data[i] = static_cast<const GLubyte*>(indices)[i];
3182 }
3183 data[count] = static_cast<const GLubyte*>(indices)[0];
3184 break;
3185 case GL_UNSIGNED_SHORT:
3186 for (int i = 0; i < count; i++)
3187 {
3188 data[i] = static_cast<const GLushort*>(indices)[i];
3189 }
3190 data[count] = static_cast<const GLushort*>(indices)[0];
3191 break;
3192 case GL_UNSIGNED_INT:
3193 for (int i = 0; i < count; i++)
3194 {
3195 data[i] = static_cast<const GLuint*>(indices)[i];
3196 }
3197 data[count] = static_cast<const GLuint*>(indices)[0];
3198 break;
3199 default: UNREACHABLE();
3200 }
3201
3202 mLineLoopIB->unmap();
3203 succeeded = true;
3204 }
3205 }
3206 }
3207
3208 if (succeeded)
3209 {
3210 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3211 {
3212 mDevice->SetIndices(mLineLoopIB->getBuffer());
3213 mAppliedIBSerial = mLineLoopIB->getSerial();
3214 }
3215
3216 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3217 }
3218 else
3219 {
3220 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3221 return error(GL_OUT_OF_MEMORY);
3222 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003223}
3224
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003225void Context::recordInvalidEnum()
3226{
3227 mInvalidEnum = true;
3228}
3229
3230void Context::recordInvalidValue()
3231{
3232 mInvalidValue = true;
3233}
3234
3235void Context::recordInvalidOperation()
3236{
3237 mInvalidOperation = true;
3238}
3239
3240void Context::recordOutOfMemory()
3241{
3242 mOutOfMemory = true;
3243}
3244
3245void Context::recordInvalidFramebufferOperation()
3246{
3247 mInvalidFramebufferOperation = true;
3248}
3249
3250// Get one of the recorded errors and clear its flag, if any.
3251// [OpenGL ES 2.0.24] section 2.5 page 13.
3252GLenum Context::getError()
3253{
3254 if (mInvalidEnum)
3255 {
3256 mInvalidEnum = false;
3257
3258 return GL_INVALID_ENUM;
3259 }
3260
3261 if (mInvalidValue)
3262 {
3263 mInvalidValue = false;
3264
3265 return GL_INVALID_VALUE;
3266 }
3267
3268 if (mInvalidOperation)
3269 {
3270 mInvalidOperation = false;
3271
3272 return GL_INVALID_OPERATION;
3273 }
3274
3275 if (mOutOfMemory)
3276 {
3277 mOutOfMemory = false;
3278
3279 return GL_OUT_OF_MEMORY;
3280 }
3281
3282 if (mInvalidFramebufferOperation)
3283 {
3284 mInvalidFramebufferOperation = false;
3285
3286 return GL_INVALID_FRAMEBUFFER_OPERATION;
3287 }
3288
3289 return GL_NO_ERROR;
3290}
3291
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003292GLenum Context::getResetStatus()
3293{
3294 if (mResetStatus == GL_NO_ERROR)
3295 {
3296 bool lost = mDisplay->testDeviceLost();
3297
3298 if (lost)
3299 {
3300 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3301 }
3302 }
3303
3304 GLenum status = mResetStatus;
3305
3306 if (mResetStatus != GL_NO_ERROR)
3307 {
3308 if (mDisplay->testDeviceResettable())
3309 {
3310 mResetStatus = GL_NO_ERROR;
3311 }
3312 }
3313
3314 return status;
3315}
3316
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003317bool Context::isResetNotificationEnabled()
3318{
3319 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3320}
3321
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003322bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003323{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003324 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003325}
3326
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003327int Context::getMaximumVaryingVectors() const
3328{
3329 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3330}
3331
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003332unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003333{
3334 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3335}
3336
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003337unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003338{
3339 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3340}
3341
daniel@transgaming.com458da142010-11-28 02:03:02 +00003342int Context::getMaximumFragmentUniformVectors() const
3343{
3344 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3345}
3346
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003347int Context::getMaxSupportedSamples() const
3348{
3349 return mMaxSupportedSamples;
3350}
3351
3352int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3353{
3354 if (requested == 0)
3355 {
3356 return requested;
3357 }
3358
3359 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3360 if (itr == mMultiSampleSupport.end())
3361 {
3362 return -1;
3363 }
3364
3365 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3366 {
3367 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3368 {
3369 return i;
3370 }
3371 }
3372
3373 return -1;
3374}
3375
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003376bool Context::supportsEventQueries() const
3377{
3378 return mSupportsEventQueries;
3379}
3380
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003381bool Context::supportsOcclusionQueries() const
3382{
3383 return mSupportsOcclusionQueries;
3384}
3385
gman@chromium.org50c526d2011-08-10 05:19:44 +00003386bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003387{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003388 return mSupportsDXT1Textures;
3389}
3390
3391bool Context::supportsDXT3Textures() const
3392{
3393 return mSupportsDXT3Textures;
3394}
3395
3396bool Context::supportsDXT5Textures() const
3397{
3398 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003399}
3400
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003401bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003402{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003403 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003404}
3405
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003406bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003407{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003408 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003409}
3410
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003411bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003412{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003413 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003414}
3415
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003416bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003417{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003418 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003419}
3420
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003421bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003422{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003423 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003424}
3425
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003426bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003427{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003428 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003429}
3430
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003431int Context::getMaximumRenderbufferDimension() const
3432{
3433 return mMaxRenderbufferDimension;
3434}
3435
3436int Context::getMaximumTextureDimension() const
3437{
3438 return mMaxTextureDimension;
3439}
3440
3441int Context::getMaximumCubeTextureDimension() const
3442{
3443 return mMaxCubeTextureDimension;
3444}
3445
3446int Context::getMaximumTextureLevel() const
3447{
3448 return mMaxTextureLevel;
3449}
3450
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003451bool Context::supportsLuminanceTextures() const
3452{
3453 return mSupportsLuminanceTextures;
3454}
3455
3456bool Context::supportsLuminanceAlphaTextures() const
3457{
3458 return mSupportsLuminanceAlphaTextures;
3459}
3460
daniel@transgaming.com83921382011-01-08 05:46:00 +00003461bool Context::supports32bitIndices() const
3462{
3463 return mSupports32bitIndices;
3464}
3465
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003466bool Context::supportsNonPower2Texture() const
3467{
3468 return mSupportsNonPower2Texture;
3469}
3470
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003471bool Context::supportsInstancing() const
3472{
3473 return mSupportsInstancing;
3474}
3475
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003476void Context::detachBuffer(GLuint buffer)
3477{
3478 // [OpenGL ES 2.0.24] section 2.9 page 22:
3479 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3480 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3481
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003482 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003483 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003484 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003485 }
3486
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003487 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003488 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003489 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003490 }
3491
3492 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3493 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003494 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003495 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003496 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003497 }
3498 }
3499}
3500
3501void Context::detachTexture(GLuint texture)
3502{
3503 // [OpenGL ES 2.0.24] section 3.8 page 84:
3504 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3505 // rebound to texture object zero
3506
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003507 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003508 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003509 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003510 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003511 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003512 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003513 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003514 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003515 }
3516 }
3517
3518 // [OpenGL ES 2.0.24] section 4.4 page 112:
3519 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3520 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3521 // image was attached in the currently bound framebuffer.
3522
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003523 Framebuffer *readFramebuffer = getReadFramebuffer();
3524 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003525
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003526 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003527 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003528 readFramebuffer->detachTexture(texture);
3529 }
3530
3531 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3532 {
3533 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003534 }
3535}
3536
3537void Context::detachFramebuffer(GLuint framebuffer)
3538{
3539 // [OpenGL ES 2.0.24] section 4.4 page 107:
3540 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3541 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3542
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003543 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003544 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003545 bindReadFramebuffer(0);
3546 }
3547
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003548 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003549 {
3550 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003551 }
3552}
3553
3554void Context::detachRenderbuffer(GLuint renderbuffer)
3555{
3556 // [OpenGL ES 2.0.24] section 4.4 page 109:
3557 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3558 // had been executed with the target RENDERBUFFER and name of zero.
3559
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003560 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003561 {
3562 bindRenderbuffer(0);
3563 }
3564
3565 // [OpenGL ES 2.0.24] section 4.4 page 111:
3566 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3567 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3568 // point to which this image was attached in the currently bound framebuffer.
3569
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003570 Framebuffer *readFramebuffer = getReadFramebuffer();
3571 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003572
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003573 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003574 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003575 readFramebuffer->detachRenderbuffer(renderbuffer);
3576 }
3577
3578 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3579 {
3580 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003581 }
3582}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003583
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003584Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003585{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003586 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003587
3588 if (t == NULL)
3589 {
3590 static const GLubyte color[] = { 0, 0, 0, 255 };
3591
3592 switch (type)
3593 {
3594 default:
3595 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003596 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003597
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003598 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003599 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003600 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003601 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003602 t = incomplete2d;
3603 }
3604 break;
3605
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003606 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003607 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003608 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003609
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003610 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3611 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3612 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3613 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3614 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3615 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003616
3617 t = incompleteCube;
3618 }
3619 break;
3620 }
3621
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003622 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003623 }
3624
3625 return t;
3626}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003627
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003628bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003629{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003630 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003631}
3632
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003633bool Context::isTriangleMode(GLenum drawMode)
3634{
3635 switch (drawMode)
3636 {
3637 case GL_TRIANGLES:
3638 case GL_TRIANGLE_FAN:
3639 case GL_TRIANGLE_STRIP:
3640 return true;
3641 case GL_POINTS:
3642 case GL_LINES:
3643 case GL_LINE_LOOP:
3644 case GL_LINE_STRIP:
3645 return false;
3646 default: UNREACHABLE();
3647 }
3648
3649 return false;
3650}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003651
3652void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3653{
3654 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3655
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003656 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3657 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3658 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3659 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003660
daniel@transgaming.com83921382011-01-08 05:46:00 +00003661 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003662}
3663
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00003664void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3665{
3666 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3667
3668 mState.vertexAttribute[index].mDivisor = divisor;
3669}
3670
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003671// keep list sorted in following order
3672// OES extensions
3673// EXT extensions
3674// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003675void Context::initExtensionString()
3676{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003677 mExtensionString = "";
3678
3679 // OES extensions
3680 if (supports32bitIndices())
3681 {
3682 mExtensionString += "GL_OES_element_index_uint ";
3683 }
3684
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003685 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003686 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003687 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003688
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003689 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003690 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003691 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003692 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003693 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003694 {
3695 mExtensionString += "GL_OES_texture_half_float_linear ";
3696 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003697 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003698 {
3699 mExtensionString += "GL_OES_texture_float ";
3700 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003701 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003702 {
3703 mExtensionString += "GL_OES_texture_float_linear ";
3704 }
3705
3706 if (supportsNonPower2Texture())
3707 {
3708 mExtensionString += "GL_OES_texture_npot ";
3709 }
3710
3711 // Multi-vendor (EXT) extensions
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003712 if (supportsOcclusionQueries())
3713 {
3714 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3715 }
3716
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003717 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003718 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003719
gman@chromium.org50c526d2011-08-10 05:19:44 +00003720 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003721 {
3722 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3723 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003724
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003725 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.comdf363722011-12-16 23:29:53 +00003726 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003727
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003728 // ANGLE-specific extensions
3729 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003730 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003731 {
3732 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3733 }
3734
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003735 if (supportsInstancing())
daniel@transgaming.comdce02fd2012-01-27 15:39:51 +00003736 {
3737 mExtensionString += "GL_ANGLE_instanced_arrays ";
3738 }
3739
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003740 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3741
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003742 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003743 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003744 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3745 }
3746 if (supportsDXT5Textures())
3747 {
3748 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003749 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003750
3751 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003752 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003753
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003754 // Other vendor-specific extensions
3755 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003756 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003757 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003758 }
3759
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003760 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3761 if (end != std::string::npos)
3762 {
3763 mExtensionString.resize(end+1);
3764 }
3765}
3766
3767const char *Context::getExtensionString() const
3768{
3769 return mExtensionString.c_str();
3770}
3771
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003772void Context::initRendererString()
3773{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003774 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003775
3776 mRendererString = "ANGLE (";
3777 mRendererString += identifier->Description;
3778 mRendererString += ")";
3779}
3780
3781const char *Context::getRendererString() const
3782{
3783 return mRendererString.c_str();
3784}
3785
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003786void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3787 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3788 GLbitfield mask)
3789{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003790 Framebuffer *readFramebuffer = getReadFramebuffer();
3791 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3792
3793 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3794 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3795 {
3796 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3797 }
3798
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003799 if (drawFramebuffer->getSamples() != 0)
3800 {
3801 return error(GL_INVALID_OPERATION);
3802 }
3803
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003804 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3805 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3806 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3807 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3808
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003809 RECT sourceRect;
3810 RECT destRect;
3811
3812 if (srcX0 < srcX1)
3813 {
3814 sourceRect.left = srcX0;
3815 sourceRect.right = srcX1;
3816 destRect.left = dstX0;
3817 destRect.right = dstX1;
3818 }
3819 else
3820 {
3821 sourceRect.left = srcX1;
3822 destRect.left = dstX1;
3823 sourceRect.right = srcX0;
3824 destRect.right = dstX0;
3825 }
3826
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003827 if (srcY0 < srcY1)
3828 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003829 sourceRect.top = readBufferHeight - srcY1;
3830 destRect.top = drawBufferHeight - dstY1;
3831 sourceRect.bottom = readBufferHeight - srcY0;
3832 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003833 }
3834 else
3835 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003836 sourceRect.top = readBufferHeight - srcY0;
3837 destRect.top = drawBufferHeight - dstY0;
3838 sourceRect.bottom = readBufferHeight - srcY1;
3839 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003840 }
3841
3842 RECT sourceScissoredRect = sourceRect;
3843 RECT destScissoredRect = destRect;
3844
3845 if (mState.scissorTest)
3846 {
3847 // Only write to parts of the destination framebuffer which pass the scissor test
3848 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3849 // rect will be checked against scissorY, rather than the bottom.
3850 if (destRect.left < mState.scissorX)
3851 {
3852 int xDiff = mState.scissorX - destRect.left;
3853 destScissoredRect.left = mState.scissorX;
3854 sourceScissoredRect.left += xDiff;
3855 }
3856
3857 if (destRect.right > mState.scissorX + mState.scissorWidth)
3858 {
3859 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3860 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3861 sourceScissoredRect.right -= xDiff;
3862 }
3863
3864 if (destRect.top < mState.scissorY)
3865 {
3866 int yDiff = mState.scissorY - destRect.top;
3867 destScissoredRect.top = mState.scissorY;
3868 sourceScissoredRect.top += yDiff;
3869 }
3870
3871 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3872 {
3873 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3874 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3875 sourceScissoredRect.bottom -= yDiff;
3876 }
3877 }
3878
3879 bool blitRenderTarget = false;
3880 bool blitDepthStencil = false;
3881
3882 RECT sourceTrimmedRect = sourceScissoredRect;
3883 RECT destTrimmedRect = destScissoredRect;
3884
3885 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3886 // the actual draw and read surfaces.
3887 if (sourceTrimmedRect.left < 0)
3888 {
3889 int xDiff = 0 - sourceTrimmedRect.left;
3890 sourceTrimmedRect.left = 0;
3891 destTrimmedRect.left += xDiff;
3892 }
3893
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003894 if (sourceTrimmedRect.right > readBufferWidth)
3895 {
3896 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3897 sourceTrimmedRect.right = readBufferWidth;
3898 destTrimmedRect.right -= xDiff;
3899 }
3900
3901 if (sourceTrimmedRect.top < 0)
3902 {
3903 int yDiff = 0 - sourceTrimmedRect.top;
3904 sourceTrimmedRect.top = 0;
3905 destTrimmedRect.top += yDiff;
3906 }
3907
3908 if (sourceTrimmedRect.bottom > readBufferHeight)
3909 {
3910 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3911 sourceTrimmedRect.bottom = readBufferHeight;
3912 destTrimmedRect.bottom -= yDiff;
3913 }
3914
3915 if (destTrimmedRect.left < 0)
3916 {
3917 int xDiff = 0 - destTrimmedRect.left;
3918 destTrimmedRect.left = 0;
3919 sourceTrimmedRect.left += xDiff;
3920 }
3921
3922 if (destTrimmedRect.right > drawBufferWidth)
3923 {
3924 int xDiff = destTrimmedRect.right - drawBufferWidth;
3925 destTrimmedRect.right = drawBufferWidth;
3926 sourceTrimmedRect.right -= xDiff;
3927 }
3928
3929 if (destTrimmedRect.top < 0)
3930 {
3931 int yDiff = 0 - destTrimmedRect.top;
3932 destTrimmedRect.top = 0;
3933 sourceTrimmedRect.top += yDiff;
3934 }
3935
3936 if (destTrimmedRect.bottom > drawBufferHeight)
3937 {
3938 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3939 destTrimmedRect.bottom = drawBufferHeight;
3940 sourceTrimmedRect.bottom -= yDiff;
3941 }
3942
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003943 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003944 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3945 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3946 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3947 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003948 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3949 {
3950 partialBufferCopy = true;
3951 }
3952
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003953 if (mask & GL_COLOR_BUFFER_BIT)
3954 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003955 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3956 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3957 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3958 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3959 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003960 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3961 {
3962 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3963 return error(GL_INVALID_OPERATION);
3964 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003965
3966 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3967 {
3968 return error(GL_INVALID_OPERATION);
3969 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003970
3971 blitRenderTarget = true;
3972
3973 }
3974
3975 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3976 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003977 Renderbuffer *readDSBuffer = NULL;
3978 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003979
3980 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3981 // both a depth and stencil buffer, it will be the same buffer.
3982
3983 if (mask & GL_DEPTH_BUFFER_BIT)
3984 {
3985 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3986 {
3987 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3988 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3989 {
3990 return error(GL_INVALID_OPERATION);
3991 }
3992
3993 blitDepthStencil = true;
3994 readDSBuffer = readFramebuffer->getDepthbuffer();
3995 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3996 }
3997 }
3998
3999 if (mask & GL_STENCIL_BUFFER_BIT)
4000 {
4001 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4002 {
4003 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
4004 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
4005 {
4006 return error(GL_INVALID_OPERATION);
4007 }
4008
4009 blitDepthStencil = true;
4010 readDSBuffer = readFramebuffer->getStencilbuffer();
4011 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4012 }
4013 }
4014
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004015 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004016 {
4017 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4018 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4019 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004020
daniel@transgaming.com97446d22010-08-24 19:20:54 +00004021 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4022 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004023 {
4024 return error(GL_INVALID_OPERATION);
4025 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004026 }
4027
4028 if (blitRenderTarget || blitDepthStencil)
4029 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004030 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004031
4032 if (blitRenderTarget)
4033 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00004034 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4035 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4036
4037 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4038 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4039
4040 readRenderTarget->Release();
4041 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004042
4043 if (FAILED(result))
4044 {
4045 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4046 return;
4047 }
4048 }
4049
4050 if (blitDepthStencil)
4051 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004052 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004053
4054 if (FAILED(result))
4055 {
4056 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4057 return;
4058 }
4059 }
4060 }
4061}
4062
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004063VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4064{
4065 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4066 {
4067 mVertexDeclCache[i].vertexDeclaration = NULL;
4068 mVertexDeclCache[i].lruCount = 0;
4069 }
4070}
4071
4072VertexDeclarationCache::~VertexDeclarationCache()
4073{
4074 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4075 {
4076 if (mVertexDeclCache[i].vertexDeclaration)
4077 {
4078 mVertexDeclCache[i].vertexDeclaration->Release();
4079 }
4080 }
4081}
4082
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004083GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004084{
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004085 *repeatDraw = 1;
4086
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004087 int indexedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004088 int instancedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004089
4090 if (instances > 0)
4091 {
4092 // Find an indexed attribute to be mapped to D3D stream 0
4093 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4094 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004095 if (attributes[i].active)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004096 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004097 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4098 {
4099 if (attributes[i].divisor == 0)
4100 {
4101 indexedAttribute = i;
4102 }
4103 }
4104 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4105 {
4106 if (attributes[i].divisor != 0)
4107 {
4108 instancedAttribute = i;
4109 }
4110 }
4111 else break; // Found both an indexed and instanced attribute
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004112 }
4113 }
4114
4115 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4116 {
4117 return GL_INVALID_OPERATION;
4118 }
4119 }
4120
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004121 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4122 D3DVERTEXELEMENT9 *element = &elements[0];
4123
4124 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4125 {
4126 if (attributes[i].active)
4127 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004128 int stream = i;
4129
4130 if (instances > 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004131 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004132 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4133 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004134 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004135 *repeatDraw = instances;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004136 }
4137 else
4138 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004139 if (i == indexedAttribute)
4140 {
4141 stream = 0;
4142 }
4143 else if (i == 0)
4144 {
4145 stream = indexedAttribute;
4146 }
4147
4148 UINT frequency = 1;
4149
4150 if (attributes[i].divisor == 0)
4151 {
4152 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4153 }
4154 else
4155 {
4156 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4157 }
4158
4159 device->SetStreamSourceFreq(stream, frequency);
4160 mInstancingEnabled = true;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004161 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004162 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004163
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004164 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4165 mAppliedVBs[stream].stride != attributes[i].stride ||
4166 mAppliedVBs[stream].offset != attributes[i].offset)
4167 {
4168 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4169 mAppliedVBs[stream].serial = attributes[i].serial;
4170 mAppliedVBs[stream].stride = attributes[i].stride;
4171 mAppliedVBs[stream].offset = attributes[i].offset;
4172 }
4173
4174 element->Stream = stream;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004175 element->Offset = 0;
4176 element->Type = attributes[i].type;
4177 element->Method = D3DDECLMETHOD_DEFAULT;
4178 element->Usage = D3DDECLUSAGE_TEXCOORD;
4179 element->UsageIndex = program->getSemanticIndex(i);
4180 element++;
4181 }
4182 }
4183
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004184 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004185 {
4186 if (mInstancingEnabled)
4187 {
4188 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4189 {
4190 device->SetStreamSourceFreq(i, 1);
4191 }
4192
4193 mInstancingEnabled = false;
4194 }
4195 }
4196
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004197 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4198 *(element++) = end;
4199
4200 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4201 {
4202 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4203 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4204 {
4205 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004206 if(entry->vertexDeclaration != mLastSetVDecl)
4207 {
4208 device->SetVertexDeclaration(entry->vertexDeclaration);
4209 mLastSetVDecl = entry->vertexDeclaration;
4210 }
4211
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004212 return GL_NO_ERROR;
4213 }
4214 }
4215
4216 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4217
4218 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4219 {
4220 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4221 {
4222 lastCache = &mVertexDeclCache[i];
4223 }
4224 }
4225
4226 if (lastCache->vertexDeclaration != NULL)
4227 {
4228 lastCache->vertexDeclaration->Release();
4229 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004230 // mLastSetVDecl is set to the replacement, so we don't have to worry
4231 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004232 }
4233
4234 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4235 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4236 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004237 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004238 lastCache->lruCount = ++mMaxLru;
4239
4240 return GL_NO_ERROR;
4241}
4242
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004243void VertexDeclarationCache::markStateDirty()
4244{
4245 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4246 {
4247 mAppliedVBs[i].serial = 0;
4248 }
4249
4250 mLastSetVDecl = NULL;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004251 mInstancingEnabled = true; // Forces it to be disabled when not used
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004252}
4253
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004254}
4255
4256extern "C"
4257{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004258gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004259{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004260 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004261}
4262
4263void glDestroyContext(gl::Context *context)
4264{
4265 delete context;
4266
4267 if (context == gl::getContext())
4268 {
4269 gl::makeCurrent(NULL, NULL, NULL);
4270 }
4271}
4272
4273void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4274{
4275 gl::makeCurrent(context, display, surface);
4276}
4277
4278gl::Context *glGetCurrentContext()
4279{
4280 return gl::getContext();
4281}
4282}