blob: 862ddaa709cc560afa26891b402b2d15954040bf [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();
1994
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001995 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001996 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001997 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001998
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001999 // These values are used for computing gl_FragCoord in Program::linkVaryings(). The approach depends on Shader Model 3.0 support.
2000 GLint coord = programObject->getDxCoordLocation();
2001 float h = mSupportsShaderModel3 ? mRenderTargetDesc.Height : mState.viewportHeight / 2.0f;
2002 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, h,
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002003 (float)mState.viewportX + mState.viewportWidth / 2.0f,
2004 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00002005 programObject->setUniform4fv(coord, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00002006
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002007 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00002008 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00002009 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00002010
daniel@transgaming.com31754962010-11-28 02:02:52 +00002011 GLint depthRange = programObject->getDxDepthRangeLocation();
2012 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
2013 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00002014 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015 }
2016
2017 return true;
2018}
2019
2020// 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 +00002021void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002023 Program *programObject = getCurrentProgram();
2024
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002025 Framebuffer *framebufferObject = getDrawFramebuffer();
2026
2027 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
2028
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002029 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002030 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002031 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002033 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002034 GLint alwaysFront = !isTriangleMode(drawMode);
2035 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
2036
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002037 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002038 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2039 // Apparently some ATI cards have a bug where a draw with a zero color
2040 // write mask can cause later draws to have incorrect results. Instead,
2041 // set a nonzero color write mask but modify the blend state so that no
2042 // drawing is done.
2043 // http://code.google.com/p/angleproject/issues/detail?id=169
2044
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002045 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002047 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002048 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002049 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 }
2051 else
2052 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002053 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054 }
2055
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002056 mCullStateDirty = false;
2057 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002059 if (mDepthStateDirty)
2060 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00002061 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002063 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2064 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065 }
2066 else
2067 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002068 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002070
2071 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002072 }
2073
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002074 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2075 {
2076 mBlendStateDirty = true;
2077 mMaskStateDirty = true;
2078 }
2079
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002080 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002082 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002083 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002084 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002085
2086 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2087 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2088 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002089 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002090 }
2091 else
2092 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002093 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002094 unorm<8>(mState.blendColor.alpha),
2095 unorm<8>(mState.blendColor.alpha),
2096 unorm<8>(mState.blendColor.alpha)));
2097 }
2098
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002099 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2100 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2101 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002102
2103 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2104 mState.destBlendRGB != mState.destBlendAlpha ||
2105 mState.blendEquationRGB != mState.blendEquationAlpha)
2106 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002107 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002108
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002109 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2110 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2111 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002112 }
2113 else
2114 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002115 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002116 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002117 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002118 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002119 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002120 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002121 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002122
2123 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124 }
2125
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002126 if (mStencilStateDirty || mFrontFaceDirty)
2127 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002128 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002129 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002130 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2131 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002132
2133 // FIXME: Unsupported by D3D9
2134 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2135 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2136 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2137 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2138 mState.stencilRef != mState.stencilBackRef ||
2139 mState.stencilMask != mState.stencilBackMask)
2140 {
2141 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2142 return error(GL_INVALID_OPERATION);
2143 }
2144
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002145 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002146 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002147 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2148
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002149 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2150 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002151 es2dx::ConvertComparison(mState.stencilFunc));
2152
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002153 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2154 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002155
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002156 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002157 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002158 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002159 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002160 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002161 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2162
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002163 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2164 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002165 es2dx::ConvertComparison(mState.stencilBackFunc));
2166
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002167 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2168 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002169
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002170 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002171 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002172 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002173 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002174 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002175 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2176 }
2177 else
2178 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002179 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002180 }
2181
2182 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002183 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002184 }
2185
2186 if (mMaskStateDirty)
2187 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002188 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2189 mState.colorMaskBlue, mState.colorMaskAlpha);
2190 if (colorMask == 0 && !zeroColorMaskAllowed)
2191 {
2192 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002193 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2194 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002195
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002196 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2197 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2198 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002199 }
2200 else
2201 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002202 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002203 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002204 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002205
2206 mMaskStateDirty = false;
2207 }
2208
2209 if (mPolygonOffsetStateDirty)
2210 {
2211 if (mState.polygonOffsetFill)
2212 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002213 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002214 if (depthbuffer)
2215 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002216 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002217 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002218 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002219 }
2220 }
2221 else
2222 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002223 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2224 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002225 }
2226
2227 mPolygonOffsetStateDirty = false;
2228 }
2229
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002230 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002231 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002232 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002233 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002234 FIXME("Sample alpha to coverage is unimplemented.");
2235 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002236
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002237 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002238 if (mState.sampleCoverage)
2239 {
2240 unsigned int mask = 0;
2241 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002242 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002243 float threshold = 0.5f;
2244
2245 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002246 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002247 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002248
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002249 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002250 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002251 threshold += 1.0f;
2252 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002253 }
2254 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002255 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002256
2257 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002258 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002259 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002260 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002261
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002262 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002263 }
2264 else
2265 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002266 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002267 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002268
2269 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002270 }
2271
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002272 if (mDitherStateDirty)
2273 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002274 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002275
2276 mDitherStateDirty = false;
2277 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278}
2279
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002280GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002281{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002282 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002283
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +00002284 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002285 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002286 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002287 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002288 }
2289
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002290 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram(), instances, repeatDraw);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002291}
2292
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002293// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00002294GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002295{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002296 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002297
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002298 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002299 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002300 if (indexInfo->serial != mAppliedIBSerial)
2301 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002302 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002303 mAppliedIBSerial = indexInfo->serial;
2304 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002305 }
2306
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002307 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308}
2309
2310// Applies the shaders and shader constants to the Direct3D 9 device
2311void Context::applyShaders()
2312{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002314 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002315 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002316 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2317 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2318
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002319 mDevice->SetPixelShader(pixelShader);
2320 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002321 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002322 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002323 }
2324
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 programObject->applyUniforms();
2326}
2327
2328// Applies the textures and sampler states to the Direct3D 9 device
2329void Context::applyTextures()
2330{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002331 applyTextures(SAMPLER_PIXEL);
2332
2333 if (mSupportsVertexTexture)
2334 {
2335 applyTextures(SAMPLER_VERTEX);
2336 }
2337}
2338
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002339// For each Direct3D 9 sampler of either the pixel or vertex stage,
2340// looks up the corresponding OpenGL texture image unit and texture type,
2341// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002342void Context::applyTextures(SamplerType type)
2343{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344 Program *programObject = getCurrentProgram();
2345
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002346 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 +00002347 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2348 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002349 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002350
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002351 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002353 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002354 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002355
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002356 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002358 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002359
2360 Texture *texture = getSamplerTexture(textureUnit, textureType);
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002361 unsigned int texSerial = texture->getTextureSerial();
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002362
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002363 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002364 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002365 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2366
2367 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002368 {
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002369 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002370 {
2371 GLenum wrapS = texture->getWrapS();
2372 GLenum wrapT = texture->getWrapT();
2373 GLenum minFilter = texture->getMinFilter();
2374 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002376 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2377 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002379 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002380 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2381 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002382 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2383 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002384 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002386 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002387 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002388 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002389 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002390 }
2391 else
2392 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002393 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002394 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002395
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002396 appliedTextureSerial[samplerIndex] = texSerial;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002397 texture->resetDirty();
2398 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002400 else
2401 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002402 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002403 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002404 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002405 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002406 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002407 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002409
2410 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2411 {
2412 if (appliedTextureSerial[samplerIndex] != 0)
2413 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002414 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002415 appliedTextureSerial[samplerIndex] = 0;
2416 }
2417 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418}
2419
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002420void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2421 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002423 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002424
2425 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2426 {
2427 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2428 }
2429
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002430 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2431 {
2432 return error(GL_INVALID_OPERATION);
2433 }
2434
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002435 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2436 // sized query sanity check
2437 if (bufSize)
2438 {
2439 int requiredSize = outputPitch * height;
2440 if (requiredSize > *bufSize)
2441 {
2442 return error(GL_INVALID_OPERATION);
2443 }
2444 }
2445
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002447 if (!renderTarget)
2448 {
2449 return; // Context must be lost, return silently
2450 }
2451
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 D3DSURFACE_DESC desc;
2453 renderTarget->GetDesc(&desc);
2454
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002455 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2456 {
2457 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002458 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002459 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460 }
2461
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002462 HRESULT result;
2463 IDirect3DSurface9 *systemSurface = NULL;
2464 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002465 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002466 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2467 if (directToPixels)
2468 {
2469 // Use the pixels ptr as a shared handle to write directly into client's memory
2470 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2471 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2472 if (FAILED(result))
2473 {
2474 // Try again without the shared handle
2475 directToPixels = false;
2476 }
2477 }
2478
2479 if (!directToPixels)
2480 {
2481 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2482 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2483 if (FAILED(result))
2484 {
2485 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00002486 renderTarget->Release();
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002487 return error(GL_OUT_OF_MEMORY);
2488 }
2489 }
2490
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002491 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002492 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002493 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002494
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002495 if (FAILED(result))
2496 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 systemSurface->Release();
2498
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002499 // It turns out that D3D will sometimes produce more error
2500 // codes than those documented.
2501 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002502 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002503 else
2504 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002505 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002506 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002507 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002508
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002509 }
2510
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002511 if (directToPixels)
2512 {
2513 systemSurface->Release();
2514 return;
2515 }
2516
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002517 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002518 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2519 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2520 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2521 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2522 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523
2524 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2525
2526 if (FAILED(result))
2527 {
2528 UNREACHABLE();
2529 systemSurface->Release();
2530
2531 return; // No sensible error to generate
2532 }
2533
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002535 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002536
2537 unsigned char *source;
2538 int inputPitch;
2539 if (getPackReverseRowOrder())
2540 {
2541 source = (unsigned char*)lock.pBits;
2542 inputPitch = lock.Pitch;
2543 }
2544 else
2545 {
2546 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2547 inputPitch = -lock.Pitch;
2548 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002549
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550 for (int j = 0; j < rect.bottom - rect.top; j++)
2551 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002552 if (desc.Format == D3DFMT_A8R8G8B8 &&
2553 format == GL_BGRA_EXT &&
2554 type == GL_UNSIGNED_BYTE)
2555 {
2556 // Fast path for EXT_read_format_bgra, given
2557 // an RGBA source buffer. Note that buffers with no
2558 // alpha go through the slow path below.
2559 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002560 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002561 (rect.right - rect.left) * 4);
2562 continue;
2563 }
2564
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565 for (int i = 0; i < rect.right - rect.left; i++)
2566 {
2567 float r;
2568 float g;
2569 float b;
2570 float a;
2571
2572 switch (desc.Format)
2573 {
2574 case D3DFMT_R5G6B5:
2575 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002576 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577
2578 a = 1.0f;
2579 b = (rgb & 0x001F) * (1.0f / 0x001F);
2580 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2581 r = (rgb & 0xF800) * (1.0f / 0xF800);
2582 }
2583 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584 case D3DFMT_A1R5G5B5:
2585 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002586 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002587
2588 a = (argb & 0x8000) ? 1.0f : 0.0f;
2589 b = (argb & 0x001F) * (1.0f / 0x001F);
2590 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2591 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2592 }
2593 break;
2594 case D3DFMT_A8R8G8B8:
2595 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002596 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597
2598 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2599 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2600 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2601 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2602 }
2603 break;
2604 case D3DFMT_X8R8G8B8:
2605 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002606 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002607
2608 a = 1.0f;
2609 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2610 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2611 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2612 }
2613 break;
2614 case D3DFMT_A2R10G10B10:
2615 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002616 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617
2618 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2619 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2620 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2621 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2622 }
2623 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002624 case D3DFMT_A32B32G32R32F:
2625 {
2626 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002627 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2628 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2629 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2630 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002631 }
2632 break;
2633 case D3DFMT_A16B16G16R16F:
2634 {
2635 // float formats in D3D are stored rgba, rather than the other way round
2636 float abgr[4];
2637
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002638 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002639
2640 a = abgr[3];
2641 b = abgr[2];
2642 g = abgr[1];
2643 r = abgr[0];
2644 }
2645 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646 default:
2647 UNIMPLEMENTED(); // FIXME
2648 UNREACHABLE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002649 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002650 }
2651
2652 switch (format)
2653 {
2654 case GL_RGBA:
2655 switch (type)
2656 {
2657 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002658 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2659 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2660 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2661 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002662 break;
2663 default: UNREACHABLE();
2664 }
2665 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002666 case GL_BGRA_EXT:
2667 switch (type)
2668 {
2669 case GL_UNSIGNED_BYTE:
2670 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2671 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2672 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2673 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2674 break;
2675 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2676 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2677 // this type is packed as follows:
2678 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2679 // --------------------------------------------------------------------------------
2680 // | 4th | 3rd | 2nd | 1st component |
2681 // --------------------------------------------------------------------------------
2682 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2683 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2684 ((unsigned short)(15 * a + 0.5f) << 12)|
2685 ((unsigned short)(15 * r + 0.5f) << 8) |
2686 ((unsigned short)(15 * g + 0.5f) << 4) |
2687 ((unsigned short)(15 * b + 0.5f) << 0);
2688 break;
2689 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2690 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2691 // this type is packed as follows:
2692 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2693 // --------------------------------------------------------------------------------
2694 // | 4th | 3rd | 2nd | 1st component |
2695 // --------------------------------------------------------------------------------
2696 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2697 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2698 ((unsigned short)( a + 0.5f) << 15) |
2699 ((unsigned short)(31 * r + 0.5f) << 10) |
2700 ((unsigned short)(31 * g + 0.5f) << 5) |
2701 ((unsigned short)(31 * b + 0.5f) << 0);
2702 break;
2703 default: UNREACHABLE();
2704 }
2705 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002706 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707 switch (type)
2708 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002709 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002710 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2711 ((unsigned short)(31 * b + 0.5f) << 0) |
2712 ((unsigned short)(63 * g + 0.5f) << 5) |
2713 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714 break;
2715 default: UNREACHABLE();
2716 }
2717 break;
2718 default: UNREACHABLE();
2719 }
2720 }
2721 }
2722
2723 systemSurface->UnlockRect();
2724
2725 systemSurface->Release();
2726}
2727
2728void Context::clear(GLbitfield mask)
2729{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002730 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002731
2732 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2733 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002734 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002735 }
2736
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737 DWORD flags = 0;
2738
2739 if (mask & GL_COLOR_BUFFER_BIT)
2740 {
2741 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002742
2743 if (framebufferObject->getColorbufferType() != GL_NONE)
2744 {
2745 flags |= D3DCLEAR_TARGET;
2746 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002747 }
2748
2749 if (mask & GL_DEPTH_BUFFER_BIT)
2750 {
2751 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002752 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002753 {
2754 flags |= D3DCLEAR_ZBUFFER;
2755 }
2756 }
2757
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758 GLuint stencilUnmasked = 0x0;
2759
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002760 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002761 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002763 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002765 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002766 if (!depthStencil)
2767 {
2768 ERR("Depth stencil pointer unexpectedly null.");
2769 return;
2770 }
2771
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002772 D3DSURFACE_DESC desc;
2773 depthStencil->GetDesc(&desc);
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00002774 depthStencil->Release();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002775
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002776 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002777 stencilUnmasked = (0x1 << stencilSize) - 1;
2778
2779 if (stencilUnmasked != 0x0)
2780 {
2781 flags |= D3DCLEAR_STENCIL;
2782 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002783 }
2784 }
2785
2786 if (mask != 0)
2787 {
2788 return error(GL_INVALID_VALUE);
2789 }
2790
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002791 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2792 {
2793 return;
2794 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002796 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002797 unorm<8>(mState.colorClearValue.red),
2798 unorm<8>(mState.colorClearValue.green),
2799 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002800 float depth = clamp01(mState.depthClearValue);
2801 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002802
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002803 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804
2805 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002806 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002808 !(mState.colorMaskRed && mState.colorMaskGreen &&
2809 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810
2811 if (needMaskedColorClear || needMaskedStencilClear)
2812 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002813 // State which is altered in all paths from this point to the clear call is saved.
2814 // State which is altered in only some paths will be flagged dirty in the case that
2815 // that path is taken.
2816 HRESULT hr;
2817 if (mMaskedClearSavedState == NULL)
2818 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002819 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002820 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2821
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002822 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2823 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2824 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2825 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2826 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2827 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2828 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2829 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2830 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2831 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2832 mDevice->SetPixelShader(NULL);
2833 mDevice->SetVertexShader(NULL);
2834 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2835 mDevice->SetStreamSource(0, NULL, 0, 0);
2836 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2837 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2838 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2839 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2840 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2841 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2842 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002843
2844 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2845 {
2846 mDevice->SetStreamSourceFreq(i, 1);
2847 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002848
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002849 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002850 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2851 }
2852
2853 ASSERT(mMaskedClearSavedState != NULL);
2854
2855 if (mMaskedClearSavedState != NULL)
2856 {
2857 hr = mMaskedClearSavedState->Capture();
2858 ASSERT(SUCCEEDED(hr));
2859 }
2860
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002861 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2862 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2863 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2864 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2865 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2866 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2867 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2868 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869
2870 if (flags & D3DCLEAR_TARGET)
2871 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002872 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873 }
2874 else
2875 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002876 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002877 }
2878
2879 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2880 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002881 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2882 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2883 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2884 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2885 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2886 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2887 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2888 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002889 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002890 }
2891 else
2892 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002893 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002894 }
2895
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002896 mDevice->SetPixelShader(NULL);
2897 mDevice->SetVertexShader(NULL);
2898 mDevice->SetFVF(D3DFVF_XYZRHW);
2899 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2900 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2901 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2902 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2903 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2904 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2905 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002906
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002907 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2908 {
2909 mDevice->SetStreamSourceFreq(i, 1);
2910 }
2911
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002912 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2913 quad[0][0] = -0.5f;
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002914 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002915 quad[0][2] = 0.0f;
2916 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002917
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002918 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2919 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002920 quad[1][2] = 0.0f;
2921 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002922
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002923 quad[2][0] = -0.5f;
2924 quad[2][1] = -0.5f;
2925 quad[2][2] = 0.0f;
2926 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002927
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002928 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002929 quad[3][1] = -0.5f;
2930 quad[3][2] = 0.0f;
2931 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002932
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002933 mDisplay->startScene();
2934 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002935
2936 if (flags & D3DCLEAR_ZBUFFER)
2937 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002938 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2939 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2940 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002942
2943 if (mMaskedClearSavedState != NULL)
2944 {
2945 mMaskedClearSavedState->Apply();
2946 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002947 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002948 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002949 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002950 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002951 }
2952}
2953
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002954void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002955{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002956 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002957 {
2958 return error(GL_INVALID_OPERATION);
2959 }
2960
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002961 D3DPRIMITIVETYPE primitiveType;
2962 int primitiveCount;
2963
2964 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2965 return error(GL_INVALID_ENUM);
2966
2967 if (primitiveCount <= 0)
2968 {
2969 return;
2970 }
2971
2972 if (!applyRenderTarget(false))
2973 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002974 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002975 }
2976
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002977 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002978
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002979 GLsizei repeatDraw = 1;
2980 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002981 if (err != GL_NO_ERROR)
2982 {
2983 return error(err);
2984 }
2985
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002986 applyShaders();
2987 applyTextures();
2988
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002989 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002990 {
2991 return error(GL_INVALID_OPERATION);
2992 }
2993
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002994 if (!cullSkipsDraw(mode))
2995 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002996 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002997
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002998 if (mode == GL_LINE_LOOP)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002999 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003000 drawLineLoop(count, GL_NONE, NULL, 0);
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003001 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003002 else if (instances > 0)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003003 {
3004 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
3005 if (countingIB)
3006 {
3007 if (mAppliedIBSerial != countingIB->getSerial())
3008 {
3009 mDevice->SetIndices(countingIB->getBuffer());
3010 mAppliedIBSerial = countingIB->getSerial();
3011 }
3012
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003013 for (int i = 0; i < repeatDraw; i++)
3014 {
3015 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
3016 }
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003017 }
3018 else
3019 {
3020 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
3021 return error(GL_OUT_OF_MEMORY);
3022 }
3023 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003024 else // Regular case
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003025 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003026 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003027 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003029}
3030
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00003031void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003032{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003033 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003034 {
3035 return error(GL_INVALID_OPERATION);
3036 }
3037
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003038 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003039 {
3040 return error(GL_INVALID_OPERATION);
3041 }
3042
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003043 D3DPRIMITIVETYPE primitiveType;
3044 int primitiveCount;
3045
3046 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3047 return error(GL_INVALID_ENUM);
3048
3049 if (primitiveCount <= 0)
3050 {
3051 return;
3052 }
3053
3054 if (!applyRenderTarget(false))
3055 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00003056 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003057 }
3058
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003059 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00003060
3061 TranslatedIndexData indexInfo;
3062 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3063 if (err != GL_NO_ERROR)
3064 {
3065 return error(err);
3066 }
3067
daniel@transgaming.com83921382011-01-08 05:46:00 +00003068 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003069 GLsizei repeatDraw = 1;
3070 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00003071 if (err != GL_NO_ERROR)
3072 {
3073 return error(err);
3074 }
3075
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003076 applyShaders();
3077 applyTextures();
3078
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00003079 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003080 {
3081 return error(GL_INVALID_OPERATION);
3082 }
3083
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003084 if (!cullSkipsDraw(mode))
3085 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003086 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003087
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003088 if (mode == GL_LINE_LOOP)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003089 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003090 drawLineLoop(count, type, indices, indexInfo.minIndex);
3091 }
3092 else
3093 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003094 for (int i = 0; i < repeatDraw; i++)
3095 {
3096 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3097 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003098 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003099 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003100}
3101
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00003102// Implements glFlush when block is false, glFinish when block is true
3103void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003104{
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +00003105 mDisplay->sync(block);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003106}
3107
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003108void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003109{
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003110 // Get the raw indices for an indexed draw
3111 if (type != GL_NONE && mState.elementArrayBuffer.get())
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003112 {
3113 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3114 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3115 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3116 }
3117
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003118 UINT startIndex = 0;
3119 bool succeeded = false;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003120
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003121 if (supports32bitIndices())
3122 {
3123 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3124
3125 if (!mLineLoopIB)
3126 {
3127 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3128 }
3129
3130 if (mLineLoopIB)
3131 {
3132 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3133
3134 UINT offset = 0;
3135 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3136 startIndex = offset / 4;
3137
3138 if (data)
3139 {
3140 switch (type)
3141 {
3142 case GL_NONE: // Non-indexed draw
3143 for (int i = 0; i < count; i++)
3144 {
3145 data[i] = i;
3146 }
3147 data[count] = 0;
3148 break;
3149 case GL_UNSIGNED_BYTE:
3150 for (int i = 0; i < count; i++)
3151 {
3152 data[i] = static_cast<const GLubyte*>(indices)[i];
3153 }
3154 data[count] = static_cast<const GLubyte*>(indices)[0];
3155 break;
3156 case GL_UNSIGNED_SHORT:
3157 for (int i = 0; i < count; i++)
3158 {
3159 data[i] = static_cast<const GLushort*>(indices)[i];
3160 }
3161 data[count] = static_cast<const GLushort*>(indices)[0];
3162 break;
3163 case GL_UNSIGNED_INT:
3164 for (int i = 0; i < count; i++)
3165 {
3166 data[i] = static_cast<const GLuint*>(indices)[i];
3167 }
3168 data[count] = static_cast<const GLuint*>(indices)[0];
3169 break;
3170 default: UNREACHABLE();
3171 }
3172
3173 mLineLoopIB->unmap();
3174 succeeded = true;
3175 }
3176 }
3177 }
3178 else
3179 {
3180 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3181
3182 if (!mLineLoopIB)
3183 {
3184 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3185 }
3186
3187 if (mLineLoopIB)
3188 {
3189 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3190
3191 UINT offset = 0;
3192 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3193 startIndex = offset / 2;
3194
3195 if (data)
3196 {
3197 switch (type)
3198 {
3199 case GL_NONE: // Non-indexed draw
3200 for (int i = 0; i < count; i++)
3201 {
3202 data[i] = i;
3203 }
3204 data[count] = 0;
3205 break;
3206 case GL_UNSIGNED_BYTE:
3207 for (int i = 0; i < count; i++)
3208 {
3209 data[i] = static_cast<const GLubyte*>(indices)[i];
3210 }
3211 data[count] = static_cast<const GLubyte*>(indices)[0];
3212 break;
3213 case GL_UNSIGNED_SHORT:
3214 for (int i = 0; i < count; i++)
3215 {
3216 data[i] = static_cast<const GLushort*>(indices)[i];
3217 }
3218 data[count] = static_cast<const GLushort*>(indices)[0];
3219 break;
3220 case GL_UNSIGNED_INT:
3221 for (int i = 0; i < count; i++)
3222 {
3223 data[i] = static_cast<const GLuint*>(indices)[i];
3224 }
3225 data[count] = static_cast<const GLuint*>(indices)[0];
3226 break;
3227 default: UNREACHABLE();
3228 }
3229
3230 mLineLoopIB->unmap();
3231 succeeded = true;
3232 }
3233 }
3234 }
3235
3236 if (succeeded)
3237 {
3238 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3239 {
3240 mDevice->SetIndices(mLineLoopIB->getBuffer());
3241 mAppliedIBSerial = mLineLoopIB->getSerial();
3242 }
3243
3244 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3245 }
3246 else
3247 {
3248 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3249 return error(GL_OUT_OF_MEMORY);
3250 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003251}
3252
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003253void Context::recordInvalidEnum()
3254{
3255 mInvalidEnum = true;
3256}
3257
3258void Context::recordInvalidValue()
3259{
3260 mInvalidValue = true;
3261}
3262
3263void Context::recordInvalidOperation()
3264{
3265 mInvalidOperation = true;
3266}
3267
3268void Context::recordOutOfMemory()
3269{
3270 mOutOfMemory = true;
3271}
3272
3273void Context::recordInvalidFramebufferOperation()
3274{
3275 mInvalidFramebufferOperation = true;
3276}
3277
3278// Get one of the recorded errors and clear its flag, if any.
3279// [OpenGL ES 2.0.24] section 2.5 page 13.
3280GLenum Context::getError()
3281{
3282 if (mInvalidEnum)
3283 {
3284 mInvalidEnum = false;
3285
3286 return GL_INVALID_ENUM;
3287 }
3288
3289 if (mInvalidValue)
3290 {
3291 mInvalidValue = false;
3292
3293 return GL_INVALID_VALUE;
3294 }
3295
3296 if (mInvalidOperation)
3297 {
3298 mInvalidOperation = false;
3299
3300 return GL_INVALID_OPERATION;
3301 }
3302
3303 if (mOutOfMemory)
3304 {
3305 mOutOfMemory = false;
3306
3307 return GL_OUT_OF_MEMORY;
3308 }
3309
3310 if (mInvalidFramebufferOperation)
3311 {
3312 mInvalidFramebufferOperation = false;
3313
3314 return GL_INVALID_FRAMEBUFFER_OPERATION;
3315 }
3316
3317 return GL_NO_ERROR;
3318}
3319
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003320GLenum Context::getResetStatus()
3321{
3322 if (mResetStatus == GL_NO_ERROR)
3323 {
3324 bool lost = mDisplay->testDeviceLost();
3325
3326 if (lost)
3327 {
3328 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3329 }
3330 }
3331
3332 GLenum status = mResetStatus;
3333
3334 if (mResetStatus != GL_NO_ERROR)
3335 {
3336 if (mDisplay->testDeviceResettable())
3337 {
3338 mResetStatus = GL_NO_ERROR;
3339 }
3340 }
3341
3342 return status;
3343}
3344
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003345bool Context::isResetNotificationEnabled()
3346{
3347 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3348}
3349
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003350bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003351{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003352 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003353}
3354
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003355int Context::getMaximumVaryingVectors() const
3356{
3357 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3358}
3359
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003360unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003361{
3362 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3363}
3364
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003365unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003366{
3367 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3368}
3369
daniel@transgaming.com458da142010-11-28 02:03:02 +00003370int Context::getMaximumFragmentUniformVectors() const
3371{
3372 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3373}
3374
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003375int Context::getMaxSupportedSamples() const
3376{
3377 return mMaxSupportedSamples;
3378}
3379
3380int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3381{
3382 if (requested == 0)
3383 {
3384 return requested;
3385 }
3386
3387 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3388 if (itr == mMultiSampleSupport.end())
3389 {
3390 return -1;
3391 }
3392
3393 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3394 {
3395 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3396 {
3397 return i;
3398 }
3399 }
3400
3401 return -1;
3402}
3403
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003404bool Context::supportsEventQueries() const
3405{
3406 return mSupportsEventQueries;
3407}
3408
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003409bool Context::supportsOcclusionQueries() const
3410{
3411 return mSupportsOcclusionQueries;
3412}
3413
gman@chromium.org50c526d2011-08-10 05:19:44 +00003414bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003415{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003416 return mSupportsDXT1Textures;
3417}
3418
3419bool Context::supportsDXT3Textures() const
3420{
3421 return mSupportsDXT3Textures;
3422}
3423
3424bool Context::supportsDXT5Textures() const
3425{
3426 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003427}
3428
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003429bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003430{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003431 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003432}
3433
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003434bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003435{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003436 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003437}
3438
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003439bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003440{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003441 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003442}
3443
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003444bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003445{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003446 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003447}
3448
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003449bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003450{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003451 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003452}
3453
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003454bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003455{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003456 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003457}
3458
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003459int Context::getMaximumRenderbufferDimension() const
3460{
3461 return mMaxRenderbufferDimension;
3462}
3463
3464int Context::getMaximumTextureDimension() const
3465{
3466 return mMaxTextureDimension;
3467}
3468
3469int Context::getMaximumCubeTextureDimension() const
3470{
3471 return mMaxCubeTextureDimension;
3472}
3473
3474int Context::getMaximumTextureLevel() const
3475{
3476 return mMaxTextureLevel;
3477}
3478
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003479bool Context::supportsLuminanceTextures() const
3480{
3481 return mSupportsLuminanceTextures;
3482}
3483
3484bool Context::supportsLuminanceAlphaTextures() const
3485{
3486 return mSupportsLuminanceAlphaTextures;
3487}
3488
daniel@transgaming.com1c49f792012-05-31 01:14:02 +00003489bool Context::supportsDepthTextures() const
3490{
3491 return mSupportsDepthTextures;
3492}
3493
daniel@transgaming.com83921382011-01-08 05:46:00 +00003494bool Context::supports32bitIndices() const
3495{
3496 return mSupports32bitIndices;
3497}
3498
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003499bool Context::supportsNonPower2Texture() const
3500{
3501 return mSupportsNonPower2Texture;
3502}
3503
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003504bool Context::supportsInstancing() const
3505{
3506 return mSupportsInstancing;
3507}
3508
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003509void Context::detachBuffer(GLuint buffer)
3510{
3511 // [OpenGL ES 2.0.24] section 2.9 page 22:
3512 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3513 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3514
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003515 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003516 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003517 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003518 }
3519
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003520 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003521 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003522 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003523 }
3524
3525 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3526 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003527 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003528 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003529 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003530 }
3531 }
3532}
3533
3534void Context::detachTexture(GLuint texture)
3535{
3536 // [OpenGL ES 2.0.24] section 3.8 page 84:
3537 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3538 // rebound to texture object zero
3539
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003540 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003541 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003542 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003543 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003544 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003545 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003546 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003547 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003548 }
3549 }
3550
3551 // [OpenGL ES 2.0.24] section 4.4 page 112:
3552 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3553 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3554 // image was attached in the currently bound framebuffer.
3555
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003556 Framebuffer *readFramebuffer = getReadFramebuffer();
3557 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003558
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003559 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003560 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003561 readFramebuffer->detachTexture(texture);
3562 }
3563
3564 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3565 {
3566 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003567 }
3568}
3569
3570void Context::detachFramebuffer(GLuint framebuffer)
3571{
3572 // [OpenGL ES 2.0.24] section 4.4 page 107:
3573 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3574 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3575
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003576 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003577 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003578 bindReadFramebuffer(0);
3579 }
3580
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003581 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003582 {
3583 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003584 }
3585}
3586
3587void Context::detachRenderbuffer(GLuint renderbuffer)
3588{
3589 // [OpenGL ES 2.0.24] section 4.4 page 109:
3590 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3591 // had been executed with the target RENDERBUFFER and name of zero.
3592
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003593 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003594 {
3595 bindRenderbuffer(0);
3596 }
3597
3598 // [OpenGL ES 2.0.24] section 4.4 page 111:
3599 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3600 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3601 // point to which this image was attached in the currently bound framebuffer.
3602
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003603 Framebuffer *readFramebuffer = getReadFramebuffer();
3604 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003605
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003606 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003607 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003608 readFramebuffer->detachRenderbuffer(renderbuffer);
3609 }
3610
3611 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3612 {
3613 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003614 }
3615}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003616
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003617Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003618{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003619 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003620
3621 if (t == NULL)
3622 {
3623 static const GLubyte color[] = { 0, 0, 0, 255 };
3624
3625 switch (type)
3626 {
3627 default:
3628 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003629 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003630
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003631 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003632 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003633 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003634 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003635 t = incomplete2d;
3636 }
3637 break;
3638
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003639 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003640 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003641 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003642
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003643 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3644 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3645 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3646 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3647 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3648 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003649
3650 t = incompleteCube;
3651 }
3652 break;
3653 }
3654
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003655 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003656 }
3657
3658 return t;
3659}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003660
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003661bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003662{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003663 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003664}
3665
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003666bool Context::isTriangleMode(GLenum drawMode)
3667{
3668 switch (drawMode)
3669 {
3670 case GL_TRIANGLES:
3671 case GL_TRIANGLE_FAN:
3672 case GL_TRIANGLE_STRIP:
3673 return true;
3674 case GL_POINTS:
3675 case GL_LINES:
3676 case GL_LINE_LOOP:
3677 case GL_LINE_STRIP:
3678 return false;
3679 default: UNREACHABLE();
3680 }
3681
3682 return false;
3683}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003684
3685void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3686{
3687 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3688
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003689 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3690 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3691 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3692 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003693
daniel@transgaming.com83921382011-01-08 05:46:00 +00003694 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003695}
3696
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00003697void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3698{
3699 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3700
3701 mState.vertexAttribute[index].mDivisor = divisor;
3702}
3703
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003704// keep list sorted in following order
3705// OES extensions
3706// EXT extensions
3707// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003708void Context::initExtensionString()
3709{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003710 mExtensionString = "";
3711
3712 // OES extensions
3713 if (supports32bitIndices())
3714 {
3715 mExtensionString += "GL_OES_element_index_uint ";
3716 }
3717
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003718 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003719 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003720 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003721
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003722 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003723 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003724 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003725 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003726 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003727 {
3728 mExtensionString += "GL_OES_texture_half_float_linear ";
3729 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003730 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003731 {
3732 mExtensionString += "GL_OES_texture_float ";
3733 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003734 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003735 {
3736 mExtensionString += "GL_OES_texture_float_linear ";
3737 }
3738
3739 if (supportsNonPower2Texture())
3740 {
3741 mExtensionString += "GL_OES_texture_npot ";
3742 }
3743
3744 // Multi-vendor (EXT) extensions
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003745 if (supportsOcclusionQueries())
3746 {
3747 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3748 }
3749
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003750 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003751 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003752
gman@chromium.org50c526d2011-08-10 05:19:44 +00003753 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003754 {
3755 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3756 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003757
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003758 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.comdf363722011-12-16 23:29:53 +00003759 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003760
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003761 // ANGLE-specific extensions
daniel@transgaming.com92d620c2012-05-31 01:16:31 +00003762 if (supportsDepthTextures())
3763 {
3764 mExtensionString += "GL_ANGLE_depth_texture ";
3765 }
3766
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003767 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003768 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003769 {
3770 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3771 }
3772
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003773 if (supportsInstancing())
daniel@transgaming.comdce02fd2012-01-27 15:39:51 +00003774 {
3775 mExtensionString += "GL_ANGLE_instanced_arrays ";
3776 }
3777
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003778 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3779
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003780 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003781 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003782 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3783 }
3784 if (supportsDXT5Textures())
3785 {
3786 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003787 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003788
3789 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003790 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003791
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003792 // Other vendor-specific extensions
3793 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003794 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003795 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003796 }
3797
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003798 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3799 if (end != std::string::npos)
3800 {
3801 mExtensionString.resize(end+1);
3802 }
3803}
3804
3805const char *Context::getExtensionString() const
3806{
3807 return mExtensionString.c_str();
3808}
3809
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003810void Context::initRendererString()
3811{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003812 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003813
3814 mRendererString = "ANGLE (";
3815 mRendererString += identifier->Description;
3816 mRendererString += ")";
3817}
3818
3819const char *Context::getRendererString() const
3820{
3821 return mRendererString.c_str();
3822}
3823
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003824void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3825 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3826 GLbitfield mask)
3827{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003828 Framebuffer *readFramebuffer = getReadFramebuffer();
3829 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3830
3831 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3832 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3833 {
3834 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3835 }
3836
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003837 if (drawFramebuffer->getSamples() != 0)
3838 {
3839 return error(GL_INVALID_OPERATION);
3840 }
3841
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003842 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3843 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3844 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3845 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3846
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003847 RECT sourceRect;
3848 RECT destRect;
3849
3850 if (srcX0 < srcX1)
3851 {
3852 sourceRect.left = srcX0;
3853 sourceRect.right = srcX1;
3854 destRect.left = dstX0;
3855 destRect.right = dstX1;
3856 }
3857 else
3858 {
3859 sourceRect.left = srcX1;
3860 destRect.left = dstX1;
3861 sourceRect.right = srcX0;
3862 destRect.right = dstX0;
3863 }
3864
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003865 if (srcY0 < srcY1)
3866 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003867 sourceRect.top = readBufferHeight - srcY1;
3868 destRect.top = drawBufferHeight - dstY1;
3869 sourceRect.bottom = readBufferHeight - srcY0;
3870 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003871 }
3872 else
3873 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003874 sourceRect.top = readBufferHeight - srcY0;
3875 destRect.top = drawBufferHeight - dstY0;
3876 sourceRect.bottom = readBufferHeight - srcY1;
3877 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003878 }
3879
3880 RECT sourceScissoredRect = sourceRect;
3881 RECT destScissoredRect = destRect;
3882
3883 if (mState.scissorTest)
3884 {
3885 // Only write to parts of the destination framebuffer which pass the scissor test
3886 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3887 // rect will be checked against scissorY, rather than the bottom.
3888 if (destRect.left < mState.scissorX)
3889 {
3890 int xDiff = mState.scissorX - destRect.left;
3891 destScissoredRect.left = mState.scissorX;
3892 sourceScissoredRect.left += xDiff;
3893 }
3894
3895 if (destRect.right > mState.scissorX + mState.scissorWidth)
3896 {
3897 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3898 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3899 sourceScissoredRect.right -= xDiff;
3900 }
3901
3902 if (destRect.top < mState.scissorY)
3903 {
3904 int yDiff = mState.scissorY - destRect.top;
3905 destScissoredRect.top = mState.scissorY;
3906 sourceScissoredRect.top += yDiff;
3907 }
3908
3909 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3910 {
3911 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3912 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3913 sourceScissoredRect.bottom -= yDiff;
3914 }
3915 }
3916
3917 bool blitRenderTarget = false;
3918 bool blitDepthStencil = false;
3919
3920 RECT sourceTrimmedRect = sourceScissoredRect;
3921 RECT destTrimmedRect = destScissoredRect;
3922
3923 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3924 // the actual draw and read surfaces.
3925 if (sourceTrimmedRect.left < 0)
3926 {
3927 int xDiff = 0 - sourceTrimmedRect.left;
3928 sourceTrimmedRect.left = 0;
3929 destTrimmedRect.left += xDiff;
3930 }
3931
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003932 if (sourceTrimmedRect.right > readBufferWidth)
3933 {
3934 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3935 sourceTrimmedRect.right = readBufferWidth;
3936 destTrimmedRect.right -= xDiff;
3937 }
3938
3939 if (sourceTrimmedRect.top < 0)
3940 {
3941 int yDiff = 0 - sourceTrimmedRect.top;
3942 sourceTrimmedRect.top = 0;
3943 destTrimmedRect.top += yDiff;
3944 }
3945
3946 if (sourceTrimmedRect.bottom > readBufferHeight)
3947 {
3948 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3949 sourceTrimmedRect.bottom = readBufferHeight;
3950 destTrimmedRect.bottom -= yDiff;
3951 }
3952
3953 if (destTrimmedRect.left < 0)
3954 {
3955 int xDiff = 0 - destTrimmedRect.left;
3956 destTrimmedRect.left = 0;
3957 sourceTrimmedRect.left += xDiff;
3958 }
3959
3960 if (destTrimmedRect.right > drawBufferWidth)
3961 {
3962 int xDiff = destTrimmedRect.right - drawBufferWidth;
3963 destTrimmedRect.right = drawBufferWidth;
3964 sourceTrimmedRect.right -= xDiff;
3965 }
3966
3967 if (destTrimmedRect.top < 0)
3968 {
3969 int yDiff = 0 - destTrimmedRect.top;
3970 destTrimmedRect.top = 0;
3971 sourceTrimmedRect.top += yDiff;
3972 }
3973
3974 if (destTrimmedRect.bottom > drawBufferHeight)
3975 {
3976 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3977 destTrimmedRect.bottom = drawBufferHeight;
3978 sourceTrimmedRect.bottom -= yDiff;
3979 }
3980
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003981 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003982 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3983 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3984 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3985 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003986 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3987 {
3988 partialBufferCopy = true;
3989 }
3990
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003991 if (mask & GL_COLOR_BUFFER_BIT)
3992 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003993 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3994 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3995 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3996 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3997 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003998 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3999 {
4000 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
4001 return error(GL_INVALID_OPERATION);
4002 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004003
4004 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
4005 {
4006 return error(GL_INVALID_OPERATION);
4007 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004008
4009 blitRenderTarget = true;
4010
4011 }
4012
4013 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
4014 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00004015 Renderbuffer *readDSBuffer = NULL;
4016 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004017
4018 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
4019 // both a depth and stencil buffer, it will be the same buffer.
4020
4021 if (mask & GL_DEPTH_BUFFER_BIT)
4022 {
4023 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
4024 {
4025 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
4026 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
4027 {
4028 return error(GL_INVALID_OPERATION);
4029 }
4030
4031 blitDepthStencil = true;
4032 readDSBuffer = readFramebuffer->getDepthbuffer();
4033 drawDSBuffer = drawFramebuffer->getDepthbuffer();
4034 }
4035 }
4036
4037 if (mask & GL_STENCIL_BUFFER_BIT)
4038 {
4039 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4040 {
4041 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
4042 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
4043 {
4044 return error(GL_INVALID_OPERATION);
4045 }
4046
4047 blitDepthStencil = true;
4048 readDSBuffer = readFramebuffer->getStencilbuffer();
4049 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4050 }
4051 }
4052
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004053 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004054 {
4055 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4056 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4057 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004058
daniel@transgaming.com97446d22010-08-24 19:20:54 +00004059 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4060 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004061 {
4062 return error(GL_INVALID_OPERATION);
4063 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004064 }
4065
4066 if (blitRenderTarget || blitDepthStencil)
4067 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004068 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004069
4070 if (blitRenderTarget)
4071 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00004072 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4073 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4074
4075 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4076 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4077
4078 readRenderTarget->Release();
4079 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004080
4081 if (FAILED(result))
4082 {
4083 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4084 return;
4085 }
4086 }
4087
4088 if (blitDepthStencil)
4089 {
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00004090 IDirect3DSurface9* readDepthStencil = readFramebuffer->getDepthStencil();
4091 IDirect3DSurface9* drawDepthStencil = drawFramebuffer->getDepthStencil();
4092
4093 HRESULT result = mDevice->StretchRect(readDepthStencil, NULL, drawDepthStencil, NULL, D3DTEXF_NONE);
4094
4095 readDepthStencil->Release();
4096 drawDepthStencil->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004097
4098 if (FAILED(result))
4099 {
4100 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4101 return;
4102 }
4103 }
4104 }
4105}
4106
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004107VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4108{
4109 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4110 {
4111 mVertexDeclCache[i].vertexDeclaration = NULL;
4112 mVertexDeclCache[i].lruCount = 0;
4113 }
4114}
4115
4116VertexDeclarationCache::~VertexDeclarationCache()
4117{
4118 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4119 {
4120 if (mVertexDeclCache[i].vertexDeclaration)
4121 {
4122 mVertexDeclCache[i].vertexDeclaration->Release();
4123 }
4124 }
4125}
4126
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004127GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004128{
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004129 *repeatDraw = 1;
4130
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004131 int indexedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004132 int instancedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004133
4134 if (instances > 0)
4135 {
4136 // Find an indexed attribute to be mapped to D3D stream 0
4137 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4138 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004139 if (attributes[i].active)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004140 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004141 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4142 {
4143 if (attributes[i].divisor == 0)
4144 {
4145 indexedAttribute = i;
4146 }
4147 }
4148 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4149 {
4150 if (attributes[i].divisor != 0)
4151 {
4152 instancedAttribute = i;
4153 }
4154 }
4155 else break; // Found both an indexed and instanced attribute
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004156 }
4157 }
4158
4159 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4160 {
4161 return GL_INVALID_OPERATION;
4162 }
4163 }
4164
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004165 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4166 D3DVERTEXELEMENT9 *element = &elements[0];
4167
4168 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4169 {
4170 if (attributes[i].active)
4171 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004172 int stream = i;
4173
4174 if (instances > 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004175 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004176 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4177 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004178 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004179 *repeatDraw = instances;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004180 }
4181 else
4182 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004183 if (i == indexedAttribute)
4184 {
4185 stream = 0;
4186 }
4187 else if (i == 0)
4188 {
4189 stream = indexedAttribute;
4190 }
4191
4192 UINT frequency = 1;
4193
4194 if (attributes[i].divisor == 0)
4195 {
4196 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4197 }
4198 else
4199 {
4200 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4201 }
4202
4203 device->SetStreamSourceFreq(stream, frequency);
4204 mInstancingEnabled = true;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004205 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004206 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004207
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004208 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4209 mAppliedVBs[stream].stride != attributes[i].stride ||
4210 mAppliedVBs[stream].offset != attributes[i].offset)
4211 {
4212 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4213 mAppliedVBs[stream].serial = attributes[i].serial;
4214 mAppliedVBs[stream].stride = attributes[i].stride;
4215 mAppliedVBs[stream].offset = attributes[i].offset;
4216 }
4217
4218 element->Stream = stream;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004219 element->Offset = 0;
4220 element->Type = attributes[i].type;
4221 element->Method = D3DDECLMETHOD_DEFAULT;
4222 element->Usage = D3DDECLUSAGE_TEXCOORD;
4223 element->UsageIndex = program->getSemanticIndex(i);
4224 element++;
4225 }
4226 }
4227
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004228 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004229 {
4230 if (mInstancingEnabled)
4231 {
4232 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4233 {
4234 device->SetStreamSourceFreq(i, 1);
4235 }
4236
4237 mInstancingEnabled = false;
4238 }
4239 }
4240
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004241 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4242 *(element++) = end;
4243
4244 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4245 {
4246 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4247 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4248 {
4249 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004250 if(entry->vertexDeclaration != mLastSetVDecl)
4251 {
4252 device->SetVertexDeclaration(entry->vertexDeclaration);
4253 mLastSetVDecl = entry->vertexDeclaration;
4254 }
4255
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004256 return GL_NO_ERROR;
4257 }
4258 }
4259
4260 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4261
4262 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4263 {
4264 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4265 {
4266 lastCache = &mVertexDeclCache[i];
4267 }
4268 }
4269
4270 if (lastCache->vertexDeclaration != NULL)
4271 {
4272 lastCache->vertexDeclaration->Release();
4273 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004274 // mLastSetVDecl is set to the replacement, so we don't have to worry
4275 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004276 }
4277
4278 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4279 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4280 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004281 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004282 lastCache->lruCount = ++mMaxLru;
4283
4284 return GL_NO_ERROR;
4285}
4286
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004287void VertexDeclarationCache::markStateDirty()
4288{
4289 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4290 {
4291 mAppliedVBs[i].serial = 0;
4292 }
4293
4294 mLastSetVDecl = NULL;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004295 mInstancingEnabled = true; // Forces it to be disabled when not used
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004296}
4297
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004298}
4299
4300extern "C"
4301{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004302gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004303{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004304 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004305}
4306
4307void glDestroyContext(gl::Context *context)
4308{
4309 delete context;
4310
4311 if (context == gl::getContext())
4312 {
4313 gl::makeCurrent(NULL, NULL, NULL);
4314 }
4315}
4316
4317void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4318{
4319 gl::makeCurrent(context, display, surface);
4320}
4321
4322gl::Context *glGetCurrentContext()
4323{
4324 return gl::getContext();
4325}
4326}