blob: 287272428941430c8fdc8017f01947a8c18f1d53 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002// Copyright (c) 2002-2011 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"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000028#include "libGLESv2/VertexDataManager.h"
29#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com86487c22010-03-11 19:41:43 +000031#undef near
32#undef far
33
jbauman@chromium.org399c35f2011-04-28 23:19:51 +000034namespace
35{
36 enum { CLOSING_INDEX_BUFFER_SIZE = 4096 };
37}
38
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039namespace gl
40{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +000041Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042{
benvanik@google.com1a233342011-04-28 19:44:39 +000043 mFenceHandleAllocator.setBaseHandle(0);
44
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000046
daniel@transgaming.com428d1582010-05-04 03:35:25 +000047 mState.depthClearValue = 1.0f;
48 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049
daniel@transgaming.com428d1582010-05-04 03:35:25 +000050 mState.cullFace = false;
51 mState.cullMode = GL_BACK;
52 mState.frontFace = GL_CCW;
53 mState.depthTest = false;
54 mState.depthFunc = GL_LESS;
55 mState.blend = false;
56 mState.sourceBlendRGB = GL_ONE;
57 mState.sourceBlendAlpha = GL_ONE;
58 mState.destBlendRGB = GL_ZERO;
59 mState.destBlendAlpha = GL_ZERO;
60 mState.blendEquationRGB = GL_FUNC_ADD;
61 mState.blendEquationAlpha = GL_FUNC_ADD;
62 mState.blendColor.red = 0;
63 mState.blendColor.green = 0;
64 mState.blendColor.blue = 0;
65 mState.blendColor.alpha = 0;
66 mState.stencilTest = false;
67 mState.stencilFunc = GL_ALWAYS;
68 mState.stencilRef = 0;
69 mState.stencilMask = -1;
70 mState.stencilWritemask = -1;
71 mState.stencilBackFunc = GL_ALWAYS;
72 mState.stencilBackRef = 0;
73 mState.stencilBackMask = - 1;
74 mState.stencilBackWritemask = -1;
75 mState.stencilFail = GL_KEEP;
76 mState.stencilPassDepthFail = GL_KEEP;
77 mState.stencilPassDepthPass = GL_KEEP;
78 mState.stencilBackFail = GL_KEEP;
79 mState.stencilBackPassDepthFail = GL_KEEP;
80 mState.stencilBackPassDepthPass = GL_KEEP;
81 mState.polygonOffsetFill = false;
82 mState.polygonOffsetFactor = 0.0f;
83 mState.polygonOffsetUnits = 0.0f;
84 mState.sampleAlphaToCoverage = false;
85 mState.sampleCoverage = false;
86 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000087 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.scissorTest = false;
89 mState.dither = true;
90 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000091 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092
daniel@transgaming.com428d1582010-05-04 03:35:25 +000093 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000094
daniel@transgaming.com428d1582010-05-04 03:35:25 +000095 mState.viewportX = 0;
96 mState.viewportY = 0;
97 mState.viewportWidth = config->mDisplayMode.Width;
98 mState.viewportHeight = config->mDisplayMode.Height;
99 mState.zNear = 0.0f;
100 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000102 mState.scissorX = 0;
103 mState.scissorY = 0;
104 mState.scissorWidth = config->mDisplayMode.Width;
105 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000107 mState.colorMaskRed = true;
108 mState.colorMaskGreen = true;
109 mState.colorMaskBlue = true;
110 mState.colorMaskAlpha = true;
111 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000113 if (shareContext != NULL)
114 {
115 mResourceManager = shareContext->mResourceManager;
116 mResourceManager->addRef();
117 }
118 else
119 {
120 mResourceManager = new ResourceManager();
121 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 // [OpenGL ES 2.0.24] section 3.7 page 83:
124 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
125 // and cube map texture state vectors respectively associated with them.
126 // In order that access to these initial textures not be lost, they are treated as texture
127 // objects all of whose names are 0.
128
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000129 mTexture2DZero.set(new Texture2D(0));
130 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000132 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000133 bindArrayBuffer(0);
134 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindTextureCubeMap(0);
136 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000137 bindReadFramebuffer(0);
138 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 bindRenderbuffer(0);
140
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000141 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.packAlignment = 4;
144 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000145
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000147 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000148 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000149 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000150
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 mInvalidEnum = false;
152 mInvalidValue = false;
153 mInvalidOperation = false;
154 mOutOfMemory = false;
155 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000156
157 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000158
gman@chromium.org50c526d2011-08-10 05:19:44 +0000159 mSupportsDXT1Textures = false;
160 mSupportsDXT3Textures = false;
161 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000162 mSupportsEventQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000163 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000164 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000165 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000166 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167}
168
169Context::~Context()
170{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000171 if (mState.currentProgram != 0)
172 {
173 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
174 if (programObject)
175 {
176 programObject->release();
177 }
178 mState.currentProgram = 0;
179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000181 while (!mFramebufferMap.empty())
182 {
183 deleteFramebuffer(mFramebufferMap.begin()->first);
184 }
185
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000186 while (!mFenceMap.empty())
187 {
188 deleteFence(mFenceMap.begin()->first);
189 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000190
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000191 while (!mMultiSampleSupport.empty())
192 {
193 delete [] mMultiSampleSupport.begin()->second;
194 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
195 }
196
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000197 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000198 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000199 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000200 {
201 mState.samplerTexture[type][sampler].set(NULL);
202 }
203 }
204
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000205 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000206 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000207 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000208 }
209
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
211 {
212 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
213 }
214
215 mState.arrayBuffer.set(NULL);
216 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000217 mState.renderbuffer.set(NULL);
218
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000219 mTexture2DZero.set(NULL);
220 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000222 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000223 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000224 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000225 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000226
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000227 if (mMaskedClearSavedState)
228 {
229 mMaskedClearSavedState->Release();
230 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000231
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000232 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233}
234
235void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
236{
237 IDirect3DDevice9 *device = display->getDevice();
238
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000239 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000240 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000241 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000242
daniel@transgaming.com83921382011-01-08 05:46:00 +0000243 mVertexDataManager = new VertexDataManager(this, device);
244 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000245 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000246
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000247 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +0000248 mSupportsVertexTexture = display->getVertexTextureSupport();
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000249 mSupportsNonPower2Texture = display->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000250
251 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
252 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
253 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
254 mMaxRenderbufferDimension = mMaxTextureDimension;
255 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
256 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
257 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
258
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000259 const D3DFORMAT renderBufferFormats[] =
260 {
261 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000262 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000263 D3DFMT_R5G6B5,
264 D3DFMT_D24S8
265 };
266
267 int max = 0;
268 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
269 {
270 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
271 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
272 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
273
274 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
275 {
276 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
277 {
278 max = j;
279 }
280 }
281 }
282
283 mMaxSupportedSamples = max;
284
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000285 mSupportsEventQueries = display->getEventQuerySupport();
gman@chromium.org50c526d2011-08-10 05:19:44 +0000286 mSupportsDXT1Textures = display->getDXT1TextureSupport();
287 mSupportsDXT3Textures = display->getDXT3TextureSupport();
288 mSupportsDXT5Textures = display->getDXT5TextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000289 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
290 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000291 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
292 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000293
daniel@transgaming.com83921382011-01-08 05:46:00 +0000294 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
295
gman@chromium.org50c526d2011-08-10 05:19:44 +0000296 mNumCompressedTextureFormats = 0;
297 if (supportsDXT1Textures())
298 {
299 mNumCompressedTextureFormats += 2;
300 }
301 if (supportsDXT3Textures())
302 {
303 mNumCompressedTextureFormats += 1;
304 }
305 if (supportsDXT5Textures())
306 {
307 mNumCompressedTextureFormats += 1;
308 }
309
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000310 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000311 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000312
313 mState.viewportX = 0;
314 mState.viewportY = 0;
315 mState.viewportWidth = surface->getWidth();
316 mState.viewportHeight = surface->getHeight();
317
318 mState.scissorX = 0;
319 mState.scissorY = 0;
320 mState.scissorWidth = surface->getWidth();
321 mState.scissorHeight = surface->getHeight();
322
323 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000324 }
325
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
327 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000328 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000331 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000332 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
334 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000336 if (defaultRenderTarget)
337 {
338 defaultRenderTarget->Release();
339 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000341 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000343 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000345
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000346 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347}
348
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000349// 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 +0000350void Context::markAllStateDirty()
351{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000352 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
353 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000354 mAppliedTextureSerialPS[t] = 0;
355 }
356
357 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
358 {
359 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000360 }
361
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000362 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000363 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000364 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000365 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000366 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000367
368 mClearStateDirty = true;
369 mCullStateDirty = true;
370 mDepthStateDirty = true;
371 mMaskStateDirty = true;
372 mBlendStateDirty = true;
373 mStencilStateDirty = true;
374 mPolygonOffsetStateDirty = true;
375 mScissorStateDirty = true;
376 mSampleStateDirty = true;
377 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000378 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000379}
380
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381void Context::setClearColor(float red, float green, float blue, float alpha)
382{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000383 mState.colorClearValue.red = red;
384 mState.colorClearValue.green = green;
385 mState.colorClearValue.blue = blue;
386 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387}
388
389void Context::setClearDepth(float depth)
390{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000391 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392}
393
394void Context::setClearStencil(int stencil)
395{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000396 mState.stencilClearValue = stencil;
397}
398
399void Context::setCullFace(bool enabled)
400{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000401 if (mState.cullFace != enabled)
402 {
403 mState.cullFace = enabled;
404 mCullStateDirty = true;
405 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000406}
407
408bool Context::isCullFaceEnabled() const
409{
410 return mState.cullFace;
411}
412
413void Context::setCullMode(GLenum mode)
414{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000415 if (mState.cullMode != mode)
416 {
417 mState.cullMode = mode;
418 mCullStateDirty = true;
419 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000420}
421
422void Context::setFrontFace(GLenum front)
423{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000424 if (mState.frontFace != front)
425 {
426 mState.frontFace = front;
427 mFrontFaceDirty = true;
428 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000429}
430
431void Context::setDepthTest(bool enabled)
432{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000433 if (mState.depthTest != enabled)
434 {
435 mState.depthTest = enabled;
436 mDepthStateDirty = true;
437 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000438}
439
440bool Context::isDepthTestEnabled() const
441{
442 return mState.depthTest;
443}
444
445void Context::setDepthFunc(GLenum depthFunc)
446{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000447 if (mState.depthFunc != depthFunc)
448 {
449 mState.depthFunc = depthFunc;
450 mDepthStateDirty = true;
451 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000452}
453
454void Context::setDepthRange(float zNear, float zFar)
455{
456 mState.zNear = zNear;
457 mState.zFar = zFar;
458}
459
460void Context::setBlend(bool enabled)
461{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000462 if (mState.blend != enabled)
463 {
464 mState.blend = enabled;
465 mBlendStateDirty = true;
466 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000467}
468
469bool Context::isBlendEnabled() const
470{
471 return mState.blend;
472}
473
474void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
475{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000476 if (mState.sourceBlendRGB != sourceRGB ||
477 mState.sourceBlendAlpha != sourceAlpha ||
478 mState.destBlendRGB != destRGB ||
479 mState.destBlendAlpha != destAlpha)
480 {
481 mState.sourceBlendRGB = sourceRGB;
482 mState.destBlendRGB = destRGB;
483 mState.sourceBlendAlpha = sourceAlpha;
484 mState.destBlendAlpha = destAlpha;
485 mBlendStateDirty = true;
486 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000487}
488
489void Context::setBlendColor(float red, float green, float blue, float alpha)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.blendColor.red != red ||
492 mState.blendColor.green != green ||
493 mState.blendColor.blue != blue ||
494 mState.blendColor.alpha != alpha)
495 {
496 mState.blendColor.red = red;
497 mState.blendColor.green = green;
498 mState.blendColor.blue = blue;
499 mState.blendColor.alpha = alpha;
500 mBlendStateDirty = true;
501 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000502}
503
504void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
505{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000506 if (mState.blendEquationRGB != rgbEquation ||
507 mState.blendEquationAlpha != alphaEquation)
508 {
509 mState.blendEquationRGB = rgbEquation;
510 mState.blendEquationAlpha = alphaEquation;
511 mBlendStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515void Context::setStencilTest(bool enabled)
516{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 if (mState.stencilTest != enabled)
518 {
519 mState.stencilTest = enabled;
520 mStencilStateDirty = true;
521 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000522}
523
524bool Context::isStencilTestEnabled() const
525{
526 return mState.stencilTest;
527}
528
529void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
530{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000531 if (mState.stencilFunc != stencilFunc ||
532 mState.stencilRef != stencilRef ||
533 mState.stencilMask != stencilMask)
534 {
535 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000536 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000537 mState.stencilMask = stencilMask;
538 mStencilStateDirty = true;
539 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000540}
541
542void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
543{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000544 if (mState.stencilBackFunc != stencilBackFunc ||
545 mState.stencilBackRef != stencilBackRef ||
546 mState.stencilBackMask != stencilBackMask)
547 {
548 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000549 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000550 mState.stencilBackMask = stencilBackMask;
551 mStencilStateDirty = true;
552 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000553}
554
555void Context::setStencilWritemask(GLuint stencilWritemask)
556{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000557 if (mState.stencilWritemask != stencilWritemask)
558 {
559 mState.stencilWritemask = stencilWritemask;
560 mStencilStateDirty = true;
561 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000562}
563
564void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
565{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000566 if (mState.stencilBackWritemask != stencilBackWritemask)
567 {
568 mState.stencilBackWritemask = stencilBackWritemask;
569 mStencilStateDirty = true;
570 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000571}
572
573void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
574{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000575 if (mState.stencilFail != stencilFail ||
576 mState.stencilPassDepthFail != stencilPassDepthFail ||
577 mState.stencilPassDepthPass != stencilPassDepthPass)
578 {
579 mState.stencilFail = stencilFail;
580 mState.stencilPassDepthFail = stencilPassDepthFail;
581 mState.stencilPassDepthPass = stencilPassDepthPass;
582 mStencilStateDirty = true;
583 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000584}
585
586void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
587{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000588 if (mState.stencilBackFail != stencilBackFail ||
589 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
590 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
591 {
592 mState.stencilBackFail = stencilBackFail;
593 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
594 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
595 mStencilStateDirty = true;
596 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000597}
598
599void Context::setPolygonOffsetFill(bool enabled)
600{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000601 if (mState.polygonOffsetFill != enabled)
602 {
603 mState.polygonOffsetFill = enabled;
604 mPolygonOffsetStateDirty = true;
605 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000606}
607
608bool Context::isPolygonOffsetFillEnabled() const
609{
610 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000611
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000612}
613
614void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
615{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000616 if (mState.polygonOffsetFactor != factor ||
617 mState.polygonOffsetUnits != units)
618 {
619 mState.polygonOffsetFactor = factor;
620 mState.polygonOffsetUnits = units;
621 mPolygonOffsetStateDirty = true;
622 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000623}
624
625void Context::setSampleAlphaToCoverage(bool enabled)
626{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000627 if (mState.sampleAlphaToCoverage != enabled)
628 {
629 mState.sampleAlphaToCoverage = enabled;
630 mSampleStateDirty = true;
631 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000632}
633
634bool Context::isSampleAlphaToCoverageEnabled() const
635{
636 return mState.sampleAlphaToCoverage;
637}
638
639void Context::setSampleCoverage(bool enabled)
640{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000641 if (mState.sampleCoverage != enabled)
642 {
643 mState.sampleCoverage = enabled;
644 mSampleStateDirty = true;
645 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000646}
647
648bool Context::isSampleCoverageEnabled() const
649{
650 return mState.sampleCoverage;
651}
652
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000653void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000654{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000655 if (mState.sampleCoverageValue != value ||
656 mState.sampleCoverageInvert != invert)
657 {
658 mState.sampleCoverageValue = value;
659 mState.sampleCoverageInvert = invert;
660 mSampleStateDirty = true;
661 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000662}
663
664void Context::setScissorTest(bool enabled)
665{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000666 if (mState.scissorTest != enabled)
667 {
668 mState.scissorTest = enabled;
669 mScissorStateDirty = true;
670 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000671}
672
673bool Context::isScissorTestEnabled() const
674{
675 return mState.scissorTest;
676}
677
678void Context::setDither(bool enabled)
679{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000680 if (mState.dither != enabled)
681 {
682 mState.dither = enabled;
683 mDitherStateDirty = true;
684 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000685}
686
687bool Context::isDitherEnabled() const
688{
689 return mState.dither;
690}
691
692void Context::setLineWidth(GLfloat width)
693{
694 mState.lineWidth = width;
695}
696
697void Context::setGenerateMipmapHint(GLenum hint)
698{
699 mState.generateMipmapHint = hint;
700}
701
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000702void Context::setFragmentShaderDerivativeHint(GLenum hint)
703{
704 mState.fragmentShaderDerivativeHint = hint;
705 // TODO: Propagate the hint to shader translator so we can write
706 // ddx, ddx_coarse, or ddx_fine depending on the hint.
707 // Ignore for now. It is valid for implementations to ignore hint.
708}
709
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000710void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
711{
712 mState.viewportX = x;
713 mState.viewportY = y;
714 mState.viewportWidth = width;
715 mState.viewportHeight = height;
716}
717
718void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
719{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000720 if (mState.scissorX != x || mState.scissorY != y ||
721 mState.scissorWidth != width || mState.scissorHeight != height)
722 {
723 mState.scissorX = x;
724 mState.scissorY = y;
725 mState.scissorWidth = width;
726 mState.scissorHeight = height;
727 mScissorStateDirty = true;
728 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000729}
730
731void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
732{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000733 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
734 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
735 {
736 mState.colorMaskRed = red;
737 mState.colorMaskGreen = green;
738 mState.colorMaskBlue = blue;
739 mState.colorMaskAlpha = alpha;
740 mMaskStateDirty = true;
741 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000742}
743
744void Context::setDepthMask(bool mask)
745{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000746 if (mState.depthMask != mask)
747 {
748 mState.depthMask = mask;
749 mMaskStateDirty = true;
750 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000751}
752
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000753void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000754{
755 mState.activeSampler = active;
756}
757
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000758GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000759{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000760 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000761}
762
763GLuint Context::getDrawFramebufferHandle() const
764{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000765 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000766}
767
768GLuint Context::getRenderbufferHandle() const
769{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000770 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000771}
772
773GLuint Context::getArrayBufferHandle() const
774{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000775 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000776}
777
daniel@transgaming.com83921382011-01-08 05:46:00 +0000778void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000779{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000780 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000781}
782
daniel@transgaming.com83921382011-01-08 05:46:00 +0000783const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000784{
785 return mState.vertexAttribute[attribNum];
786}
787
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000788void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000789 GLsizei stride, const void *pointer)
790{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000791 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000792 mState.vertexAttribute[attribNum].mSize = size;
793 mState.vertexAttribute[attribNum].mType = type;
794 mState.vertexAttribute[attribNum].mNormalized = normalized;
795 mState.vertexAttribute[attribNum].mStride = stride;
796 mState.vertexAttribute[attribNum].mPointer = pointer;
797}
798
799const void *Context::getVertexAttribPointer(unsigned int attribNum) const
800{
801 return mState.vertexAttribute[attribNum].mPointer;
802}
803
daniel@transgaming.com83921382011-01-08 05:46:00 +0000804const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000805{
806 return mState.vertexAttribute;
807}
808
809void Context::setPackAlignment(GLint alignment)
810{
811 mState.packAlignment = alignment;
812}
813
814GLint Context::getPackAlignment() const
815{
816 return mState.packAlignment;
817}
818
819void Context::setUnpackAlignment(GLint alignment)
820{
821 mState.unpackAlignment = alignment;
822}
823
824GLint Context::getUnpackAlignment() const
825{
826 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827}
828
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829GLuint Context::createBuffer()
830{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000831 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834GLuint Context::createProgram()
835{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000836 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837}
838
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000839GLuint Context::createShader(GLenum type)
840{
841 return mResourceManager->createShader(type);
842}
843
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844GLuint Context::createTexture()
845{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000846 return mResourceManager->createTexture();
847}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000849GLuint Context::createRenderbuffer()
850{
851 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852}
853
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000854// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855GLuint Context::createFramebuffer()
856{
benvanik@google.com1a233342011-04-28 19:44:39 +0000857 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000858
859 mFramebufferMap[handle] = NULL;
860
861 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000864GLuint Context::createFence()
865{
benvanik@google.com1a233342011-04-28 19:44:39 +0000866 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000867
868 mFenceMap[handle] = new Fence;
869
870 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000871}
872
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873void Context::deleteBuffer(GLuint buffer)
874{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000875 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876 {
877 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000879
880 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881}
882
883void Context::deleteShader(GLuint shader)
884{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000885 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886}
887
888void Context::deleteProgram(GLuint program)
889{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000890 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
893void Context::deleteTexture(GLuint texture)
894{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000895 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896 {
897 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000899
900 mResourceManager->deleteTexture(texture);
901}
902
903void Context::deleteRenderbuffer(GLuint renderbuffer)
904{
905 if (mResourceManager->getRenderbuffer(renderbuffer))
906 {
907 detachRenderbuffer(renderbuffer);
908 }
909
910 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911}
912
913void Context::deleteFramebuffer(GLuint framebuffer)
914{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000915 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
916
917 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918 {
919 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000920
benvanik@google.com1a233342011-04-28 19:44:39 +0000921 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000922 delete framebufferObject->second;
923 mFramebufferMap.erase(framebufferObject);
924 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000926
927void Context::deleteFence(GLuint fence)
928{
929 FenceMap::iterator fenceObject = mFenceMap.find(fence);
930
931 if (fenceObject != mFenceMap.end())
932 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000933 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000934 delete fenceObject->second;
935 mFenceMap.erase(fenceObject);
936 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000937}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000939Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000941 return mResourceManager->getBuffer(handle);
942}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000944Shader *Context::getShader(GLuint handle)
945{
946 return mResourceManager->getShader(handle);
947}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000949Program *Context::getProgram(GLuint handle)
950{
951 return mResourceManager->getProgram(handle);
952}
953
954Texture *Context::getTexture(GLuint handle)
955{
956 return mResourceManager->getTexture(handle);
957}
958
959Renderbuffer *Context::getRenderbuffer(GLuint handle)
960{
961 return mResourceManager->getRenderbuffer(handle);
962}
963
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000964Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000965{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000966 return getFramebuffer(mState.readFramebuffer);
967}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000968
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000969Framebuffer *Context::getDrawFramebuffer()
970{
971 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
974void Context::bindArrayBuffer(unsigned int buffer)
975{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000976 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000978 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979}
980
981void Context::bindElementArrayBuffer(unsigned int buffer)
982{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000983 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000985 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
988void Context::bindTexture2D(GLuint texture)
989{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000990 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000992 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
995void Context::bindTextureCubeMap(GLuint texture)
996{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000997 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000999 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000}
1001
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001002void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001004 if (!getFramebuffer(framebuffer))
1005 {
1006 mFramebufferMap[framebuffer] = new Framebuffer();
1007 }
1008
1009 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001010}
1011
1012void Context::bindDrawFramebuffer(GLuint framebuffer)
1013{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001014 if (!getFramebuffer(framebuffer))
1015 {
1016 mFramebufferMap[framebuffer] = new Framebuffer();
1017 }
1018
1019 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001020}
1021
1022void Context::bindRenderbuffer(GLuint renderbuffer)
1023{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001024 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1025
1026 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027}
1028
1029void Context::useProgram(GLuint program)
1030{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001031 GLuint priorProgram = mState.currentProgram;
1032 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001033
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001034 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001036 Program *newProgram = mResourceManager->getProgram(program);
1037 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1038
1039 if (newProgram)
1040 {
1041 newProgram->addRef();
1042 }
1043
1044 if (oldProgram)
1045 {
1046 oldProgram->release();
1047 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049}
1050
1051void Context::setFramebufferZero(Framebuffer *buffer)
1052{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001053 delete mFramebufferMap[0];
1054 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001055}
1056
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001057void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001059 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1060 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061}
1062
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001063Framebuffer *Context::getFramebuffer(unsigned int handle)
1064{
1065 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1066
1067 if (framebuffer == mFramebufferMap.end())
1068 {
1069 return NULL;
1070 }
1071 else
1072 {
1073 return framebuffer->second;
1074 }
1075}
1076
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001077Fence *Context::getFence(unsigned int handle)
1078{
1079 FenceMap::iterator fence = mFenceMap.find(handle);
1080
1081 if (fence == mFenceMap.end())
1082 {
1083 return NULL;
1084 }
1085 else
1086 {
1087 return fence->second;
1088 }
1089}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001090
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091Buffer *Context::getArrayBuffer()
1092{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001093 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
1096Buffer *Context::getElementArrayBuffer()
1097{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001098 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
1101Program *Context::getCurrentProgram()
1102{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001103 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104}
1105
1106Texture2D *Context::getTexture2D()
1107{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001108 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109}
1110
1111TextureCubeMap *Context::getTextureCubeMap()
1112{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001113 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114}
1115
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001116Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001117{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001118 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001119
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001120 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001121 {
1122 switch (type)
1123 {
1124 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001125 case TEXTURE_2D: return mTexture2DZero.get();
1126 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001127 }
1128 }
1129
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001130 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131}
1132
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001133bool Context::getBooleanv(GLenum pname, GLboolean *params)
1134{
1135 switch (pname)
1136 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001137 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1138 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1139 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001140 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001141 params[0] = mState.colorMaskRed;
1142 params[1] = mState.colorMaskGreen;
1143 params[2] = mState.colorMaskBlue;
1144 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001145 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001146 case GL_CULL_FACE: *params = mState.cullFace; break;
1147 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1148 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1149 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1150 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1151 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1152 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1153 case GL_BLEND: *params = mState.blend; break;
1154 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001155 default:
1156 return false;
1157 }
1158
1159 return true;
1160}
1161
1162bool Context::getFloatv(GLenum pname, GLfloat *params)
1163{
1164 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1165 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1166 // GetIntegerv as its native query function. As it would require conversion in any
1167 // case, this should make no difference to the calling application.
1168 switch (pname)
1169 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001170 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1171 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1172 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1173 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1174 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001175 case GL_ALIASED_LINE_WIDTH_RANGE:
1176 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1177 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1178 break;
1179 case GL_ALIASED_POINT_SIZE_RANGE:
1180 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001181 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 +00001182 break;
1183 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001184 params[0] = mState.zNear;
1185 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001186 break;
1187 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001188 params[0] = mState.colorClearValue.red;
1189 params[1] = mState.colorClearValue.green;
1190 params[2] = mState.colorClearValue.blue;
1191 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001192 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001193 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001194 params[0] = mState.blendColor.red;
1195 params[1] = mState.blendColor.green;
1196 params[2] = mState.blendColor.blue;
1197 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001198 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001199 default:
1200 return false;
1201 }
1202
1203 return true;
1204}
1205
1206bool Context::getIntegerv(GLenum pname, GLint *params)
1207{
1208 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1209 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1210 // GetIntegerv as its native query function. As it would require conversion in any
1211 // case, this should make no difference to the calling application. You may find it in
1212 // Context::getFloatv.
1213 switch (pname)
1214 {
1215 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1216 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001217 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001218 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1219 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001220 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001221 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001222 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001223 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001224 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001225 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1226 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001227 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1228 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1229 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001230 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001231 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1232 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1233 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1234 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001235 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001236 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1237 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1238 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1239 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1240 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1241 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1242 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1243 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1244 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1245 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1246 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1247 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1248 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1249 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1250 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1251 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1252 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1253 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1254 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1255 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1256 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1257 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1258 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1259 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001260 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1261 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001262 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001263 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001264 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001265 case GL_MAX_SAMPLES_ANGLE:
1266 {
1267 GLsizei maxSamples = getMaxSupportedSamples();
1268 if (maxSamples != 0)
1269 {
1270 *params = maxSamples;
1271 }
1272 else
1273 {
1274 return false;
1275 }
1276
1277 break;
1278 }
1279 case GL_SAMPLE_BUFFERS:
1280 case GL_SAMPLES:
1281 {
1282 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1283 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1284 {
1285 switch (pname)
1286 {
1287 case GL_SAMPLE_BUFFERS:
1288 if (framebuffer->getSamples() != 0)
1289 {
1290 *params = 1;
1291 }
1292 else
1293 {
1294 *params = 0;
1295 }
1296 break;
1297 case GL_SAMPLES:
1298 *params = framebuffer->getSamples();
1299 break;
1300 }
1301 }
1302 else
1303 {
1304 *params = 0;
1305 }
1306 }
1307 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001308 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1309 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1310 case GL_MAX_VIEWPORT_DIMS:
1311 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001312 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001313 params[0] = maxDimension;
1314 params[1] = maxDimension;
1315 }
1316 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001317 case GL_COMPRESSED_TEXTURE_FORMATS:
1318 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001319 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001320 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001321 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1322 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1323 }
1324 if (supportsDXT3Textures())
1325 {
1326 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1327 }
1328 if (supportsDXT5Textures())
1329 {
1330 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001331 }
1332 }
1333 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001334 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001335 params[0] = mState.viewportX;
1336 params[1] = mState.viewportY;
1337 params[2] = mState.viewportWidth;
1338 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001339 break;
1340 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001341 params[0] = mState.scissorX;
1342 params[1] = mState.scissorY;
1343 params[2] = mState.scissorWidth;
1344 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001345 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001346 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1347 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001348 case GL_RED_BITS:
1349 case GL_GREEN_BITS:
1350 case GL_BLUE_BITS:
1351 case GL_ALPHA_BITS:
1352 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001353 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001354 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1355
1356 if (colorbuffer)
1357 {
1358 switch (pname)
1359 {
1360 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1361 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1362 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1363 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1364 }
1365 }
1366 else
1367 {
1368 *params = 0;
1369 }
1370 }
1371 break;
1372 case GL_DEPTH_BITS:
1373 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001374 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001375 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001376
1377 if (depthbuffer)
1378 {
1379 *params = depthbuffer->getDepthSize();
1380 }
1381 else
1382 {
1383 *params = 0;
1384 }
1385 }
1386 break;
1387 case GL_STENCIL_BITS:
1388 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001389 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001390 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391
1392 if (stencilbuffer)
1393 {
1394 *params = stencilbuffer->getStencilSize();
1395 }
1396 else
1397 {
1398 *params = 0;
1399 }
1400 }
1401 break;
1402 case GL_TEXTURE_BINDING_2D:
1403 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001404 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001405 {
1406 error(GL_INVALID_OPERATION);
1407 return false;
1408 }
1409
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001410 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001411 }
1412 break;
1413 case GL_TEXTURE_BINDING_CUBE_MAP:
1414 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001415 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001416 {
1417 error(GL_INVALID_OPERATION);
1418 return false;
1419 }
1420
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001421 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001422 }
1423 break;
1424 default:
1425 return false;
1426 }
1427
1428 return true;
1429}
1430
1431bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1432{
1433 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1434 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1435 // to the fact that it is stored internally as a float, and so would require conversion
1436 // if returned from Context::getIntegerv. Since this conversion is already implemented
1437 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1438 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1439 // application.
1440 switch (pname)
1441 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001442 case GL_COMPRESSED_TEXTURE_FORMATS:
1443 {
1444 *type = GL_INT;
1445 *numParams = mNumCompressedTextureFormats;
1446 }
1447 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001448 case GL_SHADER_BINARY_FORMATS:
1449 {
1450 *type = GL_INT;
1451 *numParams = 0;
1452 }
1453 break;
1454 case GL_MAX_VERTEX_ATTRIBS:
1455 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1456 case GL_MAX_VARYING_VECTORS:
1457 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1458 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1459 case GL_MAX_TEXTURE_IMAGE_UNITS:
1460 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1461 case GL_MAX_RENDERBUFFER_SIZE:
1462 case GL_NUM_SHADER_BINARY_FORMATS:
1463 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1464 case GL_ARRAY_BUFFER_BINDING:
1465 case GL_FRAMEBUFFER_BINDING:
1466 case GL_RENDERBUFFER_BINDING:
1467 case GL_CURRENT_PROGRAM:
1468 case GL_PACK_ALIGNMENT:
1469 case GL_UNPACK_ALIGNMENT:
1470 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001471 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001472 case GL_RED_BITS:
1473 case GL_GREEN_BITS:
1474 case GL_BLUE_BITS:
1475 case GL_ALPHA_BITS:
1476 case GL_DEPTH_BITS:
1477 case GL_STENCIL_BITS:
1478 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1479 case GL_CULL_FACE_MODE:
1480 case GL_FRONT_FACE:
1481 case GL_ACTIVE_TEXTURE:
1482 case GL_STENCIL_FUNC:
1483 case GL_STENCIL_VALUE_MASK:
1484 case GL_STENCIL_REF:
1485 case GL_STENCIL_FAIL:
1486 case GL_STENCIL_PASS_DEPTH_FAIL:
1487 case GL_STENCIL_PASS_DEPTH_PASS:
1488 case GL_STENCIL_BACK_FUNC:
1489 case GL_STENCIL_BACK_VALUE_MASK:
1490 case GL_STENCIL_BACK_REF:
1491 case GL_STENCIL_BACK_FAIL:
1492 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1493 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1494 case GL_DEPTH_FUNC:
1495 case GL_BLEND_SRC_RGB:
1496 case GL_BLEND_SRC_ALPHA:
1497 case GL_BLEND_DST_RGB:
1498 case GL_BLEND_DST_ALPHA:
1499 case GL_BLEND_EQUATION_RGB:
1500 case GL_BLEND_EQUATION_ALPHA:
1501 case GL_STENCIL_WRITEMASK:
1502 case GL_STENCIL_BACK_WRITEMASK:
1503 case GL_STENCIL_CLEAR_VALUE:
1504 case GL_SUBPIXEL_BITS:
1505 case GL_MAX_TEXTURE_SIZE:
1506 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1507 case GL_SAMPLE_BUFFERS:
1508 case GL_SAMPLES:
1509 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1510 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1511 case GL_TEXTURE_BINDING_2D:
1512 case GL_TEXTURE_BINDING_CUBE_MAP:
1513 {
1514 *type = GL_INT;
1515 *numParams = 1;
1516 }
1517 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001518 case GL_MAX_SAMPLES_ANGLE:
1519 {
1520 if (getMaxSupportedSamples() != 0)
1521 {
1522 *type = GL_INT;
1523 *numParams = 1;
1524 }
1525 else
1526 {
1527 return false;
1528 }
1529 }
1530 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001531 case GL_MAX_VIEWPORT_DIMS:
1532 {
1533 *type = GL_INT;
1534 *numParams = 2;
1535 }
1536 break;
1537 case GL_VIEWPORT:
1538 case GL_SCISSOR_BOX:
1539 {
1540 *type = GL_INT;
1541 *numParams = 4;
1542 }
1543 break;
1544 case GL_SHADER_COMPILER:
1545 case GL_SAMPLE_COVERAGE_INVERT:
1546 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001547 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1548 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1549 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1550 case GL_SAMPLE_COVERAGE:
1551 case GL_SCISSOR_TEST:
1552 case GL_STENCIL_TEST:
1553 case GL_DEPTH_TEST:
1554 case GL_BLEND:
1555 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001556 {
1557 *type = GL_BOOL;
1558 *numParams = 1;
1559 }
1560 break;
1561 case GL_COLOR_WRITEMASK:
1562 {
1563 *type = GL_BOOL;
1564 *numParams = 4;
1565 }
1566 break;
1567 case GL_POLYGON_OFFSET_FACTOR:
1568 case GL_POLYGON_OFFSET_UNITS:
1569 case GL_SAMPLE_COVERAGE_VALUE:
1570 case GL_DEPTH_CLEAR_VALUE:
1571 case GL_LINE_WIDTH:
1572 {
1573 *type = GL_FLOAT;
1574 *numParams = 1;
1575 }
1576 break;
1577 case GL_ALIASED_LINE_WIDTH_RANGE:
1578 case GL_ALIASED_POINT_SIZE_RANGE:
1579 case GL_DEPTH_RANGE:
1580 {
1581 *type = GL_FLOAT;
1582 *numParams = 2;
1583 }
1584 break;
1585 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001586 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001587 {
1588 *type = GL_FLOAT;
1589 *numParams = 4;
1590 }
1591 break;
1592 default:
1593 return false;
1594 }
1595
1596 return true;
1597}
1598
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001599// Applies the render target surface, depth stencil surface, viewport rectangle and
1600// scissor rectangle to the Direct3D 9 device
1601bool Context::applyRenderTarget(bool ignoreViewport)
1602{
1603 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001604
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001605 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001606
1607 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1608 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001609 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001610 }
1611
1612 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001613
1614 if (!renderTarget)
1615 {
1616 return false; // Context must be lost
1617 }
1618
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001619 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001621 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1622 if (renderTargetSerial != mAppliedRenderTargetSerial)
1623 {
1624 device->SetRenderTarget(0, renderTarget);
1625 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001626 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001627 }
1628
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001629 unsigned int depthbufferSerial = 0;
1630 unsigned int stencilbufferSerial = 0;
1631 if (framebufferObject->getDepthbufferType() != GL_NONE)
1632 {
1633 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001634 if (!depthStencil)
1635 {
1636 ERR("Depth stencil pointer unexpectedly null.");
1637 return false;
1638 }
1639
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001640 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1641 }
1642 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1643 {
1644 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001645 if (!depthStencil)
1646 {
1647 ERR("Depth stencil pointer unexpectedly null.");
1648 return false;
1649 }
1650
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001651 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1652 }
1653
1654 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001655 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001656 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001657 {
1658 device->SetDepthStencilSurface(depthStencil);
1659 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001660 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001661 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001662 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001663
1664 D3DVIEWPORT9 viewport;
1665 D3DSURFACE_DESC desc;
1666 renderTarget->GetDesc(&desc);
1667
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001668 float zNear = clamp01(mState.zNear);
1669 float zFar = clamp01(mState.zFar);
1670
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671 if (ignoreViewport)
1672 {
1673 viewport.X = 0;
1674 viewport.Y = 0;
1675 viewport.Width = desc.Width;
1676 viewport.Height = desc.Height;
1677 viewport.MinZ = 0.0f;
1678 viewport.MaxZ = 1.0f;
1679 }
1680 else
1681 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001682 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1683 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1684 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1685 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1686 viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(desc.Height) - static_cast<LONG>(viewport.Y));
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001687 viewport.MinZ = zNear;
1688 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689 }
1690
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001691 if (viewport.Width <= 0 || viewport.Height <= 0)
1692 {
1693 return false; // Nothing to render
1694 }
1695
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696 device->SetViewport(&viewport);
1697
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001698 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001699 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001700 if (mState.scissorTest)
1701 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001702 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1703 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1704 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1705 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1706 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001707 device->SetScissorRect(&rect);
1708 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1709 }
1710 else
1711 {
1712 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1713 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001714
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001715 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716 }
1717
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001718 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001720 Program *programObject = getCurrentProgram();
1721
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001722 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001723 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001724 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001725
daniel@transgaming.com31754962010-11-28 02:02:52 +00001726 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001727 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1728 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1729 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001730 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001731
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001732 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001733 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001734 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001735
daniel@transgaming.com31754962010-11-28 02:02:52 +00001736 GLint depthRange = programObject->getDxDepthRangeLocation();
1737 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1738 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 }
1740
1741 return true;
1742}
1743
1744// 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 +00001745void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746{
1747 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001748 Program *programObject = getCurrentProgram();
1749
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001750 Framebuffer *framebufferObject = getDrawFramebuffer();
1751
1752 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1753
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001754 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001755 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001756 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001758 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001759 GLint alwaysFront = !isTriangleMode(drawMode);
1760 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1761
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001762 egl::Display *display = getDisplay();
1763 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
1764 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1765 // Apparently some ATI cards have a bug where a draw with a zero color
1766 // write mask can cause later draws to have incorrect results. Instead,
1767 // set a nonzero color write mask but modify the blend state so that no
1768 // drawing is done.
1769 // http://code.google.com/p/angleproject/issues/detail?id=169
1770
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001771 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001772 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001773 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001775 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776 }
1777 else
1778 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001779 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001780 }
1781
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001782 mCullStateDirty = false;
1783 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001785 if (mDepthStateDirty)
1786 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001787 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001788 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001789 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1790 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001791 }
1792 else
1793 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001794 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001796
1797 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798 }
1799
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001800 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1801 {
1802 mBlendStateDirty = true;
1803 mMaskStateDirty = true;
1804 }
1805
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001806 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001807 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001808 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001809 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001810 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1811
1812 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1813 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1814 {
1815 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1816 }
1817 else
1818 {
1819 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1820 unorm<8>(mState.blendColor.alpha),
1821 unorm<8>(mState.blendColor.alpha),
1822 unorm<8>(mState.blendColor.alpha)));
1823 }
1824
1825 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1826 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1827 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1828
1829 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1830 mState.destBlendRGB != mState.destBlendAlpha ||
1831 mState.blendEquationRGB != mState.blendEquationAlpha)
1832 {
1833 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1834
1835 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1836 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1837 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001838 }
1839 else
1840 {
1841 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1842 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001843 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001844 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001845 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001846 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001847 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848
1849 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850 }
1851
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001852 if (mStencilStateDirty || mFrontFaceDirty)
1853 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001854 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001855 {
1856 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1857 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1858
1859 // FIXME: Unsupported by D3D9
1860 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1861 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1862 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1863 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1864 mState.stencilRef != mState.stencilBackRef ||
1865 mState.stencilMask != mState.stencilBackMask)
1866 {
1867 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1868 return error(GL_INVALID_OPERATION);
1869 }
1870
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001871 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001872 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001873 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1874
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001875 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1876 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001877 es2dx::ConvertComparison(mState.stencilFunc));
1878
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001879 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1880 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001881
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001882 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001883 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001884 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001885 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001886 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001887 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1888
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001889 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1890 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001891 es2dx::ConvertComparison(mState.stencilBackFunc));
1892
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001893 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1894 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001895
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001896 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001897 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001898 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001899 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001900 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001901 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1902 }
1903 else
1904 {
1905 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1906 }
1907
1908 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001909 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001910 }
1911
1912 if (mMaskStateDirty)
1913 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001914 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1915 mState.colorMaskBlue, mState.colorMaskAlpha);
1916 if (colorMask == 0 && !zeroColorMaskAllowed)
1917 {
1918 // Enable green channel, but set blending so nothing will be drawn.
1919 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1920 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1921
1922 device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1923 device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1924 device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
1925 }
1926 else
1927 {
1928 device->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
1929 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001930 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1931
1932 mMaskStateDirty = false;
1933 }
1934
1935 if (mPolygonOffsetStateDirty)
1936 {
1937 if (mState.polygonOffsetFill)
1938 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001939 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001940 if (depthbuffer)
1941 {
1942 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1943 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1944 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1945 }
1946 }
1947 else
1948 {
1949 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1950 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1951 }
1952
1953 mPolygonOffsetStateDirty = false;
1954 }
1955
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001956 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001958 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001959 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001960 FIXME("Sample alpha to coverage is unimplemented.");
1961 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001962
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001963 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1964 if (mState.sampleCoverage)
1965 {
1966 unsigned int mask = 0;
1967 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001968 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001969 float threshold = 0.5f;
1970
1971 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001972 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001973 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001974
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001975 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001976 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001977 threshold += 1.0f;
1978 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001979 }
1980 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001981 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001982
1983 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001984 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001985 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001986 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001987
1988 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001989 }
1990 else
1991 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001992 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001993 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001994
1995 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001996 }
1997
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001998 if (mDitherStateDirty)
1999 {
2000 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
2001
2002 mDitherStateDirty = false;
2003 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002004}
2005
daniel@transgaming.com83921382011-01-08 05:46:00 +00002006GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002007{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002008 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002009
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002010 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002011 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002012 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002013 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002014 }
2015
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00002016 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002017}
2018
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002019// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002020GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002021{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002022 IDirect3DDevice9 *device = getDevice();
2023 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002024
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002025 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002026 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00002027 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002028 }
2029
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002030 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031}
2032
2033// Applies the shaders and shader constants to the Direct3D 9 device
2034void Context::applyShaders()
2035{
2036 IDirect3DDevice9 *device = getDevice();
2037 Program *programObject = getCurrentProgram();
2038 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2039 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2040
2041 device->SetVertexShader(vertexShader);
2042 device->SetPixelShader(pixelShader);
2043
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002044 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002045 {
2046 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002047 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002048 }
2049
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 programObject->applyUniforms();
2051}
2052
2053// Applies the textures and sampler states to the Direct3D 9 device
2054void Context::applyTextures()
2055{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002056 applyTextures(SAMPLER_PIXEL);
2057
2058 if (mSupportsVertexTexture)
2059 {
2060 applyTextures(SAMPLER_VERTEX);
2061 }
2062}
2063
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002064// For each Direct3D 9 sampler of either the pixel or vertex stage,
2065// looks up the corresponding OpenGL texture image unit and texture type,
2066// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002067void Context::applyTextures(SamplerType type)
2068{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 IDirect3DDevice9 *device = getDevice();
2070 Program *programObject = getCurrentProgram();
2071
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002072 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002073
2074 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002076 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002077 int d3dSampler = (type == SAMPLER_PIXEL) ? samplerIndex : D3DVERTEXTEXTURESAMPLER0 + samplerIndex;
2078 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2079
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002080 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002082 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002083
2084 Texture *texture = getSamplerTexture(textureUnit, textureType);
2085
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002086 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002087 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002088 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2089
2090 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002091 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002092 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002093 {
2094 GLenum wrapS = texture->getWrapS();
2095 GLenum wrapT = texture->getWrapT();
2096 GLenum minFilter = texture->getMinFilter();
2097 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002099 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2100 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002102 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002103 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2104 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002105 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2106 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002107 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002109 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002110 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002111 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002112 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002113 }
2114 else
2115 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002116 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002117 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002118
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002119 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002120 texture->resetDirty();
2121 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002123 else
2124 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002125 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002126 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002127 device->SetTexture(d3dSampler, NULL);
2128 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002129 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131 }
2132}
2133
2134void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2135{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002136 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002137
2138 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2139 {
2140 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2141 }
2142
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002143 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2144 {
2145 return error(GL_INVALID_OPERATION);
2146 }
2147
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002149
2150 if (!renderTarget)
2151 {
2152 return; // Context must be lost, return silently
2153 }
2154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 IDirect3DDevice9 *device = getDevice();
2156
2157 D3DSURFACE_DESC desc;
2158 renderTarget->GetDesc(&desc);
2159
2160 IDirect3DSurface9 *systemSurface;
2161 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2162
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002163 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002164 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002165 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 return error(GL_OUT_OF_MEMORY);
2167 }
2168
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2170 {
2171 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002172 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173 }
2174
2175 result = device->GetRenderTargetData(renderTarget, systemSurface);
2176
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177 if (FAILED(result))
2178 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 systemSurface->Release();
2180
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002181 switch (result)
2182 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002183 // It turns out that D3D will sometimes produce more error
2184 // codes than those documented.
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002185 case D3DERR_DRIVERINTERNALERROR:
2186 case D3DERR_DEVICELOST:
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002187 case D3DERR_DEVICEHUNG:
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002188 return error(GL_OUT_OF_MEMORY);
2189 default:
2190 UNREACHABLE();
2191 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002192 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002193 }
2194
2195 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002196 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2197 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2198 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2199 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2200 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201
2202 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2203
2204 if (FAILED(result))
2205 {
2206 UNREACHABLE();
2207 systemSurface->Release();
2208
2209 return; // No sensible error to generate
2210 }
2211
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002212 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002214 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002215 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002216 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002217
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002218 for (int j = 0; j < rect.bottom - rect.top; j++)
2219 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002220 if (desc.Format == D3DFMT_A8R8G8B8 &&
2221 format == GL_BGRA_EXT &&
2222 type == GL_UNSIGNED_BYTE)
2223 {
2224 // Fast path for EXT_read_format_bgra, given
2225 // an RGBA source buffer. Note that buffers with no
2226 // alpha go through the slow path below.
2227 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002228 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002229 (rect.right - rect.left) * 4);
2230 continue;
2231 }
2232
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233 for (int i = 0; i < rect.right - rect.left; i++)
2234 {
2235 float r;
2236 float g;
2237 float b;
2238 float a;
2239
2240 switch (desc.Format)
2241 {
2242 case D3DFMT_R5G6B5:
2243 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002244 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245
2246 a = 1.0f;
2247 b = (rgb & 0x001F) * (1.0f / 0x001F);
2248 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2249 r = (rgb & 0xF800) * (1.0f / 0xF800);
2250 }
2251 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 case D3DFMT_A1R5G5B5:
2253 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002254 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255
2256 a = (argb & 0x8000) ? 1.0f : 0.0f;
2257 b = (argb & 0x001F) * (1.0f / 0x001F);
2258 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2259 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2260 }
2261 break;
2262 case D3DFMT_A8R8G8B8:
2263 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002264 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265
2266 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2267 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2268 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2269 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2270 }
2271 break;
2272 case D3DFMT_X8R8G8B8:
2273 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002274 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275
2276 a = 1.0f;
2277 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2278 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2279 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2280 }
2281 break;
2282 case D3DFMT_A2R10G10B10:
2283 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002284 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285
2286 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2287 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2288 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2289 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2290 }
2291 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002292 case D3DFMT_A32B32G32R32F:
2293 {
2294 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002295 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2296 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2297 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2298 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002299 }
2300 break;
2301 case D3DFMT_A16B16G16R16F:
2302 {
2303 // float formats in D3D are stored rgba, rather than the other way round
2304 float abgr[4];
2305
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002306 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002307
2308 a = abgr[3];
2309 b = abgr[2];
2310 g = abgr[1];
2311 r = abgr[0];
2312 }
2313 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002314 default:
2315 UNIMPLEMENTED(); // FIXME
2316 UNREACHABLE();
2317 }
2318
2319 switch (format)
2320 {
2321 case GL_RGBA:
2322 switch (type)
2323 {
2324 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002325 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2326 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2327 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2328 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329 break;
2330 default: UNREACHABLE();
2331 }
2332 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002333 case GL_BGRA_EXT:
2334 switch (type)
2335 {
2336 case GL_UNSIGNED_BYTE:
2337 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2338 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2339 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2340 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2341 break;
2342 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2343 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2344 // this type is packed as follows:
2345 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2346 // --------------------------------------------------------------------------------
2347 // | 4th | 3rd | 2nd | 1st component |
2348 // --------------------------------------------------------------------------------
2349 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2350 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2351 ((unsigned short)(15 * a + 0.5f) << 12)|
2352 ((unsigned short)(15 * r + 0.5f) << 8) |
2353 ((unsigned short)(15 * g + 0.5f) << 4) |
2354 ((unsigned short)(15 * b + 0.5f) << 0);
2355 break;
2356 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2357 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2358 // this type is packed as follows:
2359 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2360 // --------------------------------------------------------------------------------
2361 // | 4th | 3rd | 2nd | 1st component |
2362 // --------------------------------------------------------------------------------
2363 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2364 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2365 ((unsigned short)( a + 0.5f) << 15) |
2366 ((unsigned short)(31 * r + 0.5f) << 10) |
2367 ((unsigned short)(31 * g + 0.5f) << 5) |
2368 ((unsigned short)(31 * b + 0.5f) << 0);
2369 break;
2370 default: UNREACHABLE();
2371 }
2372 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002373 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 switch (type)
2375 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002376 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002377 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2378 ((unsigned short)(31 * b + 0.5f) << 0) |
2379 ((unsigned short)(63 * g + 0.5f) << 5) |
2380 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 break;
2382 default: UNREACHABLE();
2383 }
2384 break;
2385 default: UNREACHABLE();
2386 }
2387 }
2388 }
2389
2390 systemSurface->UnlockRect();
2391
2392 systemSurface->Release();
2393}
2394
2395void Context::clear(GLbitfield mask)
2396{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002397 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002398
2399 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2400 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002401 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002402 }
2403
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002404 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 IDirect3DDevice9 *device = getDevice();
2406 DWORD flags = 0;
2407
2408 if (mask & GL_COLOR_BUFFER_BIT)
2409 {
2410 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002411
2412 if (framebufferObject->getColorbufferType() != GL_NONE)
2413 {
2414 flags |= D3DCLEAR_TARGET;
2415 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416 }
2417
2418 if (mask & GL_DEPTH_BUFFER_BIT)
2419 {
2420 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002421 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 {
2423 flags |= D3DCLEAR_ZBUFFER;
2424 }
2425 }
2426
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 GLuint stencilUnmasked = 0x0;
2428
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002429 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002432 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002434 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002435 if (!depthStencil)
2436 {
2437 ERR("Depth stencil pointer unexpectedly null.");
2438 return;
2439 }
2440
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002441 D3DSURFACE_DESC desc;
2442 depthStencil->GetDesc(&desc);
2443
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002444 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002445 stencilUnmasked = (0x1 << stencilSize) - 1;
2446
2447 if (stencilUnmasked != 0x0)
2448 {
2449 flags |= D3DCLEAR_STENCIL;
2450 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451 }
2452 }
2453
2454 if (mask != 0)
2455 {
2456 return error(GL_INVALID_VALUE);
2457 }
2458
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002459 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2460 {
2461 return;
2462 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002464 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002465 unorm<8>(mState.colorClearValue.red),
2466 unorm<8>(mState.colorClearValue.green),
2467 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002468 float depth = clamp01(mState.depthClearValue);
2469 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002470
2471 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2472
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002473 if (!renderTarget)
2474 {
2475 return; // Context must be lost, return silently
2476 }
2477
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478 D3DSURFACE_DESC desc;
2479 renderTarget->GetDesc(&desc);
2480
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002481 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002482
2483 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002484 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002486 !(mState.colorMaskRed && mState.colorMaskGreen &&
2487 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488
2489 if (needMaskedColorClear || needMaskedStencilClear)
2490 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002491 // State which is altered in all paths from this point to the clear call is saved.
2492 // State which is altered in only some paths will be flagged dirty in the case that
2493 // that path is taken.
2494 HRESULT hr;
2495 if (mMaskedClearSavedState == NULL)
2496 {
2497 hr = device->BeginStateBlock();
2498 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2499
2500 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2501 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2502 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2503 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2504 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2505 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2506 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2507 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2508 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2509 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2510 device->SetPixelShader(NULL);
2511 device->SetVertexShader(NULL);
2512 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002513 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2514 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2515 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2516 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2517 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2518 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2519 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002520
2521 hr = device->EndStateBlock(&mMaskedClearSavedState);
2522 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2523 }
2524
2525 ASSERT(mMaskedClearSavedState != NULL);
2526
2527 if (mMaskedClearSavedState != NULL)
2528 {
2529 hr = mMaskedClearSavedState->Capture();
2530 ASSERT(SUCCEEDED(hr));
2531 }
2532
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002533 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2534 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2535 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2536 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2537 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2538 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2539 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2540 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2541
2542 if (flags & D3DCLEAR_TARGET)
2543 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002544 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545 }
2546 else
2547 {
2548 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2549 }
2550
2551 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2552 {
2553 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2554 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2555 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2556 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002557 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002558 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2560 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002561 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 }
2563 else
2564 {
2565 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2566 }
2567
2568 device->SetPixelShader(NULL);
2569 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002570 device->SetFVF(D3DFVF_XYZRHW);
2571 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2572 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2573 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2574 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2575 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2576 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2577 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002578
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002579 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2580 quad[0][0] = -0.5f;
2581 quad[0][1] = desc.Height - 0.5f;
2582 quad[0][2] = 0.0f;
2583 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002585 quad[1][0] = desc.Width - 0.5f;
2586 quad[1][1] = desc.Height - 0.5f;
2587 quad[1][2] = 0.0f;
2588 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002590 quad[2][0] = -0.5f;
2591 quad[2][1] = -0.5f;
2592 quad[2][2] = 0.0f;
2593 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002595 quad[3][0] = desc.Width - 0.5f;
2596 quad[3][1] = -0.5f;
2597 quad[3][2] = 0.0f;
2598 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002600 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002601 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602
2603 if (flags & D3DCLEAR_ZBUFFER)
2604 {
2605 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2606 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2607 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2608 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002609
2610 if (mMaskedClearSavedState != NULL)
2611 {
2612 mMaskedClearSavedState->Apply();
2613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002615 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 {
2617 device->Clear(0, NULL, flags, color, depth, stencil);
2618 }
2619}
2620
2621void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2622{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002623 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 {
2625 return error(GL_INVALID_OPERATION);
2626 }
2627
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002628 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 IDirect3DDevice9 *device = getDevice();
2630 D3DPRIMITIVETYPE primitiveType;
2631 int primitiveCount;
2632
2633 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2634 return error(GL_INVALID_ENUM);
2635
2636 if (primitiveCount <= 0)
2637 {
2638 return;
2639 }
2640
2641 if (!applyRenderTarget(false))
2642 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002643 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644 }
2645
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002646 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002647
daniel@transgaming.com83921382011-01-08 05:46:00 +00002648 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002649 if (err != GL_NO_ERROR)
2650 {
2651 return error(err);
2652 }
2653
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002654 applyShaders();
2655 applyTextures();
2656
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002657 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002658 {
2659 return error(GL_INVALID_OPERATION);
2660 }
2661
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002662 if (!cullSkipsDraw(mode))
2663 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002664 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002665
2666 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002667
2668 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2669 {
2670 drawClosingLine(first, first + count - 1);
2671 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002672 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673}
2674
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002675void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002677 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002678 {
2679 return error(GL_INVALID_OPERATION);
2680 }
2681
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002682 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002683 {
2684 return error(GL_INVALID_OPERATION);
2685 }
2686
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002687 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688 IDirect3DDevice9 *device = getDevice();
2689 D3DPRIMITIVETYPE primitiveType;
2690 int primitiveCount;
2691
2692 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2693 return error(GL_INVALID_ENUM);
2694
2695 if (primitiveCount <= 0)
2696 {
2697 return;
2698 }
2699
2700 if (!applyRenderTarget(false))
2701 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002702 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 }
2704
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002705 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002706
2707 TranslatedIndexData indexInfo;
2708 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2709 if (err != GL_NO_ERROR)
2710 {
2711 return error(err);
2712 }
2713
daniel@transgaming.com83921382011-01-08 05:46:00 +00002714 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2715 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002716 if (err != GL_NO_ERROR)
2717 {
2718 return error(err);
2719 }
2720
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721 applyShaders();
2722 applyTextures();
2723
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002724 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002725 {
2726 return error(GL_INVALID_OPERATION);
2727 }
2728
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002729 if (!cullSkipsDraw(mode))
2730 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002731 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002732
daniel@transgaming.com83921382011-01-08 05:46:00 +00002733 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002734
2735 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2736 {
2737 drawClosingLine(count, type, indices);
2738 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740}
2741
2742void Context::finish()
2743{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002744 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002745 IDirect3DDevice9 *device = getDevice();
2746 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002747 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002749 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2750 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002751 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002752 ERR("CreateQuery failed hr=%x\n", result);
2753 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2754 {
2755 return error(GL_OUT_OF_MEMORY);
2756 }
2757 ASSERT(false);
2758 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002759 }
2760
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002761 IDirect3DStateBlock9 *savedState = NULL;
2762 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2763 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002765 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002767
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002768 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002769 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002770 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002771 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002772 ASSERT(false);
2773 return;
2774 }
2775
2776 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2777 if (FAILED(result))
2778 {
2779 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2780 occlusionQuery->Release();
2781 savedState->Release();
2782 ASSERT(false);
2783 return;
2784 }
2785
2786 // Render something outside the render target
2787 device->SetPixelShader(NULL);
2788 device->SetVertexShader(NULL);
2789 device->SetFVF(D3DFVF_XYZRHW);
2790 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2791 display->startScene();
2792 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2793
2794 result = occlusionQuery->Issue(D3DISSUE_END);
2795 if (FAILED(result))
2796 {
2797 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2798 occlusionQuery->Release();
2799 savedState->Apply();
2800 savedState->Release();
2801 ASSERT(false);
2802 return;
2803 }
2804
2805 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2806 {
2807 // Keep polling, but allow other threads to do something useful first
2808 Sleep(0);
2809 }
2810
2811 occlusionQuery->Release();
2812 savedState->Apply();
2813 savedState->Release();
2814
2815 if (result == D3DERR_DEVICELOST)
2816 {
2817 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818 }
2819}
2820
2821void Context::flush()
2822{
2823 IDirect3DDevice9 *device = getDevice();
2824 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002825 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002826
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002827 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2828 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002829 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002830 ERR("CreateQuery failed hr=%x\n", result);
2831 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2832 {
2833 return error(GL_OUT_OF_MEMORY);
2834 }
2835 ASSERT(false);
2836 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002837 }
2838
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002839 result = eventQuery->Issue(D3DISSUE_END);
2840 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002841 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002842 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2843 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002844 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002845 return;
2846 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002847
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002848 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2849 eventQuery->Release();
2850
2851 if (result == D3DERR_DEVICELOST)
2852 {
2853 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002854 }
2855}
2856
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002857void Context::drawClosingLine(unsigned int first, unsigned int last)
2858{
2859 IDirect3DDevice9 *device = getDevice();
2860 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002861 bool succeeded = false;
2862 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002863
2864 if (supports32bitIndices())
2865 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002866 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002867
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002868 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002869 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002870 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2871 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002872
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002873 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2874
2875 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2876 if (data)
2877 {
2878 data[0] = last;
2879 data[1] = first;
2880 mClosingIB->unmap();
2881 offset /= 4;
2882 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002883 }
2884 }
2885 else
2886 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002887 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002888
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002889 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002890 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002891 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2892 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002893
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002894 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2895
2896 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2897 if (data)
2898 {
2899 data[0] = last;
2900 data[1] = first;
2901 mClosingIB->unmap();
2902 offset /= 2;
2903 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002904 }
2905 }
2906
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002907 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002908 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002909 device->SetIndices(mClosingIB->getBuffer());
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002910
jbauman@chromium.org2c199b12011-06-13 22:20:06 +00002911 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002912 }
2913 else
2914 {
2915 ERR("Could not create an index buffer for closing a line loop.");
2916 error(GL_OUT_OF_MEMORY);
2917 }
2918}
2919
2920void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2921{
2922 unsigned int first = 0;
2923 unsigned int last = 0;
2924
2925 if (mState.elementArrayBuffer.get())
2926 {
2927 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2928 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2929 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2930 }
2931
2932 switch (type)
2933 {
2934 case GL_UNSIGNED_BYTE:
2935 first = static_cast<const GLubyte*>(indices)[0];
2936 last = static_cast<const GLubyte*>(indices)[count - 1];
2937 break;
2938 case GL_UNSIGNED_SHORT:
2939 first = static_cast<const GLushort*>(indices)[0];
2940 last = static_cast<const GLushort*>(indices)[count - 1];
2941 break;
2942 case GL_UNSIGNED_INT:
2943 first = static_cast<const GLuint*>(indices)[0];
2944 last = static_cast<const GLuint*>(indices)[count - 1];
2945 break;
2946 default: UNREACHABLE();
2947 }
2948
2949 drawClosingLine(first, last);
2950}
2951
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002952void Context::recordInvalidEnum()
2953{
2954 mInvalidEnum = true;
2955}
2956
2957void Context::recordInvalidValue()
2958{
2959 mInvalidValue = true;
2960}
2961
2962void Context::recordInvalidOperation()
2963{
2964 mInvalidOperation = true;
2965}
2966
2967void Context::recordOutOfMemory()
2968{
2969 mOutOfMemory = true;
2970}
2971
2972void Context::recordInvalidFramebufferOperation()
2973{
2974 mInvalidFramebufferOperation = true;
2975}
2976
2977// Get one of the recorded errors and clear its flag, if any.
2978// [OpenGL ES 2.0.24] section 2.5 page 13.
2979GLenum Context::getError()
2980{
2981 if (mInvalidEnum)
2982 {
2983 mInvalidEnum = false;
2984
2985 return GL_INVALID_ENUM;
2986 }
2987
2988 if (mInvalidValue)
2989 {
2990 mInvalidValue = false;
2991
2992 return GL_INVALID_VALUE;
2993 }
2994
2995 if (mInvalidOperation)
2996 {
2997 mInvalidOperation = false;
2998
2999 return GL_INVALID_OPERATION;
3000 }
3001
3002 if (mOutOfMemory)
3003 {
3004 mOutOfMemory = false;
3005
3006 return GL_OUT_OF_MEMORY;
3007 }
3008
3009 if (mInvalidFramebufferOperation)
3010 {
3011 mInvalidFramebufferOperation = false;
3012
3013 return GL_INVALID_FRAMEBUFFER_OPERATION;
3014 }
3015
3016 return GL_NO_ERROR;
3017}
3018
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003019bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003020{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003021 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003022}
3023
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003024int Context::getMaximumVaryingVectors() const
3025{
3026 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3027}
3028
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003029unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003030{
3031 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3032}
3033
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003034unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003035{
3036 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3037}
3038
daniel@transgaming.com458da142010-11-28 02:03:02 +00003039int Context::getMaximumFragmentUniformVectors() const
3040{
3041 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3042}
3043
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003044int Context::getMaxSupportedSamples() const
3045{
3046 return mMaxSupportedSamples;
3047}
3048
3049int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3050{
3051 if (requested == 0)
3052 {
3053 return requested;
3054 }
3055
3056 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3057 if (itr == mMultiSampleSupport.end())
3058 {
3059 return -1;
3060 }
3061
3062 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3063 {
3064 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3065 {
3066 return i;
3067 }
3068 }
3069
3070 return -1;
3071}
3072
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003073bool Context::supportsEventQueries() const
3074{
3075 return mSupportsEventQueries;
3076}
3077
gman@chromium.org50c526d2011-08-10 05:19:44 +00003078bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003079{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003080 return mSupportsDXT1Textures;
3081}
3082
3083bool Context::supportsDXT3Textures() const
3084{
3085 return mSupportsDXT3Textures;
3086}
3087
3088bool Context::supportsDXT5Textures() const
3089{
3090 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003091}
3092
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003093bool Context::supportsFloatTextures() const
3094{
3095 return mSupportsFloatTextures;
3096}
3097
3098bool Context::supportsFloatLinearFilter() const
3099{
3100 return mSupportsFloatLinearFilter;
3101}
3102
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003103bool Context::supportsFloatRenderableTextures() const
3104{
3105 return mSupportsFloatRenderableTextures;
3106}
3107
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003108bool Context::supportsHalfFloatTextures() const
3109{
3110 return mSupportsHalfFloatTextures;
3111}
3112
3113bool Context::supportsHalfFloatLinearFilter() const
3114{
3115 return mSupportsHalfFloatLinearFilter;
3116}
3117
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003118bool Context::supportsHalfFloatRenderableTextures() const
3119{
3120 return mSupportsHalfFloatRenderableTextures;
3121}
3122
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003123int Context::getMaximumRenderbufferDimension() const
3124{
3125 return mMaxRenderbufferDimension;
3126}
3127
3128int Context::getMaximumTextureDimension() const
3129{
3130 return mMaxTextureDimension;
3131}
3132
3133int Context::getMaximumCubeTextureDimension() const
3134{
3135 return mMaxCubeTextureDimension;
3136}
3137
3138int Context::getMaximumTextureLevel() const
3139{
3140 return mMaxTextureLevel;
3141}
3142
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003143bool Context::supportsLuminanceTextures() const
3144{
3145 return mSupportsLuminanceTextures;
3146}
3147
3148bool Context::supportsLuminanceAlphaTextures() const
3149{
3150 return mSupportsLuminanceAlphaTextures;
3151}
3152
daniel@transgaming.com83921382011-01-08 05:46:00 +00003153bool Context::supports32bitIndices() const
3154{
3155 return mSupports32bitIndices;
3156}
3157
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003158bool Context::supportsNonPower2Texture() const
3159{
3160 return mSupportsNonPower2Texture;
3161}
3162
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003163void Context::detachBuffer(GLuint buffer)
3164{
3165 // [OpenGL ES 2.0.24] section 2.9 page 22:
3166 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3167 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3168
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003169 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003170 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003171 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003172 }
3173
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003174 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003175 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003176 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003177 }
3178
3179 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3180 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003181 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003182 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003183 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003184 }
3185 }
3186}
3187
3188void Context::detachTexture(GLuint texture)
3189{
3190 // [OpenGL ES 2.0.24] section 3.8 page 84:
3191 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3192 // rebound to texture object zero
3193
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003194 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003195 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003196 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003197 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003198 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003199 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003200 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003201 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003202 }
3203 }
3204
3205 // [OpenGL ES 2.0.24] section 4.4 page 112:
3206 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3207 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3208 // image was attached in the currently bound framebuffer.
3209
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003210 Framebuffer *readFramebuffer = getReadFramebuffer();
3211 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003212
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003213 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003214 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003215 readFramebuffer->detachTexture(texture);
3216 }
3217
3218 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3219 {
3220 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003221 }
3222}
3223
3224void Context::detachFramebuffer(GLuint framebuffer)
3225{
3226 // [OpenGL ES 2.0.24] section 4.4 page 107:
3227 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3228 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3229
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003230 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003231 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003232 bindReadFramebuffer(0);
3233 }
3234
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003235 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003236 {
3237 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003238 }
3239}
3240
3241void Context::detachRenderbuffer(GLuint renderbuffer)
3242{
3243 // [OpenGL ES 2.0.24] section 4.4 page 109:
3244 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3245 // had been executed with the target RENDERBUFFER and name of zero.
3246
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003247 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003248 {
3249 bindRenderbuffer(0);
3250 }
3251
3252 // [OpenGL ES 2.0.24] section 4.4 page 111:
3253 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3254 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3255 // point to which this image was attached in the currently bound framebuffer.
3256
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003257 Framebuffer *readFramebuffer = getReadFramebuffer();
3258 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003259
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003260 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003261 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003262 readFramebuffer->detachRenderbuffer(renderbuffer);
3263 }
3264
3265 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3266 {
3267 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003268 }
3269}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003270
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003271Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003272{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003273 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003274
3275 if (t == NULL)
3276 {
3277 static const GLubyte color[] = { 0, 0, 0, 255 };
3278
3279 switch (type)
3280 {
3281 default:
3282 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003283 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003284
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003285 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003286 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003287 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003288 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003289 t = incomplete2d;
3290 }
3291 break;
3292
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003293 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003294 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003295 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003296
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003297 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3298 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3299 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3300 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3301 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3302 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003303
3304 t = incompleteCube;
3305 }
3306 break;
3307 }
3308
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003309 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003310 }
3311
3312 return t;
3313}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003314
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003315bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003316{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003317 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003318}
3319
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003320bool Context::isTriangleMode(GLenum drawMode)
3321{
3322 switch (drawMode)
3323 {
3324 case GL_TRIANGLES:
3325 case GL_TRIANGLE_FAN:
3326 case GL_TRIANGLE_STRIP:
3327 return true;
3328 case GL_POINTS:
3329 case GL_LINES:
3330 case GL_LINE_LOOP:
3331 case GL_LINE_STRIP:
3332 return false;
3333 default: UNREACHABLE();
3334 }
3335
3336 return false;
3337}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003338
3339void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3340{
3341 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3342
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003343 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3344 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3345 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3346 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003347
daniel@transgaming.com83921382011-01-08 05:46:00 +00003348 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003349}
3350
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003351void Context::initExtensionString()
3352{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003353 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003354 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3355 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003356 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003357 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003358 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003359
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003360 if (supportsEventQueries())
3361 {
3362 mExtensionString += "GL_NV_fence ";
3363 }
3364
gman@chromium.org50c526d2011-08-10 05:19:44 +00003365 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003366 {
3367 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3368 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003369
gman@chromium.org50c526d2011-08-10 05:19:44 +00003370 if (supportsDXT3Textures())
3371 {
3372 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3373 }
3374
3375 if (supportsDXT5Textures())
3376 {
3377 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
3378 }
3379
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003380 if (supportsFloatTextures())
3381 {
3382 mExtensionString += "GL_OES_texture_float ";
3383 }
3384
3385 if (supportsHalfFloatTextures())
3386 {
3387 mExtensionString += "GL_OES_texture_half_float ";
3388 }
3389
3390 if (supportsFloatLinearFilter())
3391 {
3392 mExtensionString += "GL_OES_texture_float_linear ";
3393 }
3394
3395 if (supportsHalfFloatLinearFilter())
3396 {
3397 mExtensionString += "GL_OES_texture_half_float_linear ";
3398 }
3399
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003400 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003401 {
3402 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3403 }
3404
daniel@transgaming.com83921382011-01-08 05:46:00 +00003405 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003406 {
3407 mExtensionString += "GL_OES_element_index_uint ";
3408 }
3409
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003410 if (supportsNonPower2Texture())
3411 {
3412 mExtensionString += "GL_OES_texture_npot ";
3413 }
3414
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003415 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3416 if (end != std::string::npos)
3417 {
3418 mExtensionString.resize(end+1);
3419 }
3420}
3421
3422const char *Context::getExtensionString() const
3423{
3424 return mExtensionString.c_str();
3425}
3426
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003427void Context::initRendererString()
3428{
3429 egl::Display *display = getDisplay();
3430 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
3431
3432 mRendererString = "ANGLE (";
3433 mRendererString += identifier->Description;
3434 mRendererString += ")";
3435}
3436
3437const char *Context::getRendererString() const
3438{
3439 return mRendererString.c_str();
3440}
3441
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003442void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3443 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3444 GLbitfield mask)
3445{
3446 IDirect3DDevice9 *device = getDevice();
3447
3448 Framebuffer *readFramebuffer = getReadFramebuffer();
3449 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3450
3451 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3452 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3453 {
3454 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3455 }
3456
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003457 if (drawFramebuffer->getSamples() != 0)
3458 {
3459 return error(GL_INVALID_OPERATION);
3460 }
3461
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003462 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3463 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3464 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3465 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3466
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003467 RECT sourceRect;
3468 RECT destRect;
3469
3470 if (srcX0 < srcX1)
3471 {
3472 sourceRect.left = srcX0;
3473 sourceRect.right = srcX1;
3474 destRect.left = dstX0;
3475 destRect.right = dstX1;
3476 }
3477 else
3478 {
3479 sourceRect.left = srcX1;
3480 destRect.left = dstX1;
3481 sourceRect.right = srcX0;
3482 destRect.right = dstX0;
3483 }
3484
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003485 if (srcY0 < srcY1)
3486 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003487 sourceRect.top = readBufferHeight - srcY1;
3488 destRect.top = drawBufferHeight - dstY1;
3489 sourceRect.bottom = readBufferHeight - srcY0;
3490 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003491 }
3492 else
3493 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003494 sourceRect.top = readBufferHeight - srcY0;
3495 destRect.top = drawBufferHeight - dstY0;
3496 sourceRect.bottom = readBufferHeight - srcY1;
3497 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003498 }
3499
3500 RECT sourceScissoredRect = sourceRect;
3501 RECT destScissoredRect = destRect;
3502
3503 if (mState.scissorTest)
3504 {
3505 // Only write to parts of the destination framebuffer which pass the scissor test
3506 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3507 // rect will be checked against scissorY, rather than the bottom.
3508 if (destRect.left < mState.scissorX)
3509 {
3510 int xDiff = mState.scissorX - destRect.left;
3511 destScissoredRect.left = mState.scissorX;
3512 sourceScissoredRect.left += xDiff;
3513 }
3514
3515 if (destRect.right > mState.scissorX + mState.scissorWidth)
3516 {
3517 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3518 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3519 sourceScissoredRect.right -= xDiff;
3520 }
3521
3522 if (destRect.top < mState.scissorY)
3523 {
3524 int yDiff = mState.scissorY - destRect.top;
3525 destScissoredRect.top = mState.scissorY;
3526 sourceScissoredRect.top += yDiff;
3527 }
3528
3529 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3530 {
3531 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3532 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3533 sourceScissoredRect.bottom -= yDiff;
3534 }
3535 }
3536
3537 bool blitRenderTarget = false;
3538 bool blitDepthStencil = false;
3539
3540 RECT sourceTrimmedRect = sourceScissoredRect;
3541 RECT destTrimmedRect = destScissoredRect;
3542
3543 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3544 // the actual draw and read surfaces.
3545 if (sourceTrimmedRect.left < 0)
3546 {
3547 int xDiff = 0 - sourceTrimmedRect.left;
3548 sourceTrimmedRect.left = 0;
3549 destTrimmedRect.left += xDiff;
3550 }
3551
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003552 if (sourceTrimmedRect.right > readBufferWidth)
3553 {
3554 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3555 sourceTrimmedRect.right = readBufferWidth;
3556 destTrimmedRect.right -= xDiff;
3557 }
3558
3559 if (sourceTrimmedRect.top < 0)
3560 {
3561 int yDiff = 0 - sourceTrimmedRect.top;
3562 sourceTrimmedRect.top = 0;
3563 destTrimmedRect.top += yDiff;
3564 }
3565
3566 if (sourceTrimmedRect.bottom > readBufferHeight)
3567 {
3568 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3569 sourceTrimmedRect.bottom = readBufferHeight;
3570 destTrimmedRect.bottom -= yDiff;
3571 }
3572
3573 if (destTrimmedRect.left < 0)
3574 {
3575 int xDiff = 0 - destTrimmedRect.left;
3576 destTrimmedRect.left = 0;
3577 sourceTrimmedRect.left += xDiff;
3578 }
3579
3580 if (destTrimmedRect.right > drawBufferWidth)
3581 {
3582 int xDiff = destTrimmedRect.right - drawBufferWidth;
3583 destTrimmedRect.right = drawBufferWidth;
3584 sourceTrimmedRect.right -= xDiff;
3585 }
3586
3587 if (destTrimmedRect.top < 0)
3588 {
3589 int yDiff = 0 - destTrimmedRect.top;
3590 destTrimmedRect.top = 0;
3591 sourceTrimmedRect.top += yDiff;
3592 }
3593
3594 if (destTrimmedRect.bottom > drawBufferHeight)
3595 {
3596 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3597 destTrimmedRect.bottom = drawBufferHeight;
3598 sourceTrimmedRect.bottom -= yDiff;
3599 }
3600
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003601 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003602 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3603 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3604 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3605 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003606 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3607 {
3608 partialBufferCopy = true;
3609 }
3610
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003611 if (mask & GL_COLOR_BUFFER_BIT)
3612 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003613 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3614 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3615 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3616 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3617 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003618 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3619 {
3620 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3621 return error(GL_INVALID_OPERATION);
3622 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003623
3624 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3625 {
3626 return error(GL_INVALID_OPERATION);
3627 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003628
3629 blitRenderTarget = true;
3630
3631 }
3632
3633 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3634 {
3635 DepthStencilbuffer *readDSBuffer = NULL;
3636 DepthStencilbuffer *drawDSBuffer = NULL;
3637
3638 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3639 // both a depth and stencil buffer, it will be the same buffer.
3640
3641 if (mask & GL_DEPTH_BUFFER_BIT)
3642 {
3643 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3644 {
3645 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3646 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3647 {
3648 return error(GL_INVALID_OPERATION);
3649 }
3650
3651 blitDepthStencil = true;
3652 readDSBuffer = readFramebuffer->getDepthbuffer();
3653 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3654 }
3655 }
3656
3657 if (mask & GL_STENCIL_BUFFER_BIT)
3658 {
3659 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3660 {
3661 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3662 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3663 {
3664 return error(GL_INVALID_OPERATION);
3665 }
3666
3667 blitDepthStencil = true;
3668 readDSBuffer = readFramebuffer->getStencilbuffer();
3669 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3670 }
3671 }
3672
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003673 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003674 {
3675 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3676 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3677 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003678
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003679 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3680 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003681 {
3682 return error(GL_INVALID_OPERATION);
3683 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003684 }
3685
3686 if (blitRenderTarget || blitDepthStencil)
3687 {
3688 egl::Display *display = getDisplay();
3689 display->endScene();
3690
3691 if (blitRenderTarget)
3692 {
3693 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3694 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3695
3696 if (FAILED(result))
3697 {
3698 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3699 return;
3700 }
3701 }
3702
3703 if (blitDepthStencil)
3704 {
3705 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3706
3707 if (FAILED(result))
3708 {
3709 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3710 return;
3711 }
3712 }
3713 }
3714}
3715
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003716VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3717{
3718 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3719 {
3720 mVertexDeclCache[i].vertexDeclaration = NULL;
3721 mVertexDeclCache[i].lruCount = 0;
3722 }
3723}
3724
3725VertexDeclarationCache::~VertexDeclarationCache()
3726{
3727 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3728 {
3729 if (mVertexDeclCache[i].vertexDeclaration)
3730 {
3731 mVertexDeclCache[i].vertexDeclaration->Release();
3732 }
3733 }
3734}
3735
3736GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3737{
3738 IDirect3DDevice9 *device = getDevice();
3739
3740 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3741 D3DVERTEXELEMENT9 *element = &elements[0];
3742
3743 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3744 {
3745 if (attributes[i].active)
3746 {
3747 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3748
3749 element->Stream = i;
3750 element->Offset = 0;
3751 element->Type = attributes[i].type;
3752 element->Method = D3DDECLMETHOD_DEFAULT;
3753 element->Usage = D3DDECLUSAGE_TEXCOORD;
3754 element->UsageIndex = program->getSemanticIndex(i);
3755 element++;
3756 }
3757 }
3758
3759 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3760 *(element++) = end;
3761
3762 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3763 {
3764 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3765 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3766 {
3767 entry->lruCount = ++mMaxLru;
3768 device->SetVertexDeclaration(entry->vertexDeclaration);
3769
3770 return GL_NO_ERROR;
3771 }
3772 }
3773
3774 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3775
3776 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3777 {
3778 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3779 {
3780 lastCache = &mVertexDeclCache[i];
3781 }
3782 }
3783
3784 if (lastCache->vertexDeclaration != NULL)
3785 {
3786 lastCache->vertexDeclaration->Release();
3787 lastCache->vertexDeclaration = NULL;
3788 }
3789
3790 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3791 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3792 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3793 lastCache->lruCount = ++mMaxLru;
3794
3795 return GL_NO_ERROR;
3796}
3797
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003798}
3799
3800extern "C"
3801{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003802gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003803{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003804 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003805}
3806
3807void glDestroyContext(gl::Context *context)
3808{
3809 delete context;
3810
3811 if (context == gl::getContext())
3812 {
3813 gl::makeCurrent(NULL, NULL, NULL);
3814 }
3815}
3816
3817void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3818{
3819 gl::makeCurrent(context, display, surface);
3820}
3821
3822gl::Context *glGetCurrentContext()
3823{
3824 return gl::getContext();
3825}
3826}