blob: 50d36ee69e8b1cf8714a8e59ed36c23d9a9d973f [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);
2335
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002336 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002337 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002338 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2339
2340 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002341 {
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002342 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002343 {
2344 GLenum wrapS = texture->getWrapS();
2345 GLenum wrapT = texture->getWrapT();
2346 GLenum minFilter = texture->getMinFilter();
2347 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002349 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2350 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002352 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002353 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2354 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002355 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2356 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002357 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002359 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002360 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002361 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002362 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002363 }
2364 else
2365 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002366 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002367 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002368
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002369 appliedTextureSerial[samplerIndex] = texture->getTextureSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002370 texture->resetDirty();
2371 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002373 else
2374 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002375 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002376 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002377 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002378 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002379 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002380 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002382
2383 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2384 {
2385 if (appliedTextureSerial[samplerIndex] != 0)
2386 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002387 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002388 appliedTextureSerial[samplerIndex] = 0;
2389 }
2390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391}
2392
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002393void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2394 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002396 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002397
2398 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2399 {
2400 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2401 }
2402
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002403 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2404 {
2405 return error(GL_INVALID_OPERATION);
2406 }
2407
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002408 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2409 // sized query sanity check
2410 if (bufSize)
2411 {
2412 int requiredSize = outputPitch * height;
2413 if (requiredSize > *bufSize)
2414 {
2415 return error(GL_INVALID_OPERATION);
2416 }
2417 }
2418
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002420 if (!renderTarget)
2421 {
2422 return; // Context must be lost, return silently
2423 }
2424
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425 D3DSURFACE_DESC desc;
2426 renderTarget->GetDesc(&desc);
2427
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2429 {
2430 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002431 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002432 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 }
2434
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002435 HRESULT result;
2436 IDirect3DSurface9 *systemSurface = NULL;
2437 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002438 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002439 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2440 if (directToPixels)
2441 {
2442 // Use the pixels ptr as a shared handle to write directly into client's memory
2443 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2444 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2445 if (FAILED(result))
2446 {
2447 // Try again without the shared handle
2448 directToPixels = false;
2449 }
2450 }
2451
2452 if (!directToPixels)
2453 {
2454 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2455 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2456 if (FAILED(result))
2457 {
2458 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2459 return error(GL_OUT_OF_MEMORY);
2460 }
2461 }
2462
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002463 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002464 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002465 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002466
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 if (FAILED(result))
2468 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469 systemSurface->Release();
2470
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002471 // It turns out that D3D will sometimes produce more error
2472 // codes than those documented.
2473 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002474 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002475 else
2476 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002477 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002478 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002479 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002480
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 }
2482
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002483 if (directToPixels)
2484 {
2485 systemSurface->Release();
2486 return;
2487 }
2488
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002490 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2491 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2492 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2493 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2494 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002495
2496 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2497
2498 if (FAILED(result))
2499 {
2500 UNREACHABLE();
2501 systemSurface->Release();
2502
2503 return; // No sensible error to generate
2504 }
2505
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002507 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002508
2509 unsigned char *source;
2510 int inputPitch;
2511 if (getPackReverseRowOrder())
2512 {
2513 source = (unsigned char*)lock.pBits;
2514 inputPitch = lock.Pitch;
2515 }
2516 else
2517 {
2518 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2519 inputPitch = -lock.Pitch;
2520 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002521
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002522 for (int j = 0; j < rect.bottom - rect.top; j++)
2523 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002524 if (desc.Format == D3DFMT_A8R8G8B8 &&
2525 format == GL_BGRA_EXT &&
2526 type == GL_UNSIGNED_BYTE)
2527 {
2528 // Fast path for EXT_read_format_bgra, given
2529 // an RGBA source buffer. Note that buffers with no
2530 // alpha go through the slow path below.
2531 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002532 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002533 (rect.right - rect.left) * 4);
2534 continue;
2535 }
2536
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537 for (int i = 0; i < rect.right - rect.left; i++)
2538 {
2539 float r;
2540 float g;
2541 float b;
2542 float a;
2543
2544 switch (desc.Format)
2545 {
2546 case D3DFMT_R5G6B5:
2547 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002548 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002549
2550 a = 1.0f;
2551 b = (rgb & 0x001F) * (1.0f / 0x001F);
2552 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2553 r = (rgb & 0xF800) * (1.0f / 0xF800);
2554 }
2555 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556 case D3DFMT_A1R5G5B5:
2557 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002558 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559
2560 a = (argb & 0x8000) ? 1.0f : 0.0f;
2561 b = (argb & 0x001F) * (1.0f / 0x001F);
2562 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2563 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2564 }
2565 break;
2566 case D3DFMT_A8R8G8B8:
2567 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002568 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569
2570 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2571 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2572 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2573 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2574 }
2575 break;
2576 case D3DFMT_X8R8G8B8:
2577 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002578 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579
2580 a = 1.0f;
2581 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2582 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2583 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2584 }
2585 break;
2586 case D3DFMT_A2R10G10B10:
2587 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002588 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589
2590 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2591 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2592 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2593 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2594 }
2595 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002596 case D3DFMT_A32B32G32R32F:
2597 {
2598 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002599 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2600 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2601 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2602 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002603 }
2604 break;
2605 case D3DFMT_A16B16G16R16F:
2606 {
2607 // float formats in D3D are stored rgba, rather than the other way round
2608 float abgr[4];
2609
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002610 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002611
2612 a = abgr[3];
2613 b = abgr[2];
2614 g = abgr[1];
2615 r = abgr[0];
2616 }
2617 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002618 default:
2619 UNIMPLEMENTED(); // FIXME
2620 UNREACHABLE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002621 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622 }
2623
2624 switch (format)
2625 {
2626 case GL_RGBA:
2627 switch (type)
2628 {
2629 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002630 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2631 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2632 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2633 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634 break;
2635 default: UNREACHABLE();
2636 }
2637 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002638 case GL_BGRA_EXT:
2639 switch (type)
2640 {
2641 case GL_UNSIGNED_BYTE:
2642 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2643 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2644 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2645 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2646 break;
2647 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2648 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2649 // this type is packed as follows:
2650 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2651 // --------------------------------------------------------------------------------
2652 // | 4th | 3rd | 2nd | 1st component |
2653 // --------------------------------------------------------------------------------
2654 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2655 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2656 ((unsigned short)(15 * a + 0.5f) << 12)|
2657 ((unsigned short)(15 * r + 0.5f) << 8) |
2658 ((unsigned short)(15 * g + 0.5f) << 4) |
2659 ((unsigned short)(15 * b + 0.5f) << 0);
2660 break;
2661 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2662 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2663 // this type is packed as follows:
2664 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2665 // --------------------------------------------------------------------------------
2666 // | 4th | 3rd | 2nd | 1st component |
2667 // --------------------------------------------------------------------------------
2668 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2669 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2670 ((unsigned short)( a + 0.5f) << 15) |
2671 ((unsigned short)(31 * r + 0.5f) << 10) |
2672 ((unsigned short)(31 * g + 0.5f) << 5) |
2673 ((unsigned short)(31 * b + 0.5f) << 0);
2674 break;
2675 default: UNREACHABLE();
2676 }
2677 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002678 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679 switch (type)
2680 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002681 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002682 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2683 ((unsigned short)(31 * b + 0.5f) << 0) |
2684 ((unsigned short)(63 * g + 0.5f) << 5) |
2685 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686 break;
2687 default: UNREACHABLE();
2688 }
2689 break;
2690 default: UNREACHABLE();
2691 }
2692 }
2693 }
2694
2695 systemSurface->UnlockRect();
2696
2697 systemSurface->Release();
2698}
2699
2700void Context::clear(GLbitfield mask)
2701{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002702 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002703
2704 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2705 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002706 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002707 }
2708
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002709 DWORD flags = 0;
2710
2711 if (mask & GL_COLOR_BUFFER_BIT)
2712 {
2713 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002714
2715 if (framebufferObject->getColorbufferType() != GL_NONE)
2716 {
2717 flags |= D3DCLEAR_TARGET;
2718 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719 }
2720
2721 if (mask & GL_DEPTH_BUFFER_BIT)
2722 {
2723 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002724 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725 {
2726 flags |= D3DCLEAR_ZBUFFER;
2727 }
2728 }
2729
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730 GLuint stencilUnmasked = 0x0;
2731
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002732 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002733 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002735 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002736 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002737 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002738 if (!depthStencil)
2739 {
2740 ERR("Depth stencil pointer unexpectedly null.");
2741 return;
2742 }
2743
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002744 D3DSURFACE_DESC desc;
2745 depthStencil->GetDesc(&desc);
2746
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002747 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002748 stencilUnmasked = (0x1 << stencilSize) - 1;
2749
2750 if (stencilUnmasked != 0x0)
2751 {
2752 flags |= D3DCLEAR_STENCIL;
2753 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002754 }
2755 }
2756
2757 if (mask != 0)
2758 {
2759 return error(GL_INVALID_VALUE);
2760 }
2761
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002762 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2763 {
2764 return;
2765 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002767 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002768 unorm<8>(mState.colorClearValue.red),
2769 unorm<8>(mState.colorClearValue.green),
2770 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002771 float depth = clamp01(mState.depthClearValue);
2772 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002773
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002774 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775
2776 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002777 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002779 !(mState.colorMaskRed && mState.colorMaskGreen &&
2780 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002781
2782 if (needMaskedColorClear || needMaskedStencilClear)
2783 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002784 // State which is altered in all paths from this point to the clear call is saved.
2785 // State which is altered in only some paths will be flagged dirty in the case that
2786 // that path is taken.
2787 HRESULT hr;
2788 if (mMaskedClearSavedState == NULL)
2789 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002790 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002791 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2792
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002793 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2794 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2795 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2796 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2797 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2798 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2799 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2800 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2801 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2802 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2803 mDevice->SetPixelShader(NULL);
2804 mDevice->SetVertexShader(NULL);
2805 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2806 mDevice->SetStreamSource(0, NULL, 0, 0);
2807 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2808 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2809 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2810 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2811 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2812 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2813 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002814
2815 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2816 {
2817 mDevice->SetStreamSourceFreq(i, 1);
2818 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002819
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002820 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002821 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2822 }
2823
2824 ASSERT(mMaskedClearSavedState != NULL);
2825
2826 if (mMaskedClearSavedState != NULL)
2827 {
2828 hr = mMaskedClearSavedState->Capture();
2829 ASSERT(SUCCEEDED(hr));
2830 }
2831
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002832 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2833 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2834 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2835 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2836 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2837 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2838 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2839 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002840
2841 if (flags & D3DCLEAR_TARGET)
2842 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002843 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002844 }
2845 else
2846 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002847 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002848 }
2849
2850 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2851 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002852 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2853 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2854 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2855 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2856 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2857 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2858 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2859 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002860 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002861 }
2862 else
2863 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002864 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002865 }
2866
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002867 mDevice->SetPixelShader(NULL);
2868 mDevice->SetVertexShader(NULL);
2869 mDevice->SetFVF(D3DFVF_XYZRHW);
2870 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2871 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2872 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2873 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2874 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2875 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2876 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002877
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002878 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2879 {
2880 mDevice->SetStreamSourceFreq(i, 1);
2881 }
2882
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002883 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2884 quad[0][0] = -0.5f;
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002885 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002886 quad[0][2] = 0.0f;
2887 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002888
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002889 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2890 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002891 quad[1][2] = 0.0f;
2892 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002893
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002894 quad[2][0] = -0.5f;
2895 quad[2][1] = -0.5f;
2896 quad[2][2] = 0.0f;
2897 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002898
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002899 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002900 quad[3][1] = -0.5f;
2901 quad[3][2] = 0.0f;
2902 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002903
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002904 mDisplay->startScene();
2905 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002906
2907 if (flags & D3DCLEAR_ZBUFFER)
2908 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002909 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2910 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2911 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002912 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002913
2914 if (mMaskedClearSavedState != NULL)
2915 {
2916 mMaskedClearSavedState->Apply();
2917 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002918 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002919 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002920 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002921 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002922 }
2923}
2924
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002925void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002926{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002927 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002928 {
2929 return error(GL_INVALID_OPERATION);
2930 }
2931
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002932 D3DPRIMITIVETYPE primitiveType;
2933 int primitiveCount;
2934
2935 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2936 return error(GL_INVALID_ENUM);
2937
2938 if (primitiveCount <= 0)
2939 {
2940 return;
2941 }
2942
2943 if (!applyRenderTarget(false))
2944 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002945 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002946 }
2947
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002948 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002949
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002950 GLsizei repeatDraw = 1;
2951 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002952 if (err != GL_NO_ERROR)
2953 {
2954 return error(err);
2955 }
2956
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002957 applyShaders();
2958 applyTextures();
2959
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002960 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002961 {
2962 return error(GL_INVALID_OPERATION);
2963 }
2964
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002965 if (!cullSkipsDraw(mode))
2966 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002967 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002968
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002969 if (mode == GL_LINE_LOOP)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002970 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002971 drawLineLoop(count, GL_NONE, NULL, 0);
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002972 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002973 else if (instances > 0)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002974 {
2975 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
2976 if (countingIB)
2977 {
2978 if (mAppliedIBSerial != countingIB->getSerial())
2979 {
2980 mDevice->SetIndices(countingIB->getBuffer());
2981 mAppliedIBSerial = countingIB->getSerial();
2982 }
2983
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002984 for (int i = 0; i < repeatDraw; i++)
2985 {
2986 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
2987 }
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002988 }
2989 else
2990 {
2991 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
2992 return error(GL_OUT_OF_MEMORY);
2993 }
2994 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002995 else // Regular case
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002996 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002997 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002998 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002999 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000}
3001
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00003002void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003003{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003004 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003005 {
3006 return error(GL_INVALID_OPERATION);
3007 }
3008
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003009 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003010 {
3011 return error(GL_INVALID_OPERATION);
3012 }
3013
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003014 D3DPRIMITIVETYPE primitiveType;
3015 int primitiveCount;
3016
3017 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3018 return error(GL_INVALID_ENUM);
3019
3020 if (primitiveCount <= 0)
3021 {
3022 return;
3023 }
3024
3025 if (!applyRenderTarget(false))
3026 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00003027 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003028 }
3029
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003030 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00003031
3032 TranslatedIndexData indexInfo;
3033 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3034 if (err != GL_NO_ERROR)
3035 {
3036 return error(err);
3037 }
3038
daniel@transgaming.com83921382011-01-08 05:46:00 +00003039 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003040 GLsizei repeatDraw = 1;
3041 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00003042 if (err != GL_NO_ERROR)
3043 {
3044 return error(err);
3045 }
3046
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003047 applyShaders();
3048 applyTextures();
3049
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00003050 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003051 {
3052 return error(GL_INVALID_OPERATION);
3053 }
3054
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003055 if (!cullSkipsDraw(mode))
3056 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003057 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003058
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003059 if (mode == GL_LINE_LOOP)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003060 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003061 drawLineLoop(count, type, indices, indexInfo.minIndex);
3062 }
3063 else
3064 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003065 for (int i = 0; i < repeatDraw; i++)
3066 {
3067 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3068 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003069 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003070 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003071}
3072
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00003073// Implements glFlush when block is false, glFinish when block is true
3074void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003075{
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +00003076 mDisplay->sync(block);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003077}
3078
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003079void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003080{
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003081 // Get the raw indices for an indexed draw
3082 if (type != GL_NONE && mState.elementArrayBuffer.get())
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003083 {
3084 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3085 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3086 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3087 }
3088
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003089 UINT startIndex = 0;
3090 bool succeeded = false;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003091
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003092 if (supports32bitIndices())
3093 {
3094 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3095
3096 if (!mLineLoopIB)
3097 {
3098 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3099 }
3100
3101 if (mLineLoopIB)
3102 {
3103 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3104
3105 UINT offset = 0;
3106 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3107 startIndex = offset / 4;
3108
3109 if (data)
3110 {
3111 switch (type)
3112 {
3113 case GL_NONE: // Non-indexed draw
3114 for (int i = 0; i < count; i++)
3115 {
3116 data[i] = i;
3117 }
3118 data[count] = 0;
3119 break;
3120 case GL_UNSIGNED_BYTE:
3121 for (int i = 0; i < count; i++)
3122 {
3123 data[i] = static_cast<const GLubyte*>(indices)[i];
3124 }
3125 data[count] = static_cast<const GLubyte*>(indices)[0];
3126 break;
3127 case GL_UNSIGNED_SHORT:
3128 for (int i = 0; i < count; i++)
3129 {
3130 data[i] = static_cast<const GLushort*>(indices)[i];
3131 }
3132 data[count] = static_cast<const GLushort*>(indices)[0];
3133 break;
3134 case GL_UNSIGNED_INT:
3135 for (int i = 0; i < count; i++)
3136 {
3137 data[i] = static_cast<const GLuint*>(indices)[i];
3138 }
3139 data[count] = static_cast<const GLuint*>(indices)[0];
3140 break;
3141 default: UNREACHABLE();
3142 }
3143
3144 mLineLoopIB->unmap();
3145 succeeded = true;
3146 }
3147 }
3148 }
3149 else
3150 {
3151 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3152
3153 if (!mLineLoopIB)
3154 {
3155 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3156 }
3157
3158 if (mLineLoopIB)
3159 {
3160 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3161
3162 UINT offset = 0;
3163 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3164 startIndex = offset / 2;
3165
3166 if (data)
3167 {
3168 switch (type)
3169 {
3170 case GL_NONE: // Non-indexed draw
3171 for (int i = 0; i < count; i++)
3172 {
3173 data[i] = i;
3174 }
3175 data[count] = 0;
3176 break;
3177 case GL_UNSIGNED_BYTE:
3178 for (int i = 0; i < count; i++)
3179 {
3180 data[i] = static_cast<const GLubyte*>(indices)[i];
3181 }
3182 data[count] = static_cast<const GLubyte*>(indices)[0];
3183 break;
3184 case GL_UNSIGNED_SHORT:
3185 for (int i = 0; i < count; i++)
3186 {
3187 data[i] = static_cast<const GLushort*>(indices)[i];
3188 }
3189 data[count] = static_cast<const GLushort*>(indices)[0];
3190 break;
3191 case GL_UNSIGNED_INT:
3192 for (int i = 0; i < count; i++)
3193 {
3194 data[i] = static_cast<const GLuint*>(indices)[i];
3195 }
3196 data[count] = static_cast<const GLuint*>(indices)[0];
3197 break;
3198 default: UNREACHABLE();
3199 }
3200
3201 mLineLoopIB->unmap();
3202 succeeded = true;
3203 }
3204 }
3205 }
3206
3207 if (succeeded)
3208 {
3209 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3210 {
3211 mDevice->SetIndices(mLineLoopIB->getBuffer());
3212 mAppliedIBSerial = mLineLoopIB->getSerial();
3213 }
3214
3215 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3216 }
3217 else
3218 {
3219 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3220 return error(GL_OUT_OF_MEMORY);
3221 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003222}
3223
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003224void Context::recordInvalidEnum()
3225{
3226 mInvalidEnum = true;
3227}
3228
3229void Context::recordInvalidValue()
3230{
3231 mInvalidValue = true;
3232}
3233
3234void Context::recordInvalidOperation()
3235{
3236 mInvalidOperation = true;
3237}
3238
3239void Context::recordOutOfMemory()
3240{
3241 mOutOfMemory = true;
3242}
3243
3244void Context::recordInvalidFramebufferOperation()
3245{
3246 mInvalidFramebufferOperation = true;
3247}
3248
3249// Get one of the recorded errors and clear its flag, if any.
3250// [OpenGL ES 2.0.24] section 2.5 page 13.
3251GLenum Context::getError()
3252{
3253 if (mInvalidEnum)
3254 {
3255 mInvalidEnum = false;
3256
3257 return GL_INVALID_ENUM;
3258 }
3259
3260 if (mInvalidValue)
3261 {
3262 mInvalidValue = false;
3263
3264 return GL_INVALID_VALUE;
3265 }
3266
3267 if (mInvalidOperation)
3268 {
3269 mInvalidOperation = false;
3270
3271 return GL_INVALID_OPERATION;
3272 }
3273
3274 if (mOutOfMemory)
3275 {
3276 mOutOfMemory = false;
3277
3278 return GL_OUT_OF_MEMORY;
3279 }
3280
3281 if (mInvalidFramebufferOperation)
3282 {
3283 mInvalidFramebufferOperation = false;
3284
3285 return GL_INVALID_FRAMEBUFFER_OPERATION;
3286 }
3287
3288 return GL_NO_ERROR;
3289}
3290
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003291GLenum Context::getResetStatus()
3292{
3293 if (mResetStatus == GL_NO_ERROR)
3294 {
3295 bool lost = mDisplay->testDeviceLost();
3296
3297 if (lost)
3298 {
3299 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3300 }
3301 }
3302
3303 GLenum status = mResetStatus;
3304
3305 if (mResetStatus != GL_NO_ERROR)
3306 {
3307 if (mDisplay->testDeviceResettable())
3308 {
3309 mResetStatus = GL_NO_ERROR;
3310 }
3311 }
3312
3313 return status;
3314}
3315
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003316bool Context::isResetNotificationEnabled()
3317{
3318 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3319}
3320
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003321bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003322{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003323 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003324}
3325
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003326int Context::getMaximumVaryingVectors() const
3327{
3328 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3329}
3330
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003331unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003332{
3333 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3334}
3335
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003336unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003337{
3338 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3339}
3340
daniel@transgaming.com458da142010-11-28 02:03:02 +00003341int Context::getMaximumFragmentUniformVectors() const
3342{
3343 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3344}
3345
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003346int Context::getMaxSupportedSamples() const
3347{
3348 return mMaxSupportedSamples;
3349}
3350
3351int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3352{
3353 if (requested == 0)
3354 {
3355 return requested;
3356 }
3357
3358 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3359 if (itr == mMultiSampleSupport.end())
3360 {
3361 return -1;
3362 }
3363
3364 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3365 {
3366 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3367 {
3368 return i;
3369 }
3370 }
3371
3372 return -1;
3373}
3374
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003375bool Context::supportsEventQueries() const
3376{
3377 return mSupportsEventQueries;
3378}
3379
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003380bool Context::supportsOcclusionQueries() const
3381{
3382 return mSupportsOcclusionQueries;
3383}
3384
gman@chromium.org50c526d2011-08-10 05:19:44 +00003385bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003386{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003387 return mSupportsDXT1Textures;
3388}
3389
3390bool Context::supportsDXT3Textures() const
3391{
3392 return mSupportsDXT3Textures;
3393}
3394
3395bool Context::supportsDXT5Textures() const
3396{
3397 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003398}
3399
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003400bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003401{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003402 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003403}
3404
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003405bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003406{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003407 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003408}
3409
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003410bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003411{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003412 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003413}
3414
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003415bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003416{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003417 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003418}
3419
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003420bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003421{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003422 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003423}
3424
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003425bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003426{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003427 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003428}
3429
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003430int Context::getMaximumRenderbufferDimension() const
3431{
3432 return mMaxRenderbufferDimension;
3433}
3434
3435int Context::getMaximumTextureDimension() const
3436{
3437 return mMaxTextureDimension;
3438}
3439
3440int Context::getMaximumCubeTextureDimension() const
3441{
3442 return mMaxCubeTextureDimension;
3443}
3444
3445int Context::getMaximumTextureLevel() const
3446{
3447 return mMaxTextureLevel;
3448}
3449
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003450bool Context::supportsLuminanceTextures() const
3451{
3452 return mSupportsLuminanceTextures;
3453}
3454
3455bool Context::supportsLuminanceAlphaTextures() const
3456{
3457 return mSupportsLuminanceAlphaTextures;
3458}
3459
daniel@transgaming.com83921382011-01-08 05:46:00 +00003460bool Context::supports32bitIndices() const
3461{
3462 return mSupports32bitIndices;
3463}
3464
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003465bool Context::supportsNonPower2Texture() const
3466{
3467 return mSupportsNonPower2Texture;
3468}
3469
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003470bool Context::supportsInstancing() const
3471{
3472 return mSupportsInstancing;
3473}
3474
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003475void Context::detachBuffer(GLuint buffer)
3476{
3477 // [OpenGL ES 2.0.24] section 2.9 page 22:
3478 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3479 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3480
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003481 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003482 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003483 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003484 }
3485
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003486 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003487 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003488 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003489 }
3490
3491 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3492 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003493 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003494 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003495 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003496 }
3497 }
3498}
3499
3500void Context::detachTexture(GLuint texture)
3501{
3502 // [OpenGL ES 2.0.24] section 3.8 page 84:
3503 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3504 // rebound to texture object zero
3505
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003506 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003507 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003508 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003509 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003510 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003511 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003512 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003513 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003514 }
3515 }
3516
3517 // [OpenGL ES 2.0.24] section 4.4 page 112:
3518 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3519 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3520 // image was attached in the currently bound framebuffer.
3521
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003522 Framebuffer *readFramebuffer = getReadFramebuffer();
3523 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003524
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003525 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003526 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003527 readFramebuffer->detachTexture(texture);
3528 }
3529
3530 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3531 {
3532 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003533 }
3534}
3535
3536void Context::detachFramebuffer(GLuint framebuffer)
3537{
3538 // [OpenGL ES 2.0.24] section 4.4 page 107:
3539 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3540 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3541
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003542 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003543 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003544 bindReadFramebuffer(0);
3545 }
3546
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003547 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003548 {
3549 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003550 }
3551}
3552
3553void Context::detachRenderbuffer(GLuint renderbuffer)
3554{
3555 // [OpenGL ES 2.0.24] section 4.4 page 109:
3556 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3557 // had been executed with the target RENDERBUFFER and name of zero.
3558
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003559 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003560 {
3561 bindRenderbuffer(0);
3562 }
3563
3564 // [OpenGL ES 2.0.24] section 4.4 page 111:
3565 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3566 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3567 // point to which this image was attached in the currently bound framebuffer.
3568
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003569 Framebuffer *readFramebuffer = getReadFramebuffer();
3570 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003571
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003572 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003573 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003574 readFramebuffer->detachRenderbuffer(renderbuffer);
3575 }
3576
3577 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3578 {
3579 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003580 }
3581}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003582
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003583Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003584{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003585 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003586
3587 if (t == NULL)
3588 {
3589 static const GLubyte color[] = { 0, 0, 0, 255 };
3590
3591 switch (type)
3592 {
3593 default:
3594 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003595 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003596
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003597 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003598 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003599 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003600 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003601 t = incomplete2d;
3602 }
3603 break;
3604
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003605 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003606 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003607 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003608
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003609 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3610 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3611 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3612 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3613 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3614 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003615
3616 t = incompleteCube;
3617 }
3618 break;
3619 }
3620
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003621 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003622 }
3623
3624 return t;
3625}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003626
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003627bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003628{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003629 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003630}
3631
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003632bool Context::isTriangleMode(GLenum drawMode)
3633{
3634 switch (drawMode)
3635 {
3636 case GL_TRIANGLES:
3637 case GL_TRIANGLE_FAN:
3638 case GL_TRIANGLE_STRIP:
3639 return true;
3640 case GL_POINTS:
3641 case GL_LINES:
3642 case GL_LINE_LOOP:
3643 case GL_LINE_STRIP:
3644 return false;
3645 default: UNREACHABLE();
3646 }
3647
3648 return false;
3649}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003650
3651void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3652{
3653 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3654
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003655 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3656 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3657 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3658 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003659
daniel@transgaming.com83921382011-01-08 05:46:00 +00003660 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003661}
3662
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00003663void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3664{
3665 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3666
3667 mState.vertexAttribute[index].mDivisor = divisor;
3668}
3669
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003670// keep list sorted in following order
3671// OES extensions
3672// EXT extensions
3673// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003674void Context::initExtensionString()
3675{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003676 mExtensionString = "";
3677
3678 // OES extensions
3679 if (supports32bitIndices())
3680 {
3681 mExtensionString += "GL_OES_element_index_uint ";
3682 }
3683
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003684 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003685 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003686 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003687
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003688 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003689 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003690 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003691 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003692 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003693 {
3694 mExtensionString += "GL_OES_texture_half_float_linear ";
3695 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003696 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003697 {
3698 mExtensionString += "GL_OES_texture_float ";
3699 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003700 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003701 {
3702 mExtensionString += "GL_OES_texture_float_linear ";
3703 }
3704
3705 if (supportsNonPower2Texture())
3706 {
3707 mExtensionString += "GL_OES_texture_npot ";
3708 }
3709
3710 // Multi-vendor (EXT) extensions
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003711 if (supportsOcclusionQueries())
3712 {
3713 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3714 }
3715
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003716 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003717 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003718
gman@chromium.org50c526d2011-08-10 05:19:44 +00003719 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003720 {
3721 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3722 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003723
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003724 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.comdf363722011-12-16 23:29:53 +00003725 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003726
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003727 // ANGLE-specific extensions
3728 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003729 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003730 {
3731 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3732 }
3733
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003734 if (supportsInstancing())
daniel@transgaming.comdce02fd2012-01-27 15:39:51 +00003735 {
3736 mExtensionString += "GL_ANGLE_instanced_arrays ";
3737 }
3738
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003739 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3740
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003741 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003742 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003743 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3744 }
3745 if (supportsDXT5Textures())
3746 {
3747 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003748 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003749
3750 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003751 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003752
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003753 // Other vendor-specific extensions
3754 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003755 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003756 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003757 }
3758
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003759 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3760 if (end != std::string::npos)
3761 {
3762 mExtensionString.resize(end+1);
3763 }
3764}
3765
3766const char *Context::getExtensionString() const
3767{
3768 return mExtensionString.c_str();
3769}
3770
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003771void Context::initRendererString()
3772{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003773 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003774
3775 mRendererString = "ANGLE (";
3776 mRendererString += identifier->Description;
3777 mRendererString += ")";
3778}
3779
3780const char *Context::getRendererString() const
3781{
3782 return mRendererString.c_str();
3783}
3784
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003785void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3786 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3787 GLbitfield mask)
3788{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003789 Framebuffer *readFramebuffer = getReadFramebuffer();
3790 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3791
3792 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3793 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3794 {
3795 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3796 }
3797
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003798 if (drawFramebuffer->getSamples() != 0)
3799 {
3800 return error(GL_INVALID_OPERATION);
3801 }
3802
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003803 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3804 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3805 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3806 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3807
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003808 RECT sourceRect;
3809 RECT destRect;
3810
3811 if (srcX0 < srcX1)
3812 {
3813 sourceRect.left = srcX0;
3814 sourceRect.right = srcX1;
3815 destRect.left = dstX0;
3816 destRect.right = dstX1;
3817 }
3818 else
3819 {
3820 sourceRect.left = srcX1;
3821 destRect.left = dstX1;
3822 sourceRect.right = srcX0;
3823 destRect.right = dstX0;
3824 }
3825
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003826 if (srcY0 < srcY1)
3827 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003828 sourceRect.top = readBufferHeight - srcY1;
3829 destRect.top = drawBufferHeight - dstY1;
3830 sourceRect.bottom = readBufferHeight - srcY0;
3831 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003832 }
3833 else
3834 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003835 sourceRect.top = readBufferHeight - srcY0;
3836 destRect.top = drawBufferHeight - dstY0;
3837 sourceRect.bottom = readBufferHeight - srcY1;
3838 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003839 }
3840
3841 RECT sourceScissoredRect = sourceRect;
3842 RECT destScissoredRect = destRect;
3843
3844 if (mState.scissorTest)
3845 {
3846 // Only write to parts of the destination framebuffer which pass the scissor test
3847 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3848 // rect will be checked against scissorY, rather than the bottom.
3849 if (destRect.left < mState.scissorX)
3850 {
3851 int xDiff = mState.scissorX - destRect.left;
3852 destScissoredRect.left = mState.scissorX;
3853 sourceScissoredRect.left += xDiff;
3854 }
3855
3856 if (destRect.right > mState.scissorX + mState.scissorWidth)
3857 {
3858 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3859 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3860 sourceScissoredRect.right -= xDiff;
3861 }
3862
3863 if (destRect.top < mState.scissorY)
3864 {
3865 int yDiff = mState.scissorY - destRect.top;
3866 destScissoredRect.top = mState.scissorY;
3867 sourceScissoredRect.top += yDiff;
3868 }
3869
3870 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3871 {
3872 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3873 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3874 sourceScissoredRect.bottom -= yDiff;
3875 }
3876 }
3877
3878 bool blitRenderTarget = false;
3879 bool blitDepthStencil = false;
3880
3881 RECT sourceTrimmedRect = sourceScissoredRect;
3882 RECT destTrimmedRect = destScissoredRect;
3883
3884 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3885 // the actual draw and read surfaces.
3886 if (sourceTrimmedRect.left < 0)
3887 {
3888 int xDiff = 0 - sourceTrimmedRect.left;
3889 sourceTrimmedRect.left = 0;
3890 destTrimmedRect.left += xDiff;
3891 }
3892
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003893 if (sourceTrimmedRect.right > readBufferWidth)
3894 {
3895 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3896 sourceTrimmedRect.right = readBufferWidth;
3897 destTrimmedRect.right -= xDiff;
3898 }
3899
3900 if (sourceTrimmedRect.top < 0)
3901 {
3902 int yDiff = 0 - sourceTrimmedRect.top;
3903 sourceTrimmedRect.top = 0;
3904 destTrimmedRect.top += yDiff;
3905 }
3906
3907 if (sourceTrimmedRect.bottom > readBufferHeight)
3908 {
3909 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3910 sourceTrimmedRect.bottom = readBufferHeight;
3911 destTrimmedRect.bottom -= yDiff;
3912 }
3913
3914 if (destTrimmedRect.left < 0)
3915 {
3916 int xDiff = 0 - destTrimmedRect.left;
3917 destTrimmedRect.left = 0;
3918 sourceTrimmedRect.left += xDiff;
3919 }
3920
3921 if (destTrimmedRect.right > drawBufferWidth)
3922 {
3923 int xDiff = destTrimmedRect.right - drawBufferWidth;
3924 destTrimmedRect.right = drawBufferWidth;
3925 sourceTrimmedRect.right -= xDiff;
3926 }
3927
3928 if (destTrimmedRect.top < 0)
3929 {
3930 int yDiff = 0 - destTrimmedRect.top;
3931 destTrimmedRect.top = 0;
3932 sourceTrimmedRect.top += yDiff;
3933 }
3934
3935 if (destTrimmedRect.bottom > drawBufferHeight)
3936 {
3937 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3938 destTrimmedRect.bottom = drawBufferHeight;
3939 sourceTrimmedRect.bottom -= yDiff;
3940 }
3941
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003942 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003943 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3944 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3945 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3946 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003947 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3948 {
3949 partialBufferCopy = true;
3950 }
3951
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003952 if (mask & GL_COLOR_BUFFER_BIT)
3953 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003954 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3955 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3956 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3957 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3958 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003959 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3960 {
3961 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3962 return error(GL_INVALID_OPERATION);
3963 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003964
3965 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3966 {
3967 return error(GL_INVALID_OPERATION);
3968 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003969
3970 blitRenderTarget = true;
3971
3972 }
3973
3974 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3975 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003976 Renderbuffer *readDSBuffer = NULL;
3977 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003978
3979 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3980 // both a depth and stencil buffer, it will be the same buffer.
3981
3982 if (mask & GL_DEPTH_BUFFER_BIT)
3983 {
3984 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3985 {
3986 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3987 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3988 {
3989 return error(GL_INVALID_OPERATION);
3990 }
3991
3992 blitDepthStencil = true;
3993 readDSBuffer = readFramebuffer->getDepthbuffer();
3994 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3995 }
3996 }
3997
3998 if (mask & GL_STENCIL_BUFFER_BIT)
3999 {
4000 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4001 {
4002 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
4003 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
4004 {
4005 return error(GL_INVALID_OPERATION);
4006 }
4007
4008 blitDepthStencil = true;
4009 readDSBuffer = readFramebuffer->getStencilbuffer();
4010 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4011 }
4012 }
4013
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004014 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004015 {
4016 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4017 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4018 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004019
daniel@transgaming.com97446d22010-08-24 19:20:54 +00004020 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4021 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004022 {
4023 return error(GL_INVALID_OPERATION);
4024 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004025 }
4026
4027 if (blitRenderTarget || blitDepthStencil)
4028 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004029 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004030
4031 if (blitRenderTarget)
4032 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00004033 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4034 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4035
4036 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4037 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4038
4039 readRenderTarget->Release();
4040 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004041
4042 if (FAILED(result))
4043 {
4044 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4045 return;
4046 }
4047 }
4048
4049 if (blitDepthStencil)
4050 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004051 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004052
4053 if (FAILED(result))
4054 {
4055 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4056 return;
4057 }
4058 }
4059 }
4060}
4061
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004062VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4063{
4064 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4065 {
4066 mVertexDeclCache[i].vertexDeclaration = NULL;
4067 mVertexDeclCache[i].lruCount = 0;
4068 }
4069}
4070
4071VertexDeclarationCache::~VertexDeclarationCache()
4072{
4073 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4074 {
4075 if (mVertexDeclCache[i].vertexDeclaration)
4076 {
4077 mVertexDeclCache[i].vertexDeclaration->Release();
4078 }
4079 }
4080}
4081
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004082GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004083{
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004084 *repeatDraw = 1;
4085
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004086 int indexedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004087 int instancedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004088
4089 if (instances > 0)
4090 {
4091 // Find an indexed attribute to be mapped to D3D stream 0
4092 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4093 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004094 if (attributes[i].active)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004095 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004096 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4097 {
4098 if (attributes[i].divisor == 0)
4099 {
4100 indexedAttribute = i;
4101 }
4102 }
4103 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4104 {
4105 if (attributes[i].divisor != 0)
4106 {
4107 instancedAttribute = i;
4108 }
4109 }
4110 else break; // Found both an indexed and instanced attribute
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004111 }
4112 }
4113
4114 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4115 {
4116 return GL_INVALID_OPERATION;
4117 }
4118 }
4119
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004120 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4121 D3DVERTEXELEMENT9 *element = &elements[0];
4122
4123 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4124 {
4125 if (attributes[i].active)
4126 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004127 int stream = i;
4128
4129 if (instances > 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004130 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004131 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4132 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004133 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004134 *repeatDraw = instances;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004135 }
4136 else
4137 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004138 if (i == indexedAttribute)
4139 {
4140 stream = 0;
4141 }
4142 else if (i == 0)
4143 {
4144 stream = indexedAttribute;
4145 }
4146
4147 UINT frequency = 1;
4148
4149 if (attributes[i].divisor == 0)
4150 {
4151 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4152 }
4153 else
4154 {
4155 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4156 }
4157
4158 device->SetStreamSourceFreq(stream, frequency);
4159 mInstancingEnabled = true;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004160 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004161 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004162
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004163 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4164 mAppliedVBs[stream].stride != attributes[i].stride ||
4165 mAppliedVBs[stream].offset != attributes[i].offset)
4166 {
4167 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4168 mAppliedVBs[stream].serial = attributes[i].serial;
4169 mAppliedVBs[stream].stride = attributes[i].stride;
4170 mAppliedVBs[stream].offset = attributes[i].offset;
4171 }
4172
4173 element->Stream = stream;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004174 element->Offset = 0;
4175 element->Type = attributes[i].type;
4176 element->Method = D3DDECLMETHOD_DEFAULT;
4177 element->Usage = D3DDECLUSAGE_TEXCOORD;
4178 element->UsageIndex = program->getSemanticIndex(i);
4179 element++;
4180 }
4181 }
4182
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004183 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004184 {
4185 if (mInstancingEnabled)
4186 {
4187 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4188 {
4189 device->SetStreamSourceFreq(i, 1);
4190 }
4191
4192 mInstancingEnabled = false;
4193 }
4194 }
4195
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004196 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4197 *(element++) = end;
4198
4199 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4200 {
4201 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4202 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4203 {
4204 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004205 if(entry->vertexDeclaration != mLastSetVDecl)
4206 {
4207 device->SetVertexDeclaration(entry->vertexDeclaration);
4208 mLastSetVDecl = entry->vertexDeclaration;
4209 }
4210
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004211 return GL_NO_ERROR;
4212 }
4213 }
4214
4215 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4216
4217 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4218 {
4219 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4220 {
4221 lastCache = &mVertexDeclCache[i];
4222 }
4223 }
4224
4225 if (lastCache->vertexDeclaration != NULL)
4226 {
4227 lastCache->vertexDeclaration->Release();
4228 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004229 // mLastSetVDecl is set to the replacement, so we don't have to worry
4230 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004231 }
4232
4233 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4234 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4235 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004236 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004237 lastCache->lruCount = ++mMaxLru;
4238
4239 return GL_NO_ERROR;
4240}
4241
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004242void VertexDeclarationCache::markStateDirty()
4243{
4244 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4245 {
4246 mAppliedVBs[i].serial = 0;
4247 }
4248
4249 mLastSetVDecl = NULL;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004250 mInstancingEnabled = true; // Forces it to be disabled when not used
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004251}
4252
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004253}
4254
4255extern "C"
4256{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004257gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004258{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004259 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004260}
4261
4262void glDestroyContext(gl::Context *context)
4263{
4264 delete context;
4265
4266 if (context == gl::getContext())
4267 {
4268 gl::makeCurrent(NULL, NULL, NULL);
4269 }
4270}
4271
4272void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4273{
4274 gl::makeCurrent(context, display, surface);
4275}
4276
4277gl::Context *glGetCurrentContext()
4278{
4279 return gl::getContext();
4280}
4281}