blob: 4d5f8a712edde9241f2cb9b7780d1f6f76552296 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
daniel@transgaming.com86bdb822012-01-20 18:24:39 +000025#include "libGLESv2/Query.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000026#include "libGLESv2/RenderBuffer.h"
27#include "libGLESv2/Shader.h"
28#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000029#include "libGLESv2/VertexDataManager.h"
30#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
daniel@transgaming.com86487c22010-03-11 19:41:43 +000032#undef near
33#undef far
34
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035namespace gl
36{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000037Context::Context(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000039 ASSERT(robustAccess == false); // Unimplemented
40
daniel@transgaming.comc941e252011-10-26 02:32:31 +000041 mDisplay = NULL;
42 mDevice = NULL;
43
benvanik@google.com1a233342011-04-28 19:44:39 +000044 mFenceHandleAllocator.setBaseHandle(0);
45
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000047
daniel@transgaming.com428d1582010-05-04 03:35:25 +000048 mState.depthClearValue = 1.0f;
49 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050
daniel@transgaming.com428d1582010-05-04 03:35:25 +000051 mState.cullFace = false;
52 mState.cullMode = GL_BACK;
53 mState.frontFace = GL_CCW;
54 mState.depthTest = false;
55 mState.depthFunc = GL_LESS;
56 mState.blend = false;
57 mState.sourceBlendRGB = GL_ONE;
58 mState.sourceBlendAlpha = GL_ONE;
59 mState.destBlendRGB = GL_ZERO;
60 mState.destBlendAlpha = GL_ZERO;
61 mState.blendEquationRGB = GL_FUNC_ADD;
62 mState.blendEquationAlpha = GL_FUNC_ADD;
63 mState.blendColor.red = 0;
64 mState.blendColor.green = 0;
65 mState.blendColor.blue = 0;
66 mState.blendColor.alpha = 0;
67 mState.stencilTest = false;
68 mState.stencilFunc = GL_ALWAYS;
69 mState.stencilRef = 0;
70 mState.stencilMask = -1;
71 mState.stencilWritemask = -1;
72 mState.stencilBackFunc = GL_ALWAYS;
73 mState.stencilBackRef = 0;
74 mState.stencilBackMask = - 1;
75 mState.stencilBackWritemask = -1;
76 mState.stencilFail = GL_KEEP;
77 mState.stencilPassDepthFail = GL_KEEP;
78 mState.stencilPassDepthPass = GL_KEEP;
79 mState.stencilBackFail = GL_KEEP;
80 mState.stencilBackPassDepthFail = GL_KEEP;
81 mState.stencilBackPassDepthPass = GL_KEEP;
82 mState.polygonOffsetFill = false;
83 mState.polygonOffsetFactor = 0.0f;
84 mState.polygonOffsetUnits = 0.0f;
85 mState.sampleAlphaToCoverage = false;
86 mState.sampleCoverage = false;
87 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000088 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.scissorTest = false;
90 mState.dither = true;
91 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000092 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093
daniel@transgaming.com428d1582010-05-04 03:35:25 +000094 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.viewportX = 0;
97 mState.viewportY = 0;
98 mState.viewportWidth = config->mDisplayMode.Width;
99 mState.viewportHeight = config->mDisplayMode.Height;
100 mState.zNear = 0.0f;
101 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000103 mState.scissorX = 0;
104 mState.scissorY = 0;
105 mState.scissorWidth = config->mDisplayMode.Width;
106 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000108 mState.colorMaskRed = true;
109 mState.colorMaskGreen = true;
110 mState.colorMaskBlue = true;
111 mState.colorMaskAlpha = true;
112 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000114 if (shareContext != NULL)
115 {
116 mResourceManager = shareContext->mResourceManager;
117 mResourceManager->addRef();
118 }
119 else
120 {
121 mResourceManager = new ResourceManager();
122 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000123
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124 // [OpenGL ES 2.0.24] section 3.7 page 83:
125 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
126 // and cube map texture state vectors respectively associated with them.
127 // In order that access to these initial textures not be lost, they are treated as texture
128 // objects all of whose names are 0.
129
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000130 mTexture2DZero.set(new Texture2D(0));
131 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000133 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000134 bindArrayBuffer(0);
135 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 bindTextureCubeMap(0);
137 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000138 bindReadFramebuffer(0);
139 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 bindRenderbuffer(0);
141
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000142 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000144 mState.packAlignment = 4;
145 mState.unpackAlignment = 4;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000146 mState.packReverseRowOrder = false;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000147
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000148 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000149 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000150 mBlit = NULL;
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +0000151 mLineLoopIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153 mInvalidEnum = false;
154 mInvalidValue = false;
155 mInvalidOperation = false;
156 mOutOfMemory = false;
157 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000158
159 mHasBeenCurrent = false;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000160 mContextLost = false;
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000161 mResetStatus = GL_NO_ERROR;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000162 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
163 mRobustAccess = robustAccess;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000164
gman@chromium.org50c526d2011-08-10 05:19:44 +0000165 mSupportsDXT1Textures = false;
166 mSupportsDXT3Textures = false;
167 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000168 mSupportsEventQueries = false;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000169 mSupportsOcclusionQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000170 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000171 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000172 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000173 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174}
175
176Context::~Context()
177{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000178 if (mState.currentProgram != 0)
179 {
180 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
181 if (programObject)
182 {
183 programObject->release();
184 }
185 mState.currentProgram = 0;
186 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000188 while (!mFramebufferMap.empty())
189 {
190 deleteFramebuffer(mFramebufferMap.begin()->first);
191 }
192
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000193 while (!mFenceMap.empty())
194 {
195 deleteFence(mFenceMap.begin()->first);
196 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000197
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000198 while (!mQueryMap.empty())
199 {
200 deleteQuery(mQueryMap.begin()->first);
201 }
202
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000203 while (!mMultiSampleSupport.empty())
204 {
205 delete [] mMultiSampleSupport.begin()->second;
206 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
207 }
208
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000209 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000211 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000212 {
213 mState.samplerTexture[type][sampler].set(NULL);
214 }
215 }
216
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000217 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000218 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000219 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000220 }
221
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000222 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
223 {
224 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
225 }
226
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000227 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
228 {
229 mState.activeQuery[i].set(NULL);
230 }
231
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000232 mState.arrayBuffer.set(NULL);
233 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000234 mState.renderbuffer.set(NULL);
235
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000236 mTexture2DZero.set(NULL);
237 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000239 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000240 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000241 delete mBlit;
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +0000242 delete mLineLoopIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000243
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000244 if (mMaskedClearSavedState)
245 {
246 mMaskedClearSavedState->Release();
247 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000248
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000249 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250}
251
252void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
253{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000254 mDisplay = display;
255 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000257 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000258 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000259 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000260
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000261 mVertexDataManager = new VertexDataManager(this, mDevice);
262 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000263 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000264
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +0000265 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000266 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
267 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +0000268 mSupportsInstancing = mDisplay->getInstancingSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000269
270 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
271 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
272 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
273 mMaxRenderbufferDimension = mMaxTextureDimension;
274 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
275 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
276 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
277
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000278 const D3DFORMAT renderBufferFormats[] =
279 {
280 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000281 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000282 D3DFMT_R5G6B5,
283 D3DFMT_D24S8
284 };
285
286 int max = 0;
287 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
288 {
289 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000290 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000291 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
292
293 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
294 {
295 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
296 {
297 max = j;
298 }
299 }
300 }
301
302 mMaxSupportedSamples = max;
303
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000304 mSupportsEventQueries = mDisplay->getEventQuerySupport();
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000305 mSupportsOcclusionQueries = mDisplay->getOcclusionQuerySupport();
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000306 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
307 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
308 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000309 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
310 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000311 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
312 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com1c49f792012-05-31 01:14:02 +0000313 mSupportsDepthTextures = mDisplay->getDepthTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000314
daniel@transgaming.com83921382011-01-08 05:46:00 +0000315 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
316
gman@chromium.org50c526d2011-08-10 05:19:44 +0000317 mNumCompressedTextureFormats = 0;
318 if (supportsDXT1Textures())
319 {
320 mNumCompressedTextureFormats += 2;
321 }
322 if (supportsDXT3Textures())
323 {
324 mNumCompressedTextureFormats += 1;
325 }
326 if (supportsDXT5Textures())
327 {
328 mNumCompressedTextureFormats += 1;
329 }
330
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000331 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000332 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000333
334 mState.viewportX = 0;
335 mState.viewportY = 0;
336 mState.viewportWidth = surface->getWidth();
337 mState.viewportHeight = surface->getHeight();
338
339 mState.scissorX = 0;
340 mState.scissorY = 0;
341 mState.scissorWidth = surface->getWidth();
342 mState.scissorHeight = surface->getHeight();
343
344 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000345 }
346
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
348 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000349 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000352 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000353 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354
355 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000357 if (defaultRenderTarget)
358 {
359 defaultRenderTarget->Release();
360 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000362 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000364 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000366
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000367 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368}
369
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000370// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000371void Context::markAllStateDirty()
372{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000373 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
374 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000375 mAppliedTextureSerialPS[t] = 0;
376 }
377
378 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
379 {
380 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000381 }
382
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000383 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000384 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000385 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000386 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000387 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000388 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000389 mViewportInitialized = false;
390 mRenderTargetDescInitialized = false;
391
392 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000393
394 mClearStateDirty = true;
395 mCullStateDirty = true;
396 mDepthStateDirty = true;
397 mMaskStateDirty = true;
398 mBlendStateDirty = true;
399 mStencilStateDirty = true;
400 mPolygonOffsetStateDirty = true;
401 mScissorStateDirty = true;
402 mSampleStateDirty = true;
403 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000404 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000405 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000406 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000407}
408
daniel@transgaming.com11399d52012-04-28 00:35:14 +0000409void Context::markDxUniformsDirty()
410{
411 mDxUniformsDirty = true;
412}
413
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000414void Context::markContextLost()
415{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000416 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
417 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000418 mContextLost = true;
419}
420
421bool Context::isContextLost()
422{
423 return mContextLost;
424}
425
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426void Context::setClearColor(float red, float green, float blue, float alpha)
427{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000428 mState.colorClearValue.red = red;
429 mState.colorClearValue.green = green;
430 mState.colorClearValue.blue = blue;
431 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432}
433
434void Context::setClearDepth(float depth)
435{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000436 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437}
438
439void Context::setClearStencil(int stencil)
440{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000441 mState.stencilClearValue = stencil;
442}
443
444void Context::setCullFace(bool enabled)
445{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000446 if (mState.cullFace != enabled)
447 {
448 mState.cullFace = enabled;
449 mCullStateDirty = true;
450 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000451}
452
453bool Context::isCullFaceEnabled() const
454{
455 return mState.cullFace;
456}
457
458void Context::setCullMode(GLenum mode)
459{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000460 if (mState.cullMode != mode)
461 {
462 mState.cullMode = mode;
463 mCullStateDirty = true;
464 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000465}
466
467void Context::setFrontFace(GLenum front)
468{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000469 if (mState.frontFace != front)
470 {
471 mState.frontFace = front;
472 mFrontFaceDirty = true;
473 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000474}
475
476void Context::setDepthTest(bool enabled)
477{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000478 if (mState.depthTest != enabled)
479 {
480 mState.depthTest = enabled;
481 mDepthStateDirty = true;
482 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000483}
484
485bool Context::isDepthTestEnabled() const
486{
487 return mState.depthTest;
488}
489
490void Context::setDepthFunc(GLenum depthFunc)
491{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000492 if (mState.depthFunc != depthFunc)
493 {
494 mState.depthFunc = depthFunc;
495 mDepthStateDirty = true;
496 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000497}
498
499void Context::setDepthRange(float zNear, float zFar)
500{
501 mState.zNear = zNear;
502 mState.zFar = zFar;
503}
504
505void Context::setBlend(bool enabled)
506{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000507 if (mState.blend != enabled)
508 {
509 mState.blend = enabled;
510 mBlendStateDirty = true;
511 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000512}
513
514bool Context::isBlendEnabled() const
515{
516 return mState.blend;
517}
518
519void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
520{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000521 if (mState.sourceBlendRGB != sourceRGB ||
522 mState.sourceBlendAlpha != sourceAlpha ||
523 mState.destBlendRGB != destRGB ||
524 mState.destBlendAlpha != destAlpha)
525 {
526 mState.sourceBlendRGB = sourceRGB;
527 mState.destBlendRGB = destRGB;
528 mState.sourceBlendAlpha = sourceAlpha;
529 mState.destBlendAlpha = destAlpha;
530 mBlendStateDirty = true;
531 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000532}
533
534void Context::setBlendColor(float red, float green, float blue, float alpha)
535{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000536 if (mState.blendColor.red != red ||
537 mState.blendColor.green != green ||
538 mState.blendColor.blue != blue ||
539 mState.blendColor.alpha != alpha)
540 {
541 mState.blendColor.red = red;
542 mState.blendColor.green = green;
543 mState.blendColor.blue = blue;
544 mState.blendColor.alpha = alpha;
545 mBlendStateDirty = true;
546 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000547}
548
549void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
550{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000551 if (mState.blendEquationRGB != rgbEquation ||
552 mState.blendEquationAlpha != alphaEquation)
553 {
554 mState.blendEquationRGB = rgbEquation;
555 mState.blendEquationAlpha = alphaEquation;
556 mBlendStateDirty = true;
557 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000558}
559
560void Context::setStencilTest(bool enabled)
561{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000562 if (mState.stencilTest != enabled)
563 {
564 mState.stencilTest = enabled;
565 mStencilStateDirty = true;
566 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000567}
568
569bool Context::isStencilTestEnabled() const
570{
571 return mState.stencilTest;
572}
573
574void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
575{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576 if (mState.stencilFunc != stencilFunc ||
577 mState.stencilRef != stencilRef ||
578 mState.stencilMask != stencilMask)
579 {
580 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000581 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000582 mState.stencilMask = stencilMask;
583 mStencilStateDirty = true;
584 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000585}
586
587void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
588{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000589 if (mState.stencilBackFunc != stencilBackFunc ||
590 mState.stencilBackRef != stencilBackRef ||
591 mState.stencilBackMask != stencilBackMask)
592 {
593 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000594 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 mState.stencilBackMask = stencilBackMask;
596 mStencilStateDirty = true;
597 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000598}
599
600void Context::setStencilWritemask(GLuint stencilWritemask)
601{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000602 if (mState.stencilWritemask != stencilWritemask)
603 {
604 mState.stencilWritemask = stencilWritemask;
605 mStencilStateDirty = true;
606 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000607}
608
609void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
610{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000611 if (mState.stencilBackWritemask != stencilBackWritemask)
612 {
613 mState.stencilBackWritemask = stencilBackWritemask;
614 mStencilStateDirty = true;
615 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000616}
617
618void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
619{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000620 if (mState.stencilFail != stencilFail ||
621 mState.stencilPassDepthFail != stencilPassDepthFail ||
622 mState.stencilPassDepthPass != stencilPassDepthPass)
623 {
624 mState.stencilFail = stencilFail;
625 mState.stencilPassDepthFail = stencilPassDepthFail;
626 mState.stencilPassDepthPass = stencilPassDepthPass;
627 mStencilStateDirty = true;
628 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000629}
630
631void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
632{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000633 if (mState.stencilBackFail != stencilBackFail ||
634 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
635 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
636 {
637 mState.stencilBackFail = stencilBackFail;
638 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
639 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
640 mStencilStateDirty = true;
641 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000642}
643
644void Context::setPolygonOffsetFill(bool enabled)
645{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000646 if (mState.polygonOffsetFill != enabled)
647 {
648 mState.polygonOffsetFill = enabled;
649 mPolygonOffsetStateDirty = true;
650 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000651}
652
653bool Context::isPolygonOffsetFillEnabled() const
654{
655 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000656
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000657}
658
659void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
660{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000661 if (mState.polygonOffsetFactor != factor ||
662 mState.polygonOffsetUnits != units)
663 {
664 mState.polygonOffsetFactor = factor;
665 mState.polygonOffsetUnits = units;
666 mPolygonOffsetStateDirty = true;
667 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000668}
669
670void Context::setSampleAlphaToCoverage(bool enabled)
671{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000672 if (mState.sampleAlphaToCoverage != enabled)
673 {
674 mState.sampleAlphaToCoverage = enabled;
675 mSampleStateDirty = true;
676 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000677}
678
679bool Context::isSampleAlphaToCoverageEnabled() const
680{
681 return mState.sampleAlphaToCoverage;
682}
683
684void Context::setSampleCoverage(bool enabled)
685{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000686 if (mState.sampleCoverage != enabled)
687 {
688 mState.sampleCoverage = enabled;
689 mSampleStateDirty = true;
690 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000691}
692
693bool Context::isSampleCoverageEnabled() const
694{
695 return mState.sampleCoverage;
696}
697
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000698void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000699{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000700 if (mState.sampleCoverageValue != value ||
701 mState.sampleCoverageInvert != invert)
702 {
703 mState.sampleCoverageValue = value;
704 mState.sampleCoverageInvert = invert;
705 mSampleStateDirty = true;
706 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000707}
708
709void Context::setScissorTest(bool enabled)
710{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000711 if (mState.scissorTest != enabled)
712 {
713 mState.scissorTest = enabled;
714 mScissorStateDirty = true;
715 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000716}
717
718bool Context::isScissorTestEnabled() const
719{
720 return mState.scissorTest;
721}
722
723void Context::setDither(bool enabled)
724{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000725 if (mState.dither != enabled)
726 {
727 mState.dither = enabled;
728 mDitherStateDirty = true;
729 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000730}
731
732bool Context::isDitherEnabled() const
733{
734 return mState.dither;
735}
736
737void Context::setLineWidth(GLfloat width)
738{
739 mState.lineWidth = width;
740}
741
742void Context::setGenerateMipmapHint(GLenum hint)
743{
744 mState.generateMipmapHint = hint;
745}
746
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000747void Context::setFragmentShaderDerivativeHint(GLenum hint)
748{
749 mState.fragmentShaderDerivativeHint = hint;
750 // TODO: Propagate the hint to shader translator so we can write
751 // ddx, ddx_coarse, or ddx_fine depending on the hint.
752 // Ignore for now. It is valid for implementations to ignore hint.
753}
754
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000755void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
756{
757 mState.viewportX = x;
758 mState.viewportY = y;
759 mState.viewportWidth = width;
760 mState.viewportHeight = height;
761}
762
763void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
764{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000765 if (mState.scissorX != x || mState.scissorY != y ||
766 mState.scissorWidth != width || mState.scissorHeight != height)
767 {
768 mState.scissorX = x;
769 mState.scissorY = y;
770 mState.scissorWidth = width;
771 mState.scissorHeight = height;
772 mScissorStateDirty = true;
773 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000774}
775
776void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
777{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000778 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
779 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
780 {
781 mState.colorMaskRed = red;
782 mState.colorMaskGreen = green;
783 mState.colorMaskBlue = blue;
784 mState.colorMaskAlpha = alpha;
785 mMaskStateDirty = true;
786 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000787}
788
789void Context::setDepthMask(bool mask)
790{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000791 if (mState.depthMask != mask)
792 {
793 mState.depthMask = mask;
794 mMaskStateDirty = true;
795 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000796}
797
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000798void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000799{
800 mState.activeSampler = active;
801}
802
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000803GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000804{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000805 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000806}
807
808GLuint Context::getDrawFramebufferHandle() const
809{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000810 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000811}
812
813GLuint Context::getRenderbufferHandle() const
814{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000815 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000816}
817
818GLuint Context::getArrayBufferHandle() const
819{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000820 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000821}
822
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000823GLuint Context::getActiveQuery(GLenum target) const
824{
825 Query *queryObject = NULL;
826
827 switch (target)
828 {
829 case GL_ANY_SAMPLES_PASSED_EXT:
830 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
831 break;
832 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
833 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
834 break;
835 default:
836 ASSERT(false);
837 }
838
839 if (queryObject)
840 {
841 return queryObject->id();
842 }
843 else
844 {
845 return 0;
846 }
847}
848
daniel@transgaming.com83921382011-01-08 05:46:00 +0000849void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000850{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000851 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000852}
853
daniel@transgaming.com83921382011-01-08 05:46:00 +0000854const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000855{
856 return mState.vertexAttribute[attribNum];
857}
858
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000859void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000860 GLsizei stride, const void *pointer)
861{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000862 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000863 mState.vertexAttribute[attribNum].mSize = size;
864 mState.vertexAttribute[attribNum].mType = type;
865 mState.vertexAttribute[attribNum].mNormalized = normalized;
866 mState.vertexAttribute[attribNum].mStride = stride;
867 mState.vertexAttribute[attribNum].mPointer = pointer;
868}
869
870const void *Context::getVertexAttribPointer(unsigned int attribNum) const
871{
872 return mState.vertexAttribute[attribNum].mPointer;
873}
874
daniel@transgaming.com83921382011-01-08 05:46:00 +0000875const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000876{
877 return mState.vertexAttribute;
878}
879
880void Context::setPackAlignment(GLint alignment)
881{
882 mState.packAlignment = alignment;
883}
884
885GLint Context::getPackAlignment() const
886{
887 return mState.packAlignment;
888}
889
890void Context::setUnpackAlignment(GLint alignment)
891{
892 mState.unpackAlignment = alignment;
893}
894
895GLint Context::getUnpackAlignment() const
896{
897 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000900void Context::setPackReverseRowOrder(bool reverseRowOrder)
901{
902 mState.packReverseRowOrder = reverseRowOrder;
903}
904
905bool Context::getPackReverseRowOrder() const
906{
907 return mState.packReverseRowOrder;
908}
909
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910GLuint Context::createBuffer()
911{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000912 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913}
914
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915GLuint Context::createProgram()
916{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000917 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918}
919
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000920GLuint Context::createShader(GLenum type)
921{
922 return mResourceManager->createShader(type);
923}
924
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925GLuint Context::createTexture()
926{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000927 return mResourceManager->createTexture();
928}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000930GLuint Context::createRenderbuffer()
931{
932 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933}
934
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000935// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936GLuint Context::createFramebuffer()
937{
benvanik@google.com1a233342011-04-28 19:44:39 +0000938 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000939
940 mFramebufferMap[handle] = NULL;
941
942 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943}
944
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000945GLuint Context::createFence()
946{
benvanik@google.com1a233342011-04-28 19:44:39 +0000947 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000948
apatrick@chromium.org563c0a52012-03-23 21:18:42 +0000949 mFenceMap[handle] = new Fence(mDisplay);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000950
951 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000952}
953
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000954// Returns an unused query name
955GLuint Context::createQuery()
956{
957 GLuint handle = mQueryHandleAllocator.allocate();
958
959 mQueryMap[handle] = NULL;
960
961 return handle;
962}
963
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964void Context::deleteBuffer(GLuint buffer)
965{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000966 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967 {
968 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000970
971 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
974void Context::deleteShader(GLuint shader)
975{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000976 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
979void Context::deleteProgram(GLuint program)
980{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000981 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000982 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
985void Context::deleteTexture(GLuint texture)
986{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000987 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988 {
989 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000991
992 mResourceManager->deleteTexture(texture);
993}
994
995void Context::deleteRenderbuffer(GLuint renderbuffer)
996{
997 if (mResourceManager->getRenderbuffer(renderbuffer))
998 {
999 detachRenderbuffer(renderbuffer);
1000 }
1001
1002 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003}
1004
1005void Context::deleteFramebuffer(GLuint framebuffer)
1006{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001007 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
1008
1009 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010 {
1011 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +00001012
benvanik@google.com1a233342011-04-28 19:44:39 +00001013 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001014 delete framebufferObject->second;
1015 mFramebufferMap.erase(framebufferObject);
1016 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017}
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001018
1019void Context::deleteFence(GLuint fence)
1020{
1021 FenceMap::iterator fenceObject = mFenceMap.find(fence);
1022
1023 if (fenceObject != mFenceMap.end())
1024 {
benvanik@google.com1a233342011-04-28 19:44:39 +00001025 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001026 delete fenceObject->second;
1027 mFenceMap.erase(fenceObject);
1028 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001029}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001031void Context::deleteQuery(GLuint query)
1032{
1033 QueryMap::iterator queryObject = mQueryMap.find(query);
1034 if (queryObject != mQueryMap.end())
1035 {
1036 mQueryHandleAllocator.release(queryObject->first);
1037 if (queryObject->second)
1038 {
1039 queryObject->second->release();
1040 }
1041 mQueryMap.erase(queryObject);
1042 }
1043}
1044
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001045Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001047 return mResourceManager->getBuffer(handle);
1048}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001050Shader *Context::getShader(GLuint handle)
1051{
1052 return mResourceManager->getShader(handle);
1053}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001054
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001055Program *Context::getProgram(GLuint handle)
1056{
1057 return mResourceManager->getProgram(handle);
1058}
1059
1060Texture *Context::getTexture(GLuint handle)
1061{
1062 return mResourceManager->getTexture(handle);
1063}
1064
1065Renderbuffer *Context::getRenderbuffer(GLuint handle)
1066{
1067 return mResourceManager->getRenderbuffer(handle);
1068}
1069
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001070Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001071{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001072 return getFramebuffer(mState.readFramebuffer);
1073}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001074
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001075Framebuffer *Context::getDrawFramebuffer()
1076{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001077 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078}
1079
1080void Context::bindArrayBuffer(unsigned int buffer)
1081{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001082 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001084 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085}
1086
1087void Context::bindElementArrayBuffer(unsigned int buffer)
1088{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001089 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001091 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092}
1093
1094void Context::bindTexture2D(GLuint texture)
1095{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001096 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001098 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
1101void Context::bindTextureCubeMap(GLuint texture)
1102{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001103 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001105 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106}
1107
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001108void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001110 if (!getFramebuffer(framebuffer))
1111 {
1112 mFramebufferMap[framebuffer] = new Framebuffer();
1113 }
1114
1115 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001116}
1117
1118void Context::bindDrawFramebuffer(GLuint framebuffer)
1119{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001120 if (!getFramebuffer(framebuffer))
1121 {
1122 mFramebufferMap[framebuffer] = new Framebuffer();
1123 }
1124
1125 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001126
1127 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128}
1129
1130void Context::bindRenderbuffer(GLuint renderbuffer)
1131{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001132 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1133
1134 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135}
1136
1137void Context::useProgram(GLuint program)
1138{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001139 GLuint priorProgram = mState.currentProgram;
1140 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001141
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001142 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001143 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001144 Program *newProgram = mResourceManager->getProgram(program);
1145 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001146 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001147 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001148
1149 if (newProgram)
1150 {
1151 newProgram->addRef();
1152 }
1153
1154 if (oldProgram)
1155 {
1156 oldProgram->release();
1157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001158 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159}
1160
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001161void Context::beginQuery(GLenum target, GLuint query)
1162{
1163 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1164 // of zero, if the active query object name for <target> is non-zero (for the
1165 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1166 // the active query for either target is non-zero), if <id> is the name of an
1167 // existing query object whose type does not match <target>, or if <id> is the
1168 // active query object name for any query type, the error INVALID_OPERATION is
1169 // generated.
1170
1171 // Ensure no other queries are active
1172 // NOTE: If other queries than occlusion are supported, we will need to check
1173 // separately that:
1174 // a) The query ID passed is not the current active query for any target/type
1175 // b) There are no active queries for the requested target (and in the case
1176 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1177 // no query may be active for either if glBeginQuery targets either.
1178 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1179 {
1180 if (mState.activeQuery[i].get() != NULL)
1181 {
1182 return error(GL_INVALID_OPERATION);
1183 }
1184 }
1185
1186 QueryType qType;
1187 switch (target)
1188 {
1189 case GL_ANY_SAMPLES_PASSED_EXT:
1190 qType = QUERY_ANY_SAMPLES_PASSED;
1191 break;
1192 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1193 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1194 break;
1195 default:
1196 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001197 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001198 }
1199
1200 Query *queryObject = getQuery(query, true, target);
1201
1202 // check that name was obtained with glGenQueries
1203 if (!queryObject)
1204 {
1205 return error(GL_INVALID_OPERATION);
1206 }
1207
1208 // check for type mismatch
1209 if (queryObject->getType() != target)
1210 {
1211 return error(GL_INVALID_OPERATION);
1212 }
1213
1214 // set query as active for specified target
1215 mState.activeQuery[qType].set(queryObject);
1216
1217 // begin query
1218 queryObject->begin();
1219}
1220
1221void Context::endQuery(GLenum target)
1222{
1223 QueryType qType;
1224
1225 switch (target)
1226 {
1227 case GL_ANY_SAMPLES_PASSED_EXT:
1228 qType = QUERY_ANY_SAMPLES_PASSED;
1229 break;
1230 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1231 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1232 break;
1233 default:
1234 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001235 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001236 }
1237
1238 Query *queryObject = mState.activeQuery[qType].get();
1239
1240 if (queryObject == NULL)
1241 {
1242 return error(GL_INVALID_OPERATION);
1243 }
1244
1245 queryObject->end();
1246
1247 mState.activeQuery[qType].set(NULL);
1248}
1249
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001250void Context::setFramebufferZero(Framebuffer *buffer)
1251{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001252 delete mFramebufferMap[0];
1253 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001254 if (mState.drawFramebuffer == 0)
1255 {
1256 mBoundDrawFramebuffer = buffer;
1257 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001258}
1259
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001260void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001262 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1263 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001264}
1265
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001266Framebuffer *Context::getFramebuffer(unsigned int handle)
1267{
1268 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1269
1270 if (framebuffer == mFramebufferMap.end())
1271 {
1272 return NULL;
1273 }
1274 else
1275 {
1276 return framebuffer->second;
1277 }
1278}
1279
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001280Fence *Context::getFence(unsigned int handle)
1281{
1282 FenceMap::iterator fence = mFenceMap.find(handle);
1283
1284 if (fence == mFenceMap.end())
1285 {
1286 return NULL;
1287 }
1288 else
1289 {
1290 return fence->second;
1291 }
1292}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001293
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001294Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1295{
1296 QueryMap::iterator query = mQueryMap.find(handle);
1297
1298 if (query == mQueryMap.end())
1299 {
1300 return NULL;
1301 }
1302 else
1303 {
1304 if (!query->second && create)
1305 {
1306 query->second = new Query(handle, type);
1307 query->second->addRef();
1308 }
1309 return query->second;
1310 }
1311}
1312
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001313Buffer *Context::getArrayBuffer()
1314{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001315 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001316}
1317
1318Buffer *Context::getElementArrayBuffer()
1319{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001320 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001321}
1322
1323Program *Context::getCurrentProgram()
1324{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001325 if (!mCachedCurrentProgram)
1326 {
1327 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1328 }
1329 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001330}
1331
1332Texture2D *Context::getTexture2D()
1333{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001334 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001335}
1336
1337TextureCubeMap *Context::getTextureCubeMap()
1338{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001339 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001340}
1341
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001342Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001343{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001344 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001345
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001346 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001347 {
1348 switch (type)
1349 {
1350 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001351 case TEXTURE_2D: return mTexture2DZero.get();
1352 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001353 }
1354 }
1355
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001356 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357}
1358
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359bool Context::getBooleanv(GLenum pname, GLboolean *params)
1360{
1361 switch (pname)
1362 {
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001363 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1364 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1365 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001366 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001367 params[0] = mState.colorMaskRed;
1368 params[1] = mState.colorMaskGreen;
1369 params[2] = mState.colorMaskBlue;
1370 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001371 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001372 case GL_CULL_FACE: *params = mState.cullFace; break;
1373 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1374 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1375 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1376 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1377 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1378 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1379 case GL_BLEND: *params = mState.blend; break;
1380 case GL_DITHER: *params = mState.dither; break;
1381 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001382 default:
1383 return false;
1384 }
1385
1386 return true;
1387}
1388
1389bool Context::getFloatv(GLenum pname, GLfloat *params)
1390{
1391 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1392 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1393 // GetIntegerv as its native query function. As it would require conversion in any
1394 // case, this should make no difference to the calling application.
1395 switch (pname)
1396 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001397 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1398 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1399 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1400 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1401 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001402 case GL_ALIASED_LINE_WIDTH_RANGE:
1403 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1404 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1405 break;
1406 case GL_ALIASED_POINT_SIZE_RANGE:
1407 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001408 params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001409 break;
1410 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001411 params[0] = mState.zNear;
1412 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001413 break;
1414 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001415 params[0] = mState.colorClearValue.red;
1416 params[1] = mState.colorClearValue.green;
1417 params[2] = mState.colorClearValue.blue;
1418 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001419 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001420 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001421 params[0] = mState.blendColor.red;
1422 params[1] = mState.blendColor.green;
1423 params[2] = mState.blendColor.blue;
1424 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001425 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001426 default:
1427 return false;
1428 }
1429
1430 return true;
1431}
1432
1433bool Context::getIntegerv(GLenum pname, GLint *params)
1434{
1435 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1436 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1437 // GetIntegerv as its native query function. As it would require conversion in any
1438 // case, this should make no difference to the calling application. You may find it in
1439 // Context::getFloatv.
1440 switch (pname)
1441 {
1442 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1443 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001444 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001445 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1446 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001447 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001448 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001449 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001450 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001451 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001452 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1453 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001454 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1455 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1456 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001457 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001458 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1459 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001460 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001461 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1462 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001463 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001464 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1465 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1466 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1467 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1468 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1469 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1470 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1471 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1472 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1473 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1474 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1475 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1476 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1477 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1478 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1479 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1480 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1481 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1482 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1483 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1484 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1485 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1486 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1487 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001488 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1489 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001490 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001491 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001492 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001493 case GL_MAX_SAMPLES_ANGLE:
1494 {
1495 GLsizei maxSamples = getMaxSupportedSamples();
1496 if (maxSamples != 0)
1497 {
1498 *params = maxSamples;
1499 }
1500 else
1501 {
1502 return false;
1503 }
1504
1505 break;
1506 }
1507 case GL_SAMPLE_BUFFERS:
1508 case GL_SAMPLES:
1509 {
1510 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1511 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1512 {
1513 switch (pname)
1514 {
1515 case GL_SAMPLE_BUFFERS:
1516 if (framebuffer->getSamples() != 0)
1517 {
1518 *params = 1;
1519 }
1520 else
1521 {
1522 *params = 0;
1523 }
1524 break;
1525 case GL_SAMPLES:
1526 *params = framebuffer->getSamples();
1527 break;
1528 }
1529 }
1530 else
1531 {
1532 *params = 0;
1533 }
1534 }
1535 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001536 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1537 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1538 case GL_MAX_VIEWPORT_DIMS:
1539 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001540 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001541 params[0] = maxDimension;
1542 params[1] = maxDimension;
1543 }
1544 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001545 case GL_COMPRESSED_TEXTURE_FORMATS:
1546 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001547 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001548 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001549 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1550 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1551 }
1552 if (supportsDXT3Textures())
1553 {
1554 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1555 }
1556 if (supportsDXT5Textures())
1557 {
1558 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001559 }
1560 }
1561 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001562 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001563 params[0] = mState.viewportX;
1564 params[1] = mState.viewportY;
1565 params[2] = mState.viewportWidth;
1566 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001567 break;
1568 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001569 params[0] = mState.scissorX;
1570 params[1] = mState.scissorY;
1571 params[2] = mState.scissorWidth;
1572 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001573 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001574 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1575 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001576 case GL_RED_BITS:
1577 case GL_GREEN_BITS:
1578 case GL_BLUE_BITS:
1579 case GL_ALPHA_BITS:
1580 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001581 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001582 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001583
1584 if (colorbuffer)
1585 {
1586 switch (pname)
1587 {
1588 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1589 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1590 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1591 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1592 }
1593 }
1594 else
1595 {
1596 *params = 0;
1597 }
1598 }
1599 break;
1600 case GL_DEPTH_BITS:
1601 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001602 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001603 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001604
1605 if (depthbuffer)
1606 {
1607 *params = depthbuffer->getDepthSize();
1608 }
1609 else
1610 {
1611 *params = 0;
1612 }
1613 }
1614 break;
1615 case GL_STENCIL_BITS:
1616 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001617 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001618 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001619
1620 if (stencilbuffer)
1621 {
1622 *params = stencilbuffer->getStencilSize();
1623 }
1624 else
1625 {
1626 *params = 0;
1627 }
1628 }
1629 break;
1630 case GL_TEXTURE_BINDING_2D:
1631 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001632 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001633 {
1634 error(GL_INVALID_OPERATION);
1635 return false;
1636 }
1637
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001638 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001639 }
1640 break;
1641 case GL_TEXTURE_BINDING_CUBE_MAP:
1642 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001643 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001644 {
1645 error(GL_INVALID_OPERATION);
1646 return false;
1647 }
1648
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001649 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001650 }
1651 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001652 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1653 *params = mResetStrategy;
1654 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001655 default:
1656 return false;
1657 }
1658
1659 return true;
1660}
1661
1662bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1663{
1664 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1665 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1666 // to the fact that it is stored internally as a float, and so would require conversion
1667 // if returned from Context::getIntegerv. Since this conversion is already implemented
1668 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1669 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1670 // application.
1671 switch (pname)
1672 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001673 case GL_COMPRESSED_TEXTURE_FORMATS:
1674 {
1675 *type = GL_INT;
1676 *numParams = mNumCompressedTextureFormats;
1677 }
1678 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001679 case GL_SHADER_BINARY_FORMATS:
1680 {
1681 *type = GL_INT;
1682 *numParams = 0;
1683 }
1684 break;
1685 case GL_MAX_VERTEX_ATTRIBS:
1686 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1687 case GL_MAX_VARYING_VECTORS:
1688 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1689 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1690 case GL_MAX_TEXTURE_IMAGE_UNITS:
1691 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1692 case GL_MAX_RENDERBUFFER_SIZE:
1693 case GL_NUM_SHADER_BINARY_FORMATS:
1694 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1695 case GL_ARRAY_BUFFER_BINDING:
1696 case GL_FRAMEBUFFER_BINDING:
1697 case GL_RENDERBUFFER_BINDING:
1698 case GL_CURRENT_PROGRAM:
1699 case GL_PACK_ALIGNMENT:
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001700 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001701 case GL_UNPACK_ALIGNMENT:
1702 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001703 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001704 case GL_RED_BITS:
1705 case GL_GREEN_BITS:
1706 case GL_BLUE_BITS:
1707 case GL_ALPHA_BITS:
1708 case GL_DEPTH_BITS:
1709 case GL_STENCIL_BITS:
1710 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1711 case GL_CULL_FACE_MODE:
1712 case GL_FRONT_FACE:
1713 case GL_ACTIVE_TEXTURE:
1714 case GL_STENCIL_FUNC:
1715 case GL_STENCIL_VALUE_MASK:
1716 case GL_STENCIL_REF:
1717 case GL_STENCIL_FAIL:
1718 case GL_STENCIL_PASS_DEPTH_FAIL:
1719 case GL_STENCIL_PASS_DEPTH_PASS:
1720 case GL_STENCIL_BACK_FUNC:
1721 case GL_STENCIL_BACK_VALUE_MASK:
1722 case GL_STENCIL_BACK_REF:
1723 case GL_STENCIL_BACK_FAIL:
1724 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1725 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1726 case GL_DEPTH_FUNC:
1727 case GL_BLEND_SRC_RGB:
1728 case GL_BLEND_SRC_ALPHA:
1729 case GL_BLEND_DST_RGB:
1730 case GL_BLEND_DST_ALPHA:
1731 case GL_BLEND_EQUATION_RGB:
1732 case GL_BLEND_EQUATION_ALPHA:
1733 case GL_STENCIL_WRITEMASK:
1734 case GL_STENCIL_BACK_WRITEMASK:
1735 case GL_STENCIL_CLEAR_VALUE:
1736 case GL_SUBPIXEL_BITS:
1737 case GL_MAX_TEXTURE_SIZE:
1738 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1739 case GL_SAMPLE_BUFFERS:
1740 case GL_SAMPLES:
1741 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1742 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1743 case GL_TEXTURE_BINDING_2D:
1744 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001745 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001746 {
1747 *type = GL_INT;
1748 *numParams = 1;
1749 }
1750 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001751 case GL_MAX_SAMPLES_ANGLE:
1752 {
1753 if (getMaxSupportedSamples() != 0)
1754 {
1755 *type = GL_INT;
1756 *numParams = 1;
1757 }
1758 else
1759 {
1760 return false;
1761 }
1762 }
1763 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001764 case GL_MAX_VIEWPORT_DIMS:
1765 {
1766 *type = GL_INT;
1767 *numParams = 2;
1768 }
1769 break;
1770 case GL_VIEWPORT:
1771 case GL_SCISSOR_BOX:
1772 {
1773 *type = GL_INT;
1774 *numParams = 4;
1775 }
1776 break;
1777 case GL_SHADER_COMPILER:
1778 case GL_SAMPLE_COVERAGE_INVERT:
1779 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001780 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1781 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1782 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1783 case GL_SAMPLE_COVERAGE:
1784 case GL_SCISSOR_TEST:
1785 case GL_STENCIL_TEST:
1786 case GL_DEPTH_TEST:
1787 case GL_BLEND:
1788 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001789 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001790 {
1791 *type = GL_BOOL;
1792 *numParams = 1;
1793 }
1794 break;
1795 case GL_COLOR_WRITEMASK:
1796 {
1797 *type = GL_BOOL;
1798 *numParams = 4;
1799 }
1800 break;
1801 case GL_POLYGON_OFFSET_FACTOR:
1802 case GL_POLYGON_OFFSET_UNITS:
1803 case GL_SAMPLE_COVERAGE_VALUE:
1804 case GL_DEPTH_CLEAR_VALUE:
1805 case GL_LINE_WIDTH:
1806 {
1807 *type = GL_FLOAT;
1808 *numParams = 1;
1809 }
1810 break;
1811 case GL_ALIASED_LINE_WIDTH_RANGE:
1812 case GL_ALIASED_POINT_SIZE_RANGE:
1813 case GL_DEPTH_RANGE:
1814 {
1815 *type = GL_FLOAT;
1816 *numParams = 2;
1817 }
1818 break;
1819 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001820 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001821 {
1822 *type = GL_FLOAT;
1823 *numParams = 4;
1824 }
1825 break;
1826 default:
1827 return false;
1828 }
1829
1830 return true;
1831}
1832
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833// Applies the render target surface, depth stencil surface, viewport rectangle and
1834// scissor rectangle to the Direct3D 9 device
1835bool Context::applyRenderTarget(bool ignoreViewport)
1836{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001837 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838
1839 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1840 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001841 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842 }
1843
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001844 // if there is no color attachment we must synthesize a NULL colorattachment
1845 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1846 Renderbuffer *renderbufferObject = NULL;
1847 if (framebufferObject->getColorbufferType() != GL_NONE)
1848 {
1849 renderbufferObject = framebufferObject->getColorbuffer();
1850 }
1851 else
1852 {
1853 renderbufferObject = framebufferObject->getNullColorbuffer();
1854 }
1855 if (!renderbufferObject)
1856 {
1857 ERR("unable to locate renderbuffer for FBO.");
1858 return false;
1859 }
1860
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001861 bool renderTargetChanged = false;
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001862 unsigned int renderTargetSerial = renderbufferObject->getSerial();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001863 if (renderTargetSerial != mAppliedRenderTargetSerial)
1864 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001865 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001866 if (!renderTarget)
1867 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001868 ERR("render target pointer unexpectedly null.");
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001869 return false; // Context must be lost
1870 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001871 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001872 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001873 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 +00001874 renderTargetChanged = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001875 renderTarget->Release();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001876 }
1877
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001878 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001879 unsigned int depthbufferSerial = 0;
1880 unsigned int stencilbufferSerial = 0;
1881 if (framebufferObject->getDepthbufferType() != GL_NONE)
1882 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001883 Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
1884 depthStencil = depthbuffer->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001885 if (!depthStencil)
1886 {
1887 ERR("Depth stencil pointer unexpectedly null.");
1888 return false;
1889 }
1890
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001891 depthbufferSerial = depthbuffer->getSerial();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001892 }
1893 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1894 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001895 Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
1896 depthStencil = stencilbuffer->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001897 if (!depthStencil)
1898 {
1899 ERR("Depth stencil pointer unexpectedly null.");
1900 return false;
1901 }
1902
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001903 stencilbufferSerial = stencilbuffer->getSerial();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001904 }
1905
1906 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001907 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001908 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001909 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001910 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001911 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001912 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001913 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001914 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00001916 if (depthStencil)
1917 {
1918 depthStencil->Release();
1919 }
1920
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001921 if (!mRenderTargetDescInitialized || renderTargetChanged)
1922 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001923 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001924 if (!renderTarget)
1925 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001926 return false; // Context must be lost
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001927 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001928 renderTarget->GetDesc(&mRenderTargetDesc);
1929 mRenderTargetDescInitialized = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001930 renderTarget->Release();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001931 }
1932
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001934
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001935 float zNear = clamp01(mState.zNear);
1936 float zFar = clamp01(mState.zFar);
1937
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938 if (ignoreViewport)
1939 {
1940 viewport.X = 0;
1941 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001942 viewport.Width = mRenderTargetDesc.Width;
1943 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 viewport.MinZ = 0.0f;
1945 viewport.MaxZ = 1.0f;
1946 }
1947 else
1948 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001949 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1950 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1951 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1952 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1953 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 +00001954 viewport.MinZ = zNear;
1955 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001956 }
1957
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001958 if (viewport.Width <= 0 || viewport.Height <= 0)
1959 {
1960 return false; // Nothing to render
1961 }
1962
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001963 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001964 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001965 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001966 mSetViewport = viewport;
1967 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001968 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001969 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001970
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001971 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001973 if (mState.scissorTest)
1974 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001975 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1976 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1977 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1978 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1979 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001980 mDevice->SetScissorRect(&rect);
1981 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001982 }
1983 else
1984 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001985 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001986 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001987
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001988 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989 }
1990
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001991 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001994 ProgramBinary *programBinary = programObject->getProgramBinary();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001995
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001996 GLint halfPixelSize = programBinary->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001997 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001998 programBinary->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001999
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00002000 // These values are used for computing gl_FragCoord in Program::linkVaryings(). The approach depends on Shader Model 3.0 support.
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002001 GLint coord = programBinary->getDxCoordLocation();
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00002002 float h = mSupportsShaderModel3 ? mRenderTargetDesc.Height : mState.viewportHeight / 2.0f;
2003 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, h,
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002004 (float)mState.viewportX + mState.viewportWidth / 2.0f,
2005 (float)mState.viewportY + mState.viewportHeight / 2.0f};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002006 programBinary->setUniform4fv(coord, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00002007
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002008 GLint depth = programBinary->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00002009 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002010 programBinary->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00002011
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002012 GLint depthRange = programBinary->getDxDepthRangeLocation();
daniel@transgaming.com31754962010-11-28 02:02:52 +00002013 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002014 programBinary->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00002015 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016 }
2017
2018 return true;
2019}
2020
2021// 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 +00002022void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002024 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002025 ProgramBinary *programBinary = programObject->getProgramBinary();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002026
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002027 Framebuffer *framebufferObject = getDrawFramebuffer();
2028
2029 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
2030
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002031 GLint frontCCW = programBinary->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002032 GLint ccw = (adjustedFrontFace == GL_CCW);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002033 programBinary->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002035 GLint pointsOrLines = programBinary->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002036 GLint alwaysFront = !isTriangleMode(drawMode);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002037 programBinary->setUniform1iv(pointsOrLines, 1, &alwaysFront);
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002038
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002039 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002040 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2041 // Apparently some ATI cards have a bug where a draw with a zero color
2042 // write mask can cause later draws to have incorrect results. Instead,
2043 // set a nonzero color write mask but modify the blend state so that no
2044 // drawing is done.
2045 // http://code.google.com/p/angleproject/issues/detail?id=169
2046
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002047 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002048 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002049 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002051 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052 }
2053 else
2054 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002055 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002056 }
2057
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002058 mCullStateDirty = false;
2059 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002060
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002061 if (mDepthStateDirty)
2062 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00002063 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002065 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2066 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002067 }
2068 else
2069 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002070 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002072
2073 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 }
2075
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002076 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2077 {
2078 mBlendStateDirty = true;
2079 mMaskStateDirty = true;
2080 }
2081
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002082 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002084 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002085 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002086 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002087
2088 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2089 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2090 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002091 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002092 }
2093 else
2094 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002095 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002096 unorm<8>(mState.blendColor.alpha),
2097 unorm<8>(mState.blendColor.alpha),
2098 unorm<8>(mState.blendColor.alpha)));
2099 }
2100
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002101 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2102 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2103 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002104
2105 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2106 mState.destBlendRGB != mState.destBlendAlpha ||
2107 mState.blendEquationRGB != mState.blendEquationAlpha)
2108 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002109 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002110
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002111 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2112 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2113 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002114 }
2115 else
2116 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002117 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002118 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002119 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002120 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002121 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002122 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002123 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002124
2125 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 }
2127
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002128 if (mStencilStateDirty || mFrontFaceDirty)
2129 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002130 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002131 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002132 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2133 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002134
2135 // FIXME: Unsupported by D3D9
2136 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2137 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2138 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2139 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2140 mState.stencilRef != mState.stencilBackRef ||
2141 mState.stencilMask != mState.stencilBackMask)
2142 {
2143 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2144 return error(GL_INVALID_OPERATION);
2145 }
2146
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002147 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002148 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002149 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2150
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002151 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2152 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002153 es2dx::ConvertComparison(mState.stencilFunc));
2154
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002155 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2156 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002157
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002158 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002159 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002160 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002161 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002162 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002163 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2164
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002165 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2166 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002167 es2dx::ConvertComparison(mState.stencilBackFunc));
2168
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002169 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2170 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002171
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002172 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002173 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002174 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002175 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002176 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002177 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2178 }
2179 else
2180 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002181 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002182 }
2183
2184 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002185 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002186 }
2187
2188 if (mMaskStateDirty)
2189 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002190 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2191 mState.colorMaskBlue, mState.colorMaskAlpha);
2192 if (colorMask == 0 && !zeroColorMaskAllowed)
2193 {
2194 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002195 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2196 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002197
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002198 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2199 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2200 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002201 }
2202 else
2203 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002204 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002205 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002206 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002207
2208 mMaskStateDirty = false;
2209 }
2210
2211 if (mPolygonOffsetStateDirty)
2212 {
2213 if (mState.polygonOffsetFill)
2214 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002215 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002216 if (depthbuffer)
2217 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002218 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002219 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002220 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002221 }
2222 }
2223 else
2224 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002225 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2226 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002227 }
2228
2229 mPolygonOffsetStateDirty = false;
2230 }
2231
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002232 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002234 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002235 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002236 FIXME("Sample alpha to coverage is unimplemented.");
2237 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002238
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002239 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002240 if (mState.sampleCoverage)
2241 {
2242 unsigned int mask = 0;
2243 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002244 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002245 float threshold = 0.5f;
2246
2247 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002248 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002249 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002250
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002251 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002252 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002253 threshold += 1.0f;
2254 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002255 }
2256 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002257 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002258
2259 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002260 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002261 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002262 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002263
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002264 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002265 }
2266 else
2267 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002268 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002269 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002270
2271 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 }
2273
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002274 if (mDitherStateDirty)
2275 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002276 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002277
2278 mDitherStateDirty = false;
2279 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280}
2281
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002282GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002283{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002284 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002285
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +00002286 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002287 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002288 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002289 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002290 }
2291
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002292 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram(), instances, repeatDraw);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002293}
2294
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002295// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00002296GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002297{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002298 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002299
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002300 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002301 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002302 if (indexInfo->serial != mAppliedIBSerial)
2303 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002304 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002305 mAppliedIBSerial = indexInfo->serial;
2306 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002307 }
2308
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002309 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310}
2311
2312// Applies the shaders and shader constants to the Direct3D 9 device
2313void Context::applyShaders()
2314{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002316 ProgramBinary *programBinary = programObject->getProgramBinary();
2317
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002318 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002319 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002320 IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader();
2321 IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002322
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002323 mDevice->SetPixelShader(pixelShader);
2324 mDevice->SetVertexShader(vertexShader);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002325 programBinary->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002326 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002327 }
2328
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002329 programBinary->applyUniforms();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330}
2331
2332// Applies the textures and sampler states to the Direct3D 9 device
2333void Context::applyTextures()
2334{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002335 applyTextures(SAMPLER_PIXEL);
2336
2337 if (mSupportsVertexTexture)
2338 {
2339 applyTextures(SAMPLER_VERTEX);
2340 }
2341}
2342
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002343// For each Direct3D 9 sampler of either the pixel or vertex stage,
2344// looks up the corresponding OpenGL texture image unit and texture type,
2345// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002346void Context::applyTextures(SamplerType type)
2347{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002349 ProgramBinary *programBinary = programObject->getProgramBinary();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002351 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 +00002352 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2353 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002354 int samplerRange = programBinary->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002355
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002356 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002358 int textureUnit = programBinary->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002359 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002360
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002361 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002363 TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002364
2365 Texture *texture = getSamplerTexture(textureUnit, textureType);
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002366 unsigned int texSerial = texture->getTextureSerial();
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002367
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002368 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002369 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002370 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2371
2372 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002373 {
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002374 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002375 {
2376 GLenum wrapS = texture->getWrapS();
2377 GLenum wrapT = texture->getWrapT();
2378 GLenum minFilter = texture->getMinFilter();
2379 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002381 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2382 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002384 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002385 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2386 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002387 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2388 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002389 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002390
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002391 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002392 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002393 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002394 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002395 }
2396 else
2397 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002398 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002399 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002400
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002401 appliedTextureSerial[samplerIndex] = texSerial;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002402 texture->resetDirty();
2403 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002405 else
2406 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002407 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002408 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002409 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002410 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002411 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002412 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002414
2415 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2416 {
2417 if (appliedTextureSerial[samplerIndex] != 0)
2418 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002419 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002420 appliedTextureSerial[samplerIndex] = 0;
2421 }
2422 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423}
2424
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002425void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2426 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002428 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002429
2430 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2431 {
2432 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2433 }
2434
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002435 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2436 {
2437 return error(GL_INVALID_OPERATION);
2438 }
2439
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002440 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2441 // sized query sanity check
2442 if (bufSize)
2443 {
2444 int requiredSize = outputPitch * height;
2445 if (requiredSize > *bufSize)
2446 {
2447 return error(GL_INVALID_OPERATION);
2448 }
2449 }
2450
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002452 if (!renderTarget)
2453 {
2454 return; // Context must be lost, return silently
2455 }
2456
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002457 D3DSURFACE_DESC desc;
2458 renderTarget->GetDesc(&desc);
2459
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2461 {
2462 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002463 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002464 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 }
2466
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002467 HRESULT result;
2468 IDirect3DSurface9 *systemSurface = NULL;
2469 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002470 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002471 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2472 if (directToPixels)
2473 {
2474 // Use the pixels ptr as a shared handle to write directly into client's memory
2475 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2476 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2477 if (FAILED(result))
2478 {
2479 // Try again without the shared handle
2480 directToPixels = false;
2481 }
2482 }
2483
2484 if (!directToPixels)
2485 {
2486 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2487 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2488 if (FAILED(result))
2489 {
2490 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00002491 renderTarget->Release();
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002492 return error(GL_OUT_OF_MEMORY);
2493 }
2494 }
2495
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002496 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002497 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002498 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002499
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002500 if (FAILED(result))
2501 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502 systemSurface->Release();
2503
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002504 // It turns out that D3D will sometimes produce more error
2505 // codes than those documented.
2506 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002507 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002508 else
2509 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002510 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002511 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002512 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002513
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 }
2515
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002516 if (directToPixels)
2517 {
2518 systemSurface->Release();
2519 return;
2520 }
2521
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002522 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002523 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2524 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2525 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2526 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2527 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002528
2529 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2530
2531 if (FAILED(result))
2532 {
2533 UNREACHABLE();
2534 systemSurface->Release();
2535
2536 return; // No sensible error to generate
2537 }
2538
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002540 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002541
2542 unsigned char *source;
2543 int inputPitch;
2544 if (getPackReverseRowOrder())
2545 {
2546 source = (unsigned char*)lock.pBits;
2547 inputPitch = lock.Pitch;
2548 }
2549 else
2550 {
2551 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2552 inputPitch = -lock.Pitch;
2553 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002554
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555 for (int j = 0; j < rect.bottom - rect.top; j++)
2556 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002557 if (desc.Format == D3DFMT_A8R8G8B8 &&
2558 format == GL_BGRA_EXT &&
2559 type == GL_UNSIGNED_BYTE)
2560 {
2561 // Fast path for EXT_read_format_bgra, given
2562 // an RGBA source buffer. Note that buffers with no
2563 // alpha go through the slow path below.
2564 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002565 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002566 (rect.right - rect.left) * 4);
2567 continue;
2568 }
2569
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 for (int i = 0; i < rect.right - rect.left; i++)
2571 {
2572 float r;
2573 float g;
2574 float b;
2575 float a;
2576
2577 switch (desc.Format)
2578 {
2579 case D3DFMT_R5G6B5:
2580 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002581 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002582
2583 a = 1.0f;
2584 b = (rgb & 0x001F) * (1.0f / 0x001F);
2585 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2586 r = (rgb & 0xF800) * (1.0f / 0xF800);
2587 }
2588 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589 case D3DFMT_A1R5G5B5:
2590 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002591 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592
2593 a = (argb & 0x8000) ? 1.0f : 0.0f;
2594 b = (argb & 0x001F) * (1.0f / 0x001F);
2595 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2596 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2597 }
2598 break;
2599 case D3DFMT_A8R8G8B8:
2600 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002601 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602
2603 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2604 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2605 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2606 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2607 }
2608 break;
2609 case D3DFMT_X8R8G8B8:
2610 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002611 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002612
2613 a = 1.0f;
2614 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2615 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2616 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2617 }
2618 break;
2619 case D3DFMT_A2R10G10B10:
2620 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002621 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622
2623 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2624 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2625 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2626 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2627 }
2628 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002629 case D3DFMT_A32B32G32R32F:
2630 {
2631 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002632 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2633 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2634 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2635 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002636 }
2637 break;
2638 case D3DFMT_A16B16G16R16F:
2639 {
2640 // float formats in D3D are stored rgba, rather than the other way round
2641 float abgr[4];
2642
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002643 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002644
2645 a = abgr[3];
2646 b = abgr[2];
2647 g = abgr[1];
2648 r = abgr[0];
2649 }
2650 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651 default:
2652 UNIMPLEMENTED(); // FIXME
2653 UNREACHABLE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002654 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655 }
2656
2657 switch (format)
2658 {
2659 case GL_RGBA:
2660 switch (type)
2661 {
2662 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002663 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2664 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2665 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2666 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002667 break;
2668 default: UNREACHABLE();
2669 }
2670 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002671 case GL_BGRA_EXT:
2672 switch (type)
2673 {
2674 case GL_UNSIGNED_BYTE:
2675 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2676 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2677 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2678 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2679 break;
2680 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2681 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2682 // this type is packed as follows:
2683 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2684 // --------------------------------------------------------------------------------
2685 // | 4th | 3rd | 2nd | 1st component |
2686 // --------------------------------------------------------------------------------
2687 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2688 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2689 ((unsigned short)(15 * a + 0.5f) << 12)|
2690 ((unsigned short)(15 * r + 0.5f) << 8) |
2691 ((unsigned short)(15 * g + 0.5f) << 4) |
2692 ((unsigned short)(15 * b + 0.5f) << 0);
2693 break;
2694 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2695 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2696 // this type is packed as follows:
2697 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2698 // --------------------------------------------------------------------------------
2699 // | 4th | 3rd | 2nd | 1st component |
2700 // --------------------------------------------------------------------------------
2701 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2702 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2703 ((unsigned short)( a + 0.5f) << 15) |
2704 ((unsigned short)(31 * r + 0.5f) << 10) |
2705 ((unsigned short)(31 * g + 0.5f) << 5) |
2706 ((unsigned short)(31 * b + 0.5f) << 0);
2707 break;
2708 default: UNREACHABLE();
2709 }
2710 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002711 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002712 switch (type)
2713 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002714 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002715 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2716 ((unsigned short)(31 * b + 0.5f) << 0) |
2717 ((unsigned short)(63 * g + 0.5f) << 5) |
2718 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719 break;
2720 default: UNREACHABLE();
2721 }
2722 break;
2723 default: UNREACHABLE();
2724 }
2725 }
2726 }
2727
2728 systemSurface->UnlockRect();
2729
2730 systemSurface->Release();
2731}
2732
2733void Context::clear(GLbitfield mask)
2734{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002735 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002736
2737 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2738 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002739 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002740 }
2741
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742 DWORD flags = 0;
2743
2744 if (mask & GL_COLOR_BUFFER_BIT)
2745 {
2746 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002747
2748 if (framebufferObject->getColorbufferType() != GL_NONE)
2749 {
2750 flags |= D3DCLEAR_TARGET;
2751 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002752 }
2753
2754 if (mask & GL_DEPTH_BUFFER_BIT)
2755 {
2756 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002757 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758 {
2759 flags |= D3DCLEAR_ZBUFFER;
2760 }
2761 }
2762
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002763 GLuint stencilUnmasked = 0x0;
2764
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002765 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002767 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002768 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002770 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002771 if (!depthStencil)
2772 {
2773 ERR("Depth stencil pointer unexpectedly null.");
2774 return;
2775 }
2776
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002777 D3DSURFACE_DESC desc;
2778 depthStencil->GetDesc(&desc);
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00002779 depthStencil->Release();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002780
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002781 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002782 stencilUnmasked = (0x1 << stencilSize) - 1;
2783
2784 if (stencilUnmasked != 0x0)
2785 {
2786 flags |= D3DCLEAR_STENCIL;
2787 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788 }
2789 }
2790
2791 if (mask != 0)
2792 {
2793 return error(GL_INVALID_VALUE);
2794 }
2795
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002796 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2797 {
2798 return;
2799 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002800
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002801 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002802 unorm<8>(mState.colorClearValue.red),
2803 unorm<8>(mState.colorClearValue.green),
2804 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002805 float depth = clamp01(mState.depthClearValue);
2806 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002808 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002809
2810 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002811 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002812 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002813 !(mState.colorMaskRed && mState.colorMaskGreen &&
2814 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815
2816 if (needMaskedColorClear || needMaskedStencilClear)
2817 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002818 // State which is altered in all paths from this point to the clear call is saved.
2819 // State which is altered in only some paths will be flagged dirty in the case that
2820 // that path is taken.
2821 HRESULT hr;
2822 if (mMaskedClearSavedState == NULL)
2823 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002824 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002825 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2826
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002827 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2828 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2829 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2830 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2831 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2832 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2833 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2834 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2835 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2836 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2837 mDevice->SetPixelShader(NULL);
2838 mDevice->SetVertexShader(NULL);
2839 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2840 mDevice->SetStreamSource(0, NULL, 0, 0);
2841 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2842 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2843 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2844 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2845 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2846 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2847 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002848
2849 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2850 {
2851 mDevice->SetStreamSourceFreq(i, 1);
2852 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002853
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002854 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002855 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2856 }
2857
2858 ASSERT(mMaskedClearSavedState != NULL);
2859
2860 if (mMaskedClearSavedState != NULL)
2861 {
2862 hr = mMaskedClearSavedState->Capture();
2863 ASSERT(SUCCEEDED(hr));
2864 }
2865
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002866 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2867 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2868 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2869 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2870 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2871 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2872 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2873 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874
2875 if (flags & D3DCLEAR_TARGET)
2876 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002877 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002878 }
2879 else
2880 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002881 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002882 }
2883
2884 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2885 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002886 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2887 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2888 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2889 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2890 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2891 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2892 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2893 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002894 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002895 }
2896 else
2897 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002898 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002899 }
2900
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002901 mDevice->SetPixelShader(NULL);
2902 mDevice->SetVertexShader(NULL);
2903 mDevice->SetFVF(D3DFVF_XYZRHW);
2904 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2905 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2906 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2907 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2908 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2909 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2910 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002911
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002912 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2913 {
2914 mDevice->SetStreamSourceFreq(i, 1);
2915 }
2916
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002917 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2918 quad[0][0] = -0.5f;
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002919 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002920 quad[0][2] = 0.0f;
2921 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002922
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002923 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2924 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002925 quad[1][2] = 0.0f;
2926 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002927
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002928 quad[2][0] = -0.5f;
2929 quad[2][1] = -0.5f;
2930 quad[2][2] = 0.0f;
2931 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002932
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002933 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002934 quad[3][1] = -0.5f;
2935 quad[3][2] = 0.0f;
2936 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002937
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002938 mDisplay->startScene();
2939 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002940
2941 if (flags & D3DCLEAR_ZBUFFER)
2942 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002943 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2944 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2945 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002946 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002947
2948 if (mMaskedClearSavedState != NULL)
2949 {
2950 mMaskedClearSavedState->Apply();
2951 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002952 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002953 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002954 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002955 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002956 }
2957}
2958
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002959void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002960{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002961 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002962 {
2963 return error(GL_INVALID_OPERATION);
2964 }
2965
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002966 D3DPRIMITIVETYPE primitiveType;
2967 int primitiveCount;
2968
2969 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2970 return error(GL_INVALID_ENUM);
2971
2972 if (primitiveCount <= 0)
2973 {
2974 return;
2975 }
2976
2977 if (!applyRenderTarget(false))
2978 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002979 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002980 }
2981
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002982 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002983
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002984 GLsizei repeatDraw = 1;
2985 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002986 if (err != GL_NO_ERROR)
2987 {
2988 return error(err);
2989 }
2990
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002991 applyShaders();
2992 applyTextures();
2993
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002994 if (!getCurrentProgram()->getProgramBinary()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002995 {
2996 return error(GL_INVALID_OPERATION);
2997 }
2998
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002999 if (!cullSkipsDraw(mode))
3000 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003001 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00003002
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003003 if (mode == GL_LINE_LOOP)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003004 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003005 drawLineLoop(count, GL_NONE, NULL, 0);
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003006 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003007 else if (instances > 0)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003008 {
3009 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
3010 if (countingIB)
3011 {
3012 if (mAppliedIBSerial != countingIB->getSerial())
3013 {
3014 mDevice->SetIndices(countingIB->getBuffer());
3015 mAppliedIBSerial = countingIB->getSerial();
3016 }
3017
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003018 for (int i = 0; i < repeatDraw; i++)
3019 {
3020 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
3021 }
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003022 }
3023 else
3024 {
3025 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
3026 return error(GL_OUT_OF_MEMORY);
3027 }
3028 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003029 else // Regular case
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003030 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003031 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003032 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003033 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003034}
3035
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00003036void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003037{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003038 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003039 {
3040 return error(GL_INVALID_OPERATION);
3041 }
3042
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003043 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003044 {
3045 return error(GL_INVALID_OPERATION);
3046 }
3047
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003048 D3DPRIMITIVETYPE primitiveType;
3049 int primitiveCount;
3050
3051 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3052 return error(GL_INVALID_ENUM);
3053
3054 if (primitiveCount <= 0)
3055 {
3056 return;
3057 }
3058
3059 if (!applyRenderTarget(false))
3060 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00003061 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003062 }
3063
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003064 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00003065
3066 TranslatedIndexData indexInfo;
3067 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3068 if (err != GL_NO_ERROR)
3069 {
3070 return error(err);
3071 }
3072
daniel@transgaming.com83921382011-01-08 05:46:00 +00003073 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003074 GLsizei repeatDraw = 1;
3075 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00003076 if (err != GL_NO_ERROR)
3077 {
3078 return error(err);
3079 }
3080
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003081 applyShaders();
3082 applyTextures();
3083
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00003084 if (!getCurrentProgram()->getProgramBinary()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003085 {
3086 return error(GL_INVALID_OPERATION);
3087 }
3088
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003089 if (!cullSkipsDraw(mode))
3090 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003091 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003092
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003093 if (mode == GL_LINE_LOOP)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003094 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003095 drawLineLoop(count, type, indices, indexInfo.minIndex);
3096 }
3097 else
3098 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003099 for (int i = 0; i < repeatDraw; i++)
3100 {
3101 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3102 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003103 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003104 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003105}
3106
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00003107// Implements glFlush when block is false, glFinish when block is true
3108void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003109{
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +00003110 mDisplay->sync(block);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003111}
3112
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003113void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003114{
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003115 // Get the raw indices for an indexed draw
3116 if (type != GL_NONE && mState.elementArrayBuffer.get())
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003117 {
3118 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3119 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3120 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3121 }
3122
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003123 UINT startIndex = 0;
3124 bool succeeded = false;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003125
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003126 if (supports32bitIndices())
3127 {
3128 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3129
3130 if (!mLineLoopIB)
3131 {
3132 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3133 }
3134
3135 if (mLineLoopIB)
3136 {
3137 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3138
3139 UINT offset = 0;
3140 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3141 startIndex = offset / 4;
3142
3143 if (data)
3144 {
3145 switch (type)
3146 {
3147 case GL_NONE: // Non-indexed draw
3148 for (int i = 0; i < count; i++)
3149 {
3150 data[i] = i;
3151 }
3152 data[count] = 0;
3153 break;
3154 case GL_UNSIGNED_BYTE:
3155 for (int i = 0; i < count; i++)
3156 {
3157 data[i] = static_cast<const GLubyte*>(indices)[i];
3158 }
3159 data[count] = static_cast<const GLubyte*>(indices)[0];
3160 break;
3161 case GL_UNSIGNED_SHORT:
3162 for (int i = 0; i < count; i++)
3163 {
3164 data[i] = static_cast<const GLushort*>(indices)[i];
3165 }
3166 data[count] = static_cast<const GLushort*>(indices)[0];
3167 break;
3168 case GL_UNSIGNED_INT:
3169 for (int i = 0; i < count; i++)
3170 {
3171 data[i] = static_cast<const GLuint*>(indices)[i];
3172 }
3173 data[count] = static_cast<const GLuint*>(indices)[0];
3174 break;
3175 default: UNREACHABLE();
3176 }
3177
3178 mLineLoopIB->unmap();
3179 succeeded = true;
3180 }
3181 }
3182 }
3183 else
3184 {
3185 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3186
3187 if (!mLineLoopIB)
3188 {
3189 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3190 }
3191
3192 if (mLineLoopIB)
3193 {
3194 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3195
3196 UINT offset = 0;
3197 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3198 startIndex = offset / 2;
3199
3200 if (data)
3201 {
3202 switch (type)
3203 {
3204 case GL_NONE: // Non-indexed draw
3205 for (int i = 0; i < count; i++)
3206 {
3207 data[i] = i;
3208 }
3209 data[count] = 0;
3210 break;
3211 case GL_UNSIGNED_BYTE:
3212 for (int i = 0; i < count; i++)
3213 {
3214 data[i] = static_cast<const GLubyte*>(indices)[i];
3215 }
3216 data[count] = static_cast<const GLubyte*>(indices)[0];
3217 break;
3218 case GL_UNSIGNED_SHORT:
3219 for (int i = 0; i < count; i++)
3220 {
3221 data[i] = static_cast<const GLushort*>(indices)[i];
3222 }
3223 data[count] = static_cast<const GLushort*>(indices)[0];
3224 break;
3225 case GL_UNSIGNED_INT:
3226 for (int i = 0; i < count; i++)
3227 {
3228 data[i] = static_cast<const GLuint*>(indices)[i];
3229 }
3230 data[count] = static_cast<const GLuint*>(indices)[0];
3231 break;
3232 default: UNREACHABLE();
3233 }
3234
3235 mLineLoopIB->unmap();
3236 succeeded = true;
3237 }
3238 }
3239 }
3240
3241 if (succeeded)
3242 {
3243 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3244 {
3245 mDevice->SetIndices(mLineLoopIB->getBuffer());
3246 mAppliedIBSerial = mLineLoopIB->getSerial();
3247 }
3248
3249 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3250 }
3251 else
3252 {
3253 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3254 return error(GL_OUT_OF_MEMORY);
3255 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003256}
3257
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003258void Context::recordInvalidEnum()
3259{
3260 mInvalidEnum = true;
3261}
3262
3263void Context::recordInvalidValue()
3264{
3265 mInvalidValue = true;
3266}
3267
3268void Context::recordInvalidOperation()
3269{
3270 mInvalidOperation = true;
3271}
3272
3273void Context::recordOutOfMemory()
3274{
3275 mOutOfMemory = true;
3276}
3277
3278void Context::recordInvalidFramebufferOperation()
3279{
3280 mInvalidFramebufferOperation = true;
3281}
3282
3283// Get one of the recorded errors and clear its flag, if any.
3284// [OpenGL ES 2.0.24] section 2.5 page 13.
3285GLenum Context::getError()
3286{
3287 if (mInvalidEnum)
3288 {
3289 mInvalidEnum = false;
3290
3291 return GL_INVALID_ENUM;
3292 }
3293
3294 if (mInvalidValue)
3295 {
3296 mInvalidValue = false;
3297
3298 return GL_INVALID_VALUE;
3299 }
3300
3301 if (mInvalidOperation)
3302 {
3303 mInvalidOperation = false;
3304
3305 return GL_INVALID_OPERATION;
3306 }
3307
3308 if (mOutOfMemory)
3309 {
3310 mOutOfMemory = false;
3311
3312 return GL_OUT_OF_MEMORY;
3313 }
3314
3315 if (mInvalidFramebufferOperation)
3316 {
3317 mInvalidFramebufferOperation = false;
3318
3319 return GL_INVALID_FRAMEBUFFER_OPERATION;
3320 }
3321
3322 return GL_NO_ERROR;
3323}
3324
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003325GLenum Context::getResetStatus()
3326{
3327 if (mResetStatus == GL_NO_ERROR)
3328 {
3329 bool lost = mDisplay->testDeviceLost();
3330
3331 if (lost)
3332 {
3333 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3334 }
3335 }
3336
3337 GLenum status = mResetStatus;
3338
3339 if (mResetStatus != GL_NO_ERROR)
3340 {
3341 if (mDisplay->testDeviceResettable())
3342 {
3343 mResetStatus = GL_NO_ERROR;
3344 }
3345 }
3346
3347 return status;
3348}
3349
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003350bool Context::isResetNotificationEnabled()
3351{
3352 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3353}
3354
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003355bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003356{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003357 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003358}
3359
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003360int Context::getMaximumVaryingVectors() const
3361{
3362 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3363}
3364
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003365unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003366{
3367 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3368}
3369
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003370unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003371{
3372 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3373}
3374
daniel@transgaming.com458da142010-11-28 02:03:02 +00003375int Context::getMaximumFragmentUniformVectors() const
3376{
3377 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3378}
3379
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003380int Context::getMaxSupportedSamples() const
3381{
3382 return mMaxSupportedSamples;
3383}
3384
3385int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3386{
3387 if (requested == 0)
3388 {
3389 return requested;
3390 }
3391
3392 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3393 if (itr == mMultiSampleSupport.end())
3394 {
3395 return -1;
3396 }
3397
3398 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3399 {
3400 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3401 {
3402 return i;
3403 }
3404 }
3405
3406 return -1;
3407}
3408
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003409bool Context::supportsEventQueries() const
3410{
3411 return mSupportsEventQueries;
3412}
3413
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003414bool Context::supportsOcclusionQueries() const
3415{
3416 return mSupportsOcclusionQueries;
3417}
3418
gman@chromium.org50c526d2011-08-10 05:19:44 +00003419bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003420{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003421 return mSupportsDXT1Textures;
3422}
3423
3424bool Context::supportsDXT3Textures() const
3425{
3426 return mSupportsDXT3Textures;
3427}
3428
3429bool Context::supportsDXT5Textures() const
3430{
3431 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003432}
3433
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003434bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003435{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003436 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003437}
3438
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003439bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003440{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003441 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003442}
3443
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003444bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003445{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003446 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003447}
3448
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003449bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003450{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003451 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003452}
3453
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003454bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003455{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003456 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003457}
3458
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003459bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003460{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003461 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003462}
3463
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003464int Context::getMaximumRenderbufferDimension() const
3465{
3466 return mMaxRenderbufferDimension;
3467}
3468
3469int Context::getMaximumTextureDimension() const
3470{
3471 return mMaxTextureDimension;
3472}
3473
3474int Context::getMaximumCubeTextureDimension() const
3475{
3476 return mMaxCubeTextureDimension;
3477}
3478
3479int Context::getMaximumTextureLevel() const
3480{
3481 return mMaxTextureLevel;
3482}
3483
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003484bool Context::supportsLuminanceTextures() const
3485{
3486 return mSupportsLuminanceTextures;
3487}
3488
3489bool Context::supportsLuminanceAlphaTextures() const
3490{
3491 return mSupportsLuminanceAlphaTextures;
3492}
3493
daniel@transgaming.com1c49f792012-05-31 01:14:02 +00003494bool Context::supportsDepthTextures() const
3495{
3496 return mSupportsDepthTextures;
3497}
3498
daniel@transgaming.com83921382011-01-08 05:46:00 +00003499bool Context::supports32bitIndices() const
3500{
3501 return mSupports32bitIndices;
3502}
3503
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003504bool Context::supportsNonPower2Texture() const
3505{
3506 return mSupportsNonPower2Texture;
3507}
3508
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003509bool Context::supportsInstancing() const
3510{
3511 return mSupportsInstancing;
3512}
3513
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003514void Context::detachBuffer(GLuint buffer)
3515{
3516 // [OpenGL ES 2.0.24] section 2.9 page 22:
3517 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3518 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3519
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003520 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003521 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003522 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003523 }
3524
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003525 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003526 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003527 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003528 }
3529
3530 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3531 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003532 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003533 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003534 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003535 }
3536 }
3537}
3538
3539void Context::detachTexture(GLuint texture)
3540{
3541 // [OpenGL ES 2.0.24] section 3.8 page 84:
3542 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3543 // rebound to texture object zero
3544
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003545 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003546 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003547 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003548 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003549 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003550 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003551 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003552 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003553 }
3554 }
3555
3556 // [OpenGL ES 2.0.24] section 4.4 page 112:
3557 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3558 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3559 // image was attached in the currently bound framebuffer.
3560
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003561 Framebuffer *readFramebuffer = getReadFramebuffer();
3562 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003563
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003564 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003565 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003566 readFramebuffer->detachTexture(texture);
3567 }
3568
3569 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3570 {
3571 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003572 }
3573}
3574
3575void Context::detachFramebuffer(GLuint framebuffer)
3576{
3577 // [OpenGL ES 2.0.24] section 4.4 page 107:
3578 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3579 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3580
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003581 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003582 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003583 bindReadFramebuffer(0);
3584 }
3585
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003586 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003587 {
3588 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003589 }
3590}
3591
3592void Context::detachRenderbuffer(GLuint renderbuffer)
3593{
3594 // [OpenGL ES 2.0.24] section 4.4 page 109:
3595 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3596 // had been executed with the target RENDERBUFFER and name of zero.
3597
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003598 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003599 {
3600 bindRenderbuffer(0);
3601 }
3602
3603 // [OpenGL ES 2.0.24] section 4.4 page 111:
3604 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3605 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3606 // point to which this image was attached in the currently bound framebuffer.
3607
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003608 Framebuffer *readFramebuffer = getReadFramebuffer();
3609 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003610
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003611 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003612 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003613 readFramebuffer->detachRenderbuffer(renderbuffer);
3614 }
3615
3616 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3617 {
3618 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003619 }
3620}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003621
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003622Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003623{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003624 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003625
3626 if (t == NULL)
3627 {
3628 static const GLubyte color[] = { 0, 0, 0, 255 };
3629
3630 switch (type)
3631 {
3632 default:
3633 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003634 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003635
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003636 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003637 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003638 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003639 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003640 t = incomplete2d;
3641 }
3642 break;
3643
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003644 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003645 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003646 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003647
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003648 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3649 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3650 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3651 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3652 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3653 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003654
3655 t = incompleteCube;
3656 }
3657 break;
3658 }
3659
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003660 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003661 }
3662
3663 return t;
3664}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003665
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003666bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003667{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003668 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003669}
3670
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003671bool Context::isTriangleMode(GLenum drawMode)
3672{
3673 switch (drawMode)
3674 {
3675 case GL_TRIANGLES:
3676 case GL_TRIANGLE_FAN:
3677 case GL_TRIANGLE_STRIP:
3678 return true;
3679 case GL_POINTS:
3680 case GL_LINES:
3681 case GL_LINE_LOOP:
3682 case GL_LINE_STRIP:
3683 return false;
3684 default: UNREACHABLE();
3685 }
3686
3687 return false;
3688}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003689
3690void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3691{
3692 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3693
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003694 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3695 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3696 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3697 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003698
daniel@transgaming.com83921382011-01-08 05:46:00 +00003699 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003700}
3701
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00003702void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3703{
3704 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3705
3706 mState.vertexAttribute[index].mDivisor = divisor;
3707}
3708
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003709// keep list sorted in following order
3710// OES extensions
3711// EXT extensions
3712// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003713void Context::initExtensionString()
3714{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003715 mExtensionString = "";
3716
3717 // OES extensions
3718 if (supports32bitIndices())
3719 {
3720 mExtensionString += "GL_OES_element_index_uint ";
3721 }
3722
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003723 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003724 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003725 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003726
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003727 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003728 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003729 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003730 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003731 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003732 {
3733 mExtensionString += "GL_OES_texture_half_float_linear ";
3734 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003735 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003736 {
3737 mExtensionString += "GL_OES_texture_float ";
3738 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003739 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003740 {
3741 mExtensionString += "GL_OES_texture_float_linear ";
3742 }
3743
3744 if (supportsNonPower2Texture())
3745 {
3746 mExtensionString += "GL_OES_texture_npot ";
3747 }
3748
3749 // Multi-vendor (EXT) extensions
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003750 if (supportsOcclusionQueries())
3751 {
3752 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3753 }
3754
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003755 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003756 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003757
gman@chromium.org50c526d2011-08-10 05:19:44 +00003758 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003759 {
3760 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3761 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003762
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003763 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.comdf363722011-12-16 23:29:53 +00003764 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003765
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003766 // ANGLE-specific extensions
daniel@transgaming.com92d620c2012-05-31 01:16:31 +00003767 if (supportsDepthTextures())
3768 {
3769 mExtensionString += "GL_ANGLE_depth_texture ";
3770 }
3771
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003772 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003773 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003774 {
3775 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3776 }
3777
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003778 if (supportsInstancing())
daniel@transgaming.comdce02fd2012-01-27 15:39:51 +00003779 {
3780 mExtensionString += "GL_ANGLE_instanced_arrays ";
3781 }
3782
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003783 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3784
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003785 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003786 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003787 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3788 }
3789 if (supportsDXT5Textures())
3790 {
3791 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003792 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003793
3794 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003795 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003796
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003797 // Other vendor-specific extensions
3798 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003799 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003800 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003801 }
3802
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003803 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3804 if (end != std::string::npos)
3805 {
3806 mExtensionString.resize(end+1);
3807 }
3808}
3809
3810const char *Context::getExtensionString() const
3811{
3812 return mExtensionString.c_str();
3813}
3814
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003815void Context::initRendererString()
3816{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003817 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003818
3819 mRendererString = "ANGLE (";
3820 mRendererString += identifier->Description;
3821 mRendererString += ")";
3822}
3823
3824const char *Context::getRendererString() const
3825{
3826 return mRendererString.c_str();
3827}
3828
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003829void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3830 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3831 GLbitfield mask)
3832{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003833 Framebuffer *readFramebuffer = getReadFramebuffer();
3834 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3835
3836 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3837 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3838 {
3839 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3840 }
3841
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003842 if (drawFramebuffer->getSamples() != 0)
3843 {
3844 return error(GL_INVALID_OPERATION);
3845 }
3846
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003847 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3848 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3849 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3850 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3851
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003852 RECT sourceRect;
3853 RECT destRect;
3854
3855 if (srcX0 < srcX1)
3856 {
3857 sourceRect.left = srcX0;
3858 sourceRect.right = srcX1;
3859 destRect.left = dstX0;
3860 destRect.right = dstX1;
3861 }
3862 else
3863 {
3864 sourceRect.left = srcX1;
3865 destRect.left = dstX1;
3866 sourceRect.right = srcX0;
3867 destRect.right = dstX0;
3868 }
3869
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003870 if (srcY0 < srcY1)
3871 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003872 sourceRect.top = readBufferHeight - srcY1;
3873 destRect.top = drawBufferHeight - dstY1;
3874 sourceRect.bottom = readBufferHeight - srcY0;
3875 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003876 }
3877 else
3878 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003879 sourceRect.top = readBufferHeight - srcY0;
3880 destRect.top = drawBufferHeight - dstY0;
3881 sourceRect.bottom = readBufferHeight - srcY1;
3882 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003883 }
3884
3885 RECT sourceScissoredRect = sourceRect;
3886 RECT destScissoredRect = destRect;
3887
3888 if (mState.scissorTest)
3889 {
3890 // Only write to parts of the destination framebuffer which pass the scissor test
3891 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3892 // rect will be checked against scissorY, rather than the bottom.
3893 if (destRect.left < mState.scissorX)
3894 {
3895 int xDiff = mState.scissorX - destRect.left;
3896 destScissoredRect.left = mState.scissorX;
3897 sourceScissoredRect.left += xDiff;
3898 }
3899
3900 if (destRect.right > mState.scissorX + mState.scissorWidth)
3901 {
3902 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3903 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3904 sourceScissoredRect.right -= xDiff;
3905 }
3906
3907 if (destRect.top < mState.scissorY)
3908 {
3909 int yDiff = mState.scissorY - destRect.top;
3910 destScissoredRect.top = mState.scissorY;
3911 sourceScissoredRect.top += yDiff;
3912 }
3913
3914 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3915 {
3916 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3917 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3918 sourceScissoredRect.bottom -= yDiff;
3919 }
3920 }
3921
3922 bool blitRenderTarget = false;
3923 bool blitDepthStencil = false;
3924
3925 RECT sourceTrimmedRect = sourceScissoredRect;
3926 RECT destTrimmedRect = destScissoredRect;
3927
3928 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3929 // the actual draw and read surfaces.
3930 if (sourceTrimmedRect.left < 0)
3931 {
3932 int xDiff = 0 - sourceTrimmedRect.left;
3933 sourceTrimmedRect.left = 0;
3934 destTrimmedRect.left += xDiff;
3935 }
3936
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003937 if (sourceTrimmedRect.right > readBufferWidth)
3938 {
3939 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3940 sourceTrimmedRect.right = readBufferWidth;
3941 destTrimmedRect.right -= xDiff;
3942 }
3943
3944 if (sourceTrimmedRect.top < 0)
3945 {
3946 int yDiff = 0 - sourceTrimmedRect.top;
3947 sourceTrimmedRect.top = 0;
3948 destTrimmedRect.top += yDiff;
3949 }
3950
3951 if (sourceTrimmedRect.bottom > readBufferHeight)
3952 {
3953 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3954 sourceTrimmedRect.bottom = readBufferHeight;
3955 destTrimmedRect.bottom -= yDiff;
3956 }
3957
3958 if (destTrimmedRect.left < 0)
3959 {
3960 int xDiff = 0 - destTrimmedRect.left;
3961 destTrimmedRect.left = 0;
3962 sourceTrimmedRect.left += xDiff;
3963 }
3964
3965 if (destTrimmedRect.right > drawBufferWidth)
3966 {
3967 int xDiff = destTrimmedRect.right - drawBufferWidth;
3968 destTrimmedRect.right = drawBufferWidth;
3969 sourceTrimmedRect.right -= xDiff;
3970 }
3971
3972 if (destTrimmedRect.top < 0)
3973 {
3974 int yDiff = 0 - destTrimmedRect.top;
3975 destTrimmedRect.top = 0;
3976 sourceTrimmedRect.top += yDiff;
3977 }
3978
3979 if (destTrimmedRect.bottom > drawBufferHeight)
3980 {
3981 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3982 destTrimmedRect.bottom = drawBufferHeight;
3983 sourceTrimmedRect.bottom -= yDiff;
3984 }
3985
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003986 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003987 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3988 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3989 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3990 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003991 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3992 {
3993 partialBufferCopy = true;
3994 }
3995
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003996 if (mask & GL_COLOR_BUFFER_BIT)
3997 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003998 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3999 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
4000 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
4001 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
4002 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004003 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
4004 {
4005 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
4006 return error(GL_INVALID_OPERATION);
4007 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004008
4009 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
4010 {
4011 return error(GL_INVALID_OPERATION);
4012 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004013
4014 blitRenderTarget = true;
4015
4016 }
4017
4018 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
4019 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00004020 Renderbuffer *readDSBuffer = NULL;
4021 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004022
4023 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
4024 // both a depth and stencil buffer, it will be the same buffer.
4025
4026 if (mask & GL_DEPTH_BUFFER_BIT)
4027 {
4028 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
4029 {
4030 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
4031 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
4032 {
4033 return error(GL_INVALID_OPERATION);
4034 }
4035
4036 blitDepthStencil = true;
4037 readDSBuffer = readFramebuffer->getDepthbuffer();
4038 drawDSBuffer = drawFramebuffer->getDepthbuffer();
4039 }
4040 }
4041
4042 if (mask & GL_STENCIL_BUFFER_BIT)
4043 {
4044 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4045 {
4046 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
4047 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
4048 {
4049 return error(GL_INVALID_OPERATION);
4050 }
4051
4052 blitDepthStencil = true;
4053 readDSBuffer = readFramebuffer->getStencilbuffer();
4054 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4055 }
4056 }
4057
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004058 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004059 {
4060 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4061 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4062 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004063
daniel@transgaming.com97446d22010-08-24 19:20:54 +00004064 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4065 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004066 {
4067 return error(GL_INVALID_OPERATION);
4068 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004069 }
4070
4071 if (blitRenderTarget || blitDepthStencil)
4072 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004073 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004074
4075 if (blitRenderTarget)
4076 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00004077 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4078 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4079
4080 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4081 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4082
4083 readRenderTarget->Release();
4084 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004085
4086 if (FAILED(result))
4087 {
4088 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4089 return;
4090 }
4091 }
4092
4093 if (blitDepthStencil)
4094 {
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00004095 IDirect3DSurface9* readDepthStencil = readFramebuffer->getDepthStencil();
4096 IDirect3DSurface9* drawDepthStencil = drawFramebuffer->getDepthStencil();
4097
4098 HRESULT result = mDevice->StretchRect(readDepthStencil, NULL, drawDepthStencil, NULL, D3DTEXF_NONE);
4099
4100 readDepthStencil->Release();
4101 drawDepthStencil->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004102
4103 if (FAILED(result))
4104 {
4105 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4106 return;
4107 }
4108 }
4109 }
4110}
4111
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004112VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4113{
4114 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4115 {
4116 mVertexDeclCache[i].vertexDeclaration = NULL;
4117 mVertexDeclCache[i].lruCount = 0;
4118 }
4119}
4120
4121VertexDeclarationCache::~VertexDeclarationCache()
4122{
4123 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4124 {
4125 if (mVertexDeclCache[i].vertexDeclaration)
4126 {
4127 mVertexDeclCache[i].vertexDeclaration->Release();
4128 }
4129 }
4130}
4131
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004132GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004133{
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004134 *repeatDraw = 1;
4135
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004136 int indexedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004137 int instancedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004138
4139 if (instances > 0)
4140 {
4141 // Find an indexed attribute to be mapped to D3D stream 0
4142 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4143 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004144 if (attributes[i].active)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004145 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004146 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4147 {
4148 if (attributes[i].divisor == 0)
4149 {
4150 indexedAttribute = i;
4151 }
4152 }
4153 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4154 {
4155 if (attributes[i].divisor != 0)
4156 {
4157 instancedAttribute = i;
4158 }
4159 }
4160 else break; // Found both an indexed and instanced attribute
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004161 }
4162 }
4163
4164 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4165 {
4166 return GL_INVALID_OPERATION;
4167 }
4168 }
4169
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004170 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4171 D3DVERTEXELEMENT9 *element = &elements[0];
4172
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00004173 ProgramBinary *programBinary = program->getProgramBinary();
4174
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004175 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4176 {
4177 if (attributes[i].active)
4178 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004179 int stream = i;
4180
4181 if (instances > 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004182 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004183 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4184 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004185 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004186 *repeatDraw = instances;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004187 }
4188 else
4189 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004190 if (i == indexedAttribute)
4191 {
4192 stream = 0;
4193 }
4194 else if (i == 0)
4195 {
4196 stream = indexedAttribute;
4197 }
4198
4199 UINT frequency = 1;
4200
4201 if (attributes[i].divisor == 0)
4202 {
4203 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4204 }
4205 else
4206 {
4207 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4208 }
4209
4210 device->SetStreamSourceFreq(stream, frequency);
4211 mInstancingEnabled = true;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004212 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004213 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004214
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004215 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4216 mAppliedVBs[stream].stride != attributes[i].stride ||
4217 mAppliedVBs[stream].offset != attributes[i].offset)
4218 {
4219 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4220 mAppliedVBs[stream].serial = attributes[i].serial;
4221 mAppliedVBs[stream].stride = attributes[i].stride;
4222 mAppliedVBs[stream].offset = attributes[i].offset;
4223 }
4224
4225 element->Stream = stream;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004226 element->Offset = 0;
4227 element->Type = attributes[i].type;
4228 element->Method = D3DDECLMETHOD_DEFAULT;
4229 element->Usage = D3DDECLUSAGE_TEXCOORD;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00004230 element->UsageIndex = programBinary->getSemanticIndex(i);
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004231 element++;
4232 }
4233 }
4234
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004235 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004236 {
4237 if (mInstancingEnabled)
4238 {
4239 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4240 {
4241 device->SetStreamSourceFreq(i, 1);
4242 }
4243
4244 mInstancingEnabled = false;
4245 }
4246 }
4247
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004248 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4249 *(element++) = end;
4250
4251 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4252 {
4253 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4254 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4255 {
4256 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004257 if(entry->vertexDeclaration != mLastSetVDecl)
4258 {
4259 device->SetVertexDeclaration(entry->vertexDeclaration);
4260 mLastSetVDecl = entry->vertexDeclaration;
4261 }
4262
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004263 return GL_NO_ERROR;
4264 }
4265 }
4266
4267 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4268
4269 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4270 {
4271 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4272 {
4273 lastCache = &mVertexDeclCache[i];
4274 }
4275 }
4276
4277 if (lastCache->vertexDeclaration != NULL)
4278 {
4279 lastCache->vertexDeclaration->Release();
4280 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004281 // mLastSetVDecl is set to the replacement, so we don't have to worry
4282 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004283 }
4284
4285 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4286 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4287 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004288 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004289 lastCache->lruCount = ++mMaxLru;
4290
4291 return GL_NO_ERROR;
4292}
4293
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004294void VertexDeclarationCache::markStateDirty()
4295{
4296 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4297 {
4298 mAppliedVBs[i].serial = 0;
4299 }
4300
4301 mLastSetVDecl = NULL;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004302 mInstancingEnabled = true; // Forces it to be disabled when not used
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004303}
4304
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004305}
4306
4307extern "C"
4308{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004309gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004310{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004311 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004312}
4313
4314void glDestroyContext(gl::Context *context)
4315{
4316 delete context;
4317
4318 if (context == gl::getContext())
4319 {
4320 gl::makeCurrent(NULL, NULL, NULL);
4321 }
4322}
4323
4324void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4325{
4326 gl::makeCurrent(context, display, surface);
4327}
4328
4329gl::Context *glGetCurrentContext()
4330{
4331 return gl::getContext();
4332}
4333}