blob: 58d6510838818082b3e93490eb488dcc196e463f [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;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000366 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000367 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000368 mViewportInitialized = false;
369 mRenderTargetDescInitialized = false;
370
371 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000372
373 mClearStateDirty = true;
374 mCullStateDirty = true;
375 mDepthStateDirty = true;
376 mMaskStateDirty = true;
377 mBlendStateDirty = true;
378 mStencilStateDirty = true;
379 mPolygonOffsetStateDirty = true;
380 mScissorStateDirty = true;
381 mSampleStateDirty = true;
382 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000383 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000384}
385
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386void Context::setClearColor(float red, float green, float blue, float alpha)
387{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000388 mState.colorClearValue.red = red;
389 mState.colorClearValue.green = green;
390 mState.colorClearValue.blue = blue;
391 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392}
393
394void Context::setClearDepth(float depth)
395{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000396 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397}
398
399void Context::setClearStencil(int stencil)
400{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000401 mState.stencilClearValue = stencil;
402}
403
404void Context::setCullFace(bool enabled)
405{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000406 if (mState.cullFace != enabled)
407 {
408 mState.cullFace = enabled;
409 mCullStateDirty = true;
410 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000411}
412
413bool Context::isCullFaceEnabled() const
414{
415 return mState.cullFace;
416}
417
418void Context::setCullMode(GLenum mode)
419{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000420 if (mState.cullMode != mode)
421 {
422 mState.cullMode = mode;
423 mCullStateDirty = true;
424 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000425}
426
427void Context::setFrontFace(GLenum front)
428{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000429 if (mState.frontFace != front)
430 {
431 mState.frontFace = front;
432 mFrontFaceDirty = true;
433 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000434}
435
436void Context::setDepthTest(bool enabled)
437{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000438 if (mState.depthTest != enabled)
439 {
440 mState.depthTest = enabled;
441 mDepthStateDirty = true;
442 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000443}
444
445bool Context::isDepthTestEnabled() const
446{
447 return mState.depthTest;
448}
449
450void Context::setDepthFunc(GLenum depthFunc)
451{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000452 if (mState.depthFunc != depthFunc)
453 {
454 mState.depthFunc = depthFunc;
455 mDepthStateDirty = true;
456 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000457}
458
459void Context::setDepthRange(float zNear, float zFar)
460{
461 mState.zNear = zNear;
462 mState.zFar = zFar;
463}
464
465void Context::setBlend(bool enabled)
466{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000467 if (mState.blend != enabled)
468 {
469 mState.blend = enabled;
470 mBlendStateDirty = true;
471 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000472}
473
474bool Context::isBlendEnabled() const
475{
476 return mState.blend;
477}
478
479void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
480{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000481 if (mState.sourceBlendRGB != sourceRGB ||
482 mState.sourceBlendAlpha != sourceAlpha ||
483 mState.destBlendRGB != destRGB ||
484 mState.destBlendAlpha != destAlpha)
485 {
486 mState.sourceBlendRGB = sourceRGB;
487 mState.destBlendRGB = destRGB;
488 mState.sourceBlendAlpha = sourceAlpha;
489 mState.destBlendAlpha = destAlpha;
490 mBlendStateDirty = true;
491 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000492}
493
494void Context::setBlendColor(float red, float green, float blue, float alpha)
495{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000496 if (mState.blendColor.red != red ||
497 mState.blendColor.green != green ||
498 mState.blendColor.blue != blue ||
499 mState.blendColor.alpha != alpha)
500 {
501 mState.blendColor.red = red;
502 mState.blendColor.green = green;
503 mState.blendColor.blue = blue;
504 mState.blendColor.alpha = alpha;
505 mBlendStateDirty = true;
506 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000507}
508
509void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
510{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000511 if (mState.blendEquationRGB != rgbEquation ||
512 mState.blendEquationAlpha != alphaEquation)
513 {
514 mState.blendEquationRGB = rgbEquation;
515 mState.blendEquationAlpha = alphaEquation;
516 mBlendStateDirty = true;
517 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000518}
519
520void Context::setStencilTest(bool enabled)
521{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000522 if (mState.stencilTest != enabled)
523 {
524 mState.stencilTest = enabled;
525 mStencilStateDirty = true;
526 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000527}
528
529bool Context::isStencilTestEnabled() const
530{
531 return mState.stencilTest;
532}
533
534void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
535{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000536 if (mState.stencilFunc != stencilFunc ||
537 mState.stencilRef != stencilRef ||
538 mState.stencilMask != stencilMask)
539 {
540 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000541 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000542 mState.stencilMask = stencilMask;
543 mStencilStateDirty = true;
544 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000545}
546
547void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
548{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000549 if (mState.stencilBackFunc != stencilBackFunc ||
550 mState.stencilBackRef != stencilBackRef ||
551 mState.stencilBackMask != stencilBackMask)
552 {
553 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000554 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000555 mState.stencilBackMask = stencilBackMask;
556 mStencilStateDirty = true;
557 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000558}
559
560void Context::setStencilWritemask(GLuint stencilWritemask)
561{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000562 if (mState.stencilWritemask != stencilWritemask)
563 {
564 mState.stencilWritemask = stencilWritemask;
565 mStencilStateDirty = true;
566 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000567}
568
569void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
570{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000571 if (mState.stencilBackWritemask != stencilBackWritemask)
572 {
573 mState.stencilBackWritemask = stencilBackWritemask;
574 mStencilStateDirty = true;
575 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000576}
577
578void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
579{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000580 if (mState.stencilFail != stencilFail ||
581 mState.stencilPassDepthFail != stencilPassDepthFail ||
582 mState.stencilPassDepthPass != stencilPassDepthPass)
583 {
584 mState.stencilFail = stencilFail;
585 mState.stencilPassDepthFail = stencilPassDepthFail;
586 mState.stencilPassDepthPass = stencilPassDepthPass;
587 mStencilStateDirty = true;
588 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000589}
590
591void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
592{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000593 if (mState.stencilBackFail != stencilBackFail ||
594 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
595 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
596 {
597 mState.stencilBackFail = stencilBackFail;
598 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
599 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
600 mStencilStateDirty = true;
601 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000602}
603
604void Context::setPolygonOffsetFill(bool enabled)
605{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000606 if (mState.polygonOffsetFill != enabled)
607 {
608 mState.polygonOffsetFill = enabled;
609 mPolygonOffsetStateDirty = true;
610 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000611}
612
613bool Context::isPolygonOffsetFillEnabled() const
614{
615 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000616
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000617}
618
619void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
620{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000621 if (mState.polygonOffsetFactor != factor ||
622 mState.polygonOffsetUnits != units)
623 {
624 mState.polygonOffsetFactor = factor;
625 mState.polygonOffsetUnits = units;
626 mPolygonOffsetStateDirty = true;
627 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000628}
629
630void Context::setSampleAlphaToCoverage(bool enabled)
631{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000632 if (mState.sampleAlphaToCoverage != enabled)
633 {
634 mState.sampleAlphaToCoverage = enabled;
635 mSampleStateDirty = true;
636 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000637}
638
639bool Context::isSampleAlphaToCoverageEnabled() const
640{
641 return mState.sampleAlphaToCoverage;
642}
643
644void Context::setSampleCoverage(bool enabled)
645{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000646 if (mState.sampleCoverage != enabled)
647 {
648 mState.sampleCoverage = enabled;
649 mSampleStateDirty = true;
650 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000651}
652
653bool Context::isSampleCoverageEnabled() const
654{
655 return mState.sampleCoverage;
656}
657
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000658void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000659{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000660 if (mState.sampleCoverageValue != value ||
661 mState.sampleCoverageInvert != invert)
662 {
663 mState.sampleCoverageValue = value;
664 mState.sampleCoverageInvert = invert;
665 mSampleStateDirty = true;
666 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000667}
668
669void Context::setScissorTest(bool enabled)
670{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000671 if (mState.scissorTest != enabled)
672 {
673 mState.scissorTest = enabled;
674 mScissorStateDirty = true;
675 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000676}
677
678bool Context::isScissorTestEnabled() const
679{
680 return mState.scissorTest;
681}
682
683void Context::setDither(bool enabled)
684{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000685 if (mState.dither != enabled)
686 {
687 mState.dither = enabled;
688 mDitherStateDirty = true;
689 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000690}
691
692bool Context::isDitherEnabled() const
693{
694 return mState.dither;
695}
696
697void Context::setLineWidth(GLfloat width)
698{
699 mState.lineWidth = width;
700}
701
702void Context::setGenerateMipmapHint(GLenum hint)
703{
704 mState.generateMipmapHint = hint;
705}
706
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000707void Context::setFragmentShaderDerivativeHint(GLenum hint)
708{
709 mState.fragmentShaderDerivativeHint = hint;
710 // TODO: Propagate the hint to shader translator so we can write
711 // ddx, ddx_coarse, or ddx_fine depending on the hint.
712 // Ignore for now. It is valid for implementations to ignore hint.
713}
714
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000715void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
716{
717 mState.viewportX = x;
718 mState.viewportY = y;
719 mState.viewportWidth = width;
720 mState.viewportHeight = height;
721}
722
723void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
724{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000725 if (mState.scissorX != x || mState.scissorY != y ||
726 mState.scissorWidth != width || mState.scissorHeight != height)
727 {
728 mState.scissorX = x;
729 mState.scissorY = y;
730 mState.scissorWidth = width;
731 mState.scissorHeight = height;
732 mScissorStateDirty = true;
733 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734}
735
736void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
737{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000738 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
739 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
740 {
741 mState.colorMaskRed = red;
742 mState.colorMaskGreen = green;
743 mState.colorMaskBlue = blue;
744 mState.colorMaskAlpha = alpha;
745 mMaskStateDirty = true;
746 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000747}
748
749void Context::setDepthMask(bool mask)
750{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000751 if (mState.depthMask != mask)
752 {
753 mState.depthMask = mask;
754 mMaskStateDirty = true;
755 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000756}
757
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000758void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000759{
760 mState.activeSampler = active;
761}
762
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000763GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000764{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000765 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000766}
767
768GLuint Context::getDrawFramebufferHandle() const
769{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000770 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000771}
772
773GLuint Context::getRenderbufferHandle() const
774{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000775 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000776}
777
778GLuint Context::getArrayBufferHandle() const
779{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000780 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000781}
782
daniel@transgaming.com83921382011-01-08 05:46:00 +0000783void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000784{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000785 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000786}
787
daniel@transgaming.com83921382011-01-08 05:46:00 +0000788const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000789{
790 return mState.vertexAttribute[attribNum];
791}
792
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000793void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000794 GLsizei stride, const void *pointer)
795{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000796 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000797 mState.vertexAttribute[attribNum].mSize = size;
798 mState.vertexAttribute[attribNum].mType = type;
799 mState.vertexAttribute[attribNum].mNormalized = normalized;
800 mState.vertexAttribute[attribNum].mStride = stride;
801 mState.vertexAttribute[attribNum].mPointer = pointer;
802}
803
804const void *Context::getVertexAttribPointer(unsigned int attribNum) const
805{
806 return mState.vertexAttribute[attribNum].mPointer;
807}
808
daniel@transgaming.com83921382011-01-08 05:46:00 +0000809const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000810{
811 return mState.vertexAttribute;
812}
813
814void Context::setPackAlignment(GLint alignment)
815{
816 mState.packAlignment = alignment;
817}
818
819GLint Context::getPackAlignment() const
820{
821 return mState.packAlignment;
822}
823
824void Context::setUnpackAlignment(GLint alignment)
825{
826 mState.unpackAlignment = alignment;
827}
828
829GLint Context::getUnpackAlignment() const
830{
831 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834GLuint Context::createBuffer()
835{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000836 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837}
838
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839GLuint Context::createProgram()
840{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000841 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842}
843
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000844GLuint Context::createShader(GLenum type)
845{
846 return mResourceManager->createShader(type);
847}
848
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849GLuint Context::createTexture()
850{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000851 return mResourceManager->createTexture();
852}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000854GLuint Context::createRenderbuffer()
855{
856 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000859// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860GLuint Context::createFramebuffer()
861{
benvanik@google.com1a233342011-04-28 19:44:39 +0000862 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000863
864 mFramebufferMap[handle] = NULL;
865
866 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867}
868
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000869GLuint Context::createFence()
870{
benvanik@google.com1a233342011-04-28 19:44:39 +0000871 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000872
873 mFenceMap[handle] = new Fence;
874
875 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000876}
877
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878void Context::deleteBuffer(GLuint buffer)
879{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000880 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881 {
882 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000884
885 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886}
887
888void Context::deleteShader(GLuint shader)
889{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000890 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
893void Context::deleteProgram(GLuint program)
894{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000895 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896}
897
898void Context::deleteTexture(GLuint texture)
899{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000900 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901 {
902 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000904
905 mResourceManager->deleteTexture(texture);
906}
907
908void Context::deleteRenderbuffer(GLuint renderbuffer)
909{
910 if (mResourceManager->getRenderbuffer(renderbuffer))
911 {
912 detachRenderbuffer(renderbuffer);
913 }
914
915 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916}
917
918void Context::deleteFramebuffer(GLuint framebuffer)
919{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000920 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
921
922 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923 {
924 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000925
benvanik@google.com1a233342011-04-28 19:44:39 +0000926 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000927 delete framebufferObject->second;
928 mFramebufferMap.erase(framebufferObject);
929 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000931
932void Context::deleteFence(GLuint fence)
933{
934 FenceMap::iterator fenceObject = mFenceMap.find(fence);
935
936 if (fenceObject != mFenceMap.end())
937 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000938 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000939 delete fenceObject->second;
940 mFenceMap.erase(fenceObject);
941 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000942}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000944Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000946 return mResourceManager->getBuffer(handle);
947}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000949Shader *Context::getShader(GLuint handle)
950{
951 return mResourceManager->getShader(handle);
952}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000954Program *Context::getProgram(GLuint handle)
955{
956 return mResourceManager->getProgram(handle);
957}
958
959Texture *Context::getTexture(GLuint handle)
960{
961 return mResourceManager->getTexture(handle);
962}
963
964Renderbuffer *Context::getRenderbuffer(GLuint handle)
965{
966 return mResourceManager->getRenderbuffer(handle);
967}
968
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000969Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000970{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000971 return getFramebuffer(mState.readFramebuffer);
972}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000973
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000974Framebuffer *Context::getDrawFramebuffer()
975{
976 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
979void Context::bindArrayBuffer(unsigned int buffer)
980{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000981 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000983 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984}
985
986void Context::bindElementArrayBuffer(unsigned int buffer)
987{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000988 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000990 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991}
992
993void Context::bindTexture2D(GLuint texture)
994{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000995 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000997 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998}
999
1000void Context::bindTextureCubeMap(GLuint texture)
1001{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001002 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001004 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005}
1006
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001007void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001009 if (!getFramebuffer(framebuffer))
1010 {
1011 mFramebufferMap[framebuffer] = new Framebuffer();
1012 }
1013
1014 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001015}
1016
1017void Context::bindDrawFramebuffer(GLuint framebuffer)
1018{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001019 if (!getFramebuffer(framebuffer))
1020 {
1021 mFramebufferMap[framebuffer] = new Framebuffer();
1022 }
1023
1024 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025}
1026
1027void Context::bindRenderbuffer(GLuint renderbuffer)
1028{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001029 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1030
1031 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032}
1033
1034void Context::useProgram(GLuint program)
1035{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001036 GLuint priorProgram = mState.currentProgram;
1037 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001038
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001039 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001041 Program *newProgram = mResourceManager->getProgram(program);
1042 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1043
1044 if (newProgram)
1045 {
1046 newProgram->addRef();
1047 }
1048
1049 if (oldProgram)
1050 {
1051 oldProgram->release();
1052 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001053 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001054}
1055
1056void Context::setFramebufferZero(Framebuffer *buffer)
1057{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001058 delete mFramebufferMap[0];
1059 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060}
1061
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001062void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001064 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1065 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066}
1067
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001068Framebuffer *Context::getFramebuffer(unsigned int handle)
1069{
1070 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1071
1072 if (framebuffer == mFramebufferMap.end())
1073 {
1074 return NULL;
1075 }
1076 else
1077 {
1078 return framebuffer->second;
1079 }
1080}
1081
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001082Fence *Context::getFence(unsigned int handle)
1083{
1084 FenceMap::iterator fence = mFenceMap.find(handle);
1085
1086 if (fence == mFenceMap.end())
1087 {
1088 return NULL;
1089 }
1090 else
1091 {
1092 return fence->second;
1093 }
1094}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001095
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096Buffer *Context::getArrayBuffer()
1097{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001098 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
1101Buffer *Context::getElementArrayBuffer()
1102{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001103 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104}
1105
1106Program *Context::getCurrentProgram()
1107{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001108 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109}
1110
1111Texture2D *Context::getTexture2D()
1112{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001113 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114}
1115
1116TextureCubeMap *Context::getTextureCubeMap()
1117{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001118 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119}
1120
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001121Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001123 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001124
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001125 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001126 {
1127 switch (type)
1128 {
1129 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001130 case TEXTURE_2D: return mTexture2DZero.get();
1131 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001132 }
1133 }
1134
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001135 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001136}
1137
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001138bool Context::getBooleanv(GLenum pname, GLboolean *params)
1139{
1140 switch (pname)
1141 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001142 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1143 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1144 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001145 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001146 params[0] = mState.colorMaskRed;
1147 params[1] = mState.colorMaskGreen;
1148 params[2] = mState.colorMaskBlue;
1149 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001150 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001151 case GL_CULL_FACE: *params = mState.cullFace; break;
1152 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1153 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1154 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1155 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1156 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1157 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1158 case GL_BLEND: *params = mState.blend; break;
1159 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001160 default:
1161 return false;
1162 }
1163
1164 return true;
1165}
1166
1167bool Context::getFloatv(GLenum pname, GLfloat *params)
1168{
1169 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1170 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1171 // GetIntegerv as its native query function. As it would require conversion in any
1172 // case, this should make no difference to the calling application.
1173 switch (pname)
1174 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001175 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1176 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1177 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1178 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1179 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001180 case GL_ALIASED_LINE_WIDTH_RANGE:
1181 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1182 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1183 break;
1184 case GL_ALIASED_POINT_SIZE_RANGE:
1185 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001186 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 +00001187 break;
1188 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001189 params[0] = mState.zNear;
1190 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001191 break;
1192 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001193 params[0] = mState.colorClearValue.red;
1194 params[1] = mState.colorClearValue.green;
1195 params[2] = mState.colorClearValue.blue;
1196 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001197 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001198 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001199 params[0] = mState.blendColor.red;
1200 params[1] = mState.blendColor.green;
1201 params[2] = mState.blendColor.blue;
1202 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001203 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001204 default:
1205 return false;
1206 }
1207
1208 return true;
1209}
1210
1211bool Context::getIntegerv(GLenum pname, GLint *params)
1212{
1213 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1214 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1215 // GetIntegerv as its native query function. As it would require conversion in any
1216 // case, this should make no difference to the calling application. You may find it in
1217 // Context::getFloatv.
1218 switch (pname)
1219 {
1220 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1221 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001222 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001223 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1224 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001225 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001226 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001227 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001228 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001229 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001230 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1231 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001232 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1233 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1234 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001235 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001236 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1237 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1238 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1239 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001240 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001241 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1242 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1243 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1244 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1245 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1246 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1247 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1248 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1249 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1250 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1251 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1252 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1253 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1254 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1255 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1256 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1257 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1258 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1259 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1260 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1261 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1262 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1263 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1264 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001265 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1266 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001267 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001268 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001269 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001270 case GL_MAX_SAMPLES_ANGLE:
1271 {
1272 GLsizei maxSamples = getMaxSupportedSamples();
1273 if (maxSamples != 0)
1274 {
1275 *params = maxSamples;
1276 }
1277 else
1278 {
1279 return false;
1280 }
1281
1282 break;
1283 }
1284 case GL_SAMPLE_BUFFERS:
1285 case GL_SAMPLES:
1286 {
1287 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1288 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1289 {
1290 switch (pname)
1291 {
1292 case GL_SAMPLE_BUFFERS:
1293 if (framebuffer->getSamples() != 0)
1294 {
1295 *params = 1;
1296 }
1297 else
1298 {
1299 *params = 0;
1300 }
1301 break;
1302 case GL_SAMPLES:
1303 *params = framebuffer->getSamples();
1304 break;
1305 }
1306 }
1307 else
1308 {
1309 *params = 0;
1310 }
1311 }
1312 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001313 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1314 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1315 case GL_MAX_VIEWPORT_DIMS:
1316 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001317 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001318 params[0] = maxDimension;
1319 params[1] = maxDimension;
1320 }
1321 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001322 case GL_COMPRESSED_TEXTURE_FORMATS:
1323 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001324 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001325 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001326 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1327 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1328 }
1329 if (supportsDXT3Textures())
1330 {
1331 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1332 }
1333 if (supportsDXT5Textures())
1334 {
1335 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001336 }
1337 }
1338 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001339 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001340 params[0] = mState.viewportX;
1341 params[1] = mState.viewportY;
1342 params[2] = mState.viewportWidth;
1343 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001344 break;
1345 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001346 params[0] = mState.scissorX;
1347 params[1] = mState.scissorY;
1348 params[2] = mState.scissorWidth;
1349 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001350 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001351 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1352 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001353 case GL_RED_BITS:
1354 case GL_GREEN_BITS:
1355 case GL_BLUE_BITS:
1356 case GL_ALPHA_BITS:
1357 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001358 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1360
1361 if (colorbuffer)
1362 {
1363 switch (pname)
1364 {
1365 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1366 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1367 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1368 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1369 }
1370 }
1371 else
1372 {
1373 *params = 0;
1374 }
1375 }
1376 break;
1377 case GL_DEPTH_BITS:
1378 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001379 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001380 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001381
1382 if (depthbuffer)
1383 {
1384 *params = depthbuffer->getDepthSize();
1385 }
1386 else
1387 {
1388 *params = 0;
1389 }
1390 }
1391 break;
1392 case GL_STENCIL_BITS:
1393 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001394 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001395 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001396
1397 if (stencilbuffer)
1398 {
1399 *params = stencilbuffer->getStencilSize();
1400 }
1401 else
1402 {
1403 *params = 0;
1404 }
1405 }
1406 break;
1407 case GL_TEXTURE_BINDING_2D:
1408 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001409 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001410 {
1411 error(GL_INVALID_OPERATION);
1412 return false;
1413 }
1414
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001415 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001416 }
1417 break;
1418 case GL_TEXTURE_BINDING_CUBE_MAP:
1419 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001420 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001421 {
1422 error(GL_INVALID_OPERATION);
1423 return false;
1424 }
1425
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001426 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001427 }
1428 break;
1429 default:
1430 return false;
1431 }
1432
1433 return true;
1434}
1435
1436bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1437{
1438 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1439 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1440 // to the fact that it is stored internally as a float, and so would require conversion
1441 // if returned from Context::getIntegerv. Since this conversion is already implemented
1442 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1443 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1444 // application.
1445 switch (pname)
1446 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001447 case GL_COMPRESSED_TEXTURE_FORMATS:
1448 {
1449 *type = GL_INT;
1450 *numParams = mNumCompressedTextureFormats;
1451 }
1452 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001453 case GL_SHADER_BINARY_FORMATS:
1454 {
1455 *type = GL_INT;
1456 *numParams = 0;
1457 }
1458 break;
1459 case GL_MAX_VERTEX_ATTRIBS:
1460 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1461 case GL_MAX_VARYING_VECTORS:
1462 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1463 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1464 case GL_MAX_TEXTURE_IMAGE_UNITS:
1465 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1466 case GL_MAX_RENDERBUFFER_SIZE:
1467 case GL_NUM_SHADER_BINARY_FORMATS:
1468 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1469 case GL_ARRAY_BUFFER_BINDING:
1470 case GL_FRAMEBUFFER_BINDING:
1471 case GL_RENDERBUFFER_BINDING:
1472 case GL_CURRENT_PROGRAM:
1473 case GL_PACK_ALIGNMENT:
1474 case GL_UNPACK_ALIGNMENT:
1475 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001476 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001477 case GL_RED_BITS:
1478 case GL_GREEN_BITS:
1479 case GL_BLUE_BITS:
1480 case GL_ALPHA_BITS:
1481 case GL_DEPTH_BITS:
1482 case GL_STENCIL_BITS:
1483 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1484 case GL_CULL_FACE_MODE:
1485 case GL_FRONT_FACE:
1486 case GL_ACTIVE_TEXTURE:
1487 case GL_STENCIL_FUNC:
1488 case GL_STENCIL_VALUE_MASK:
1489 case GL_STENCIL_REF:
1490 case GL_STENCIL_FAIL:
1491 case GL_STENCIL_PASS_DEPTH_FAIL:
1492 case GL_STENCIL_PASS_DEPTH_PASS:
1493 case GL_STENCIL_BACK_FUNC:
1494 case GL_STENCIL_BACK_VALUE_MASK:
1495 case GL_STENCIL_BACK_REF:
1496 case GL_STENCIL_BACK_FAIL:
1497 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1498 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1499 case GL_DEPTH_FUNC:
1500 case GL_BLEND_SRC_RGB:
1501 case GL_BLEND_SRC_ALPHA:
1502 case GL_BLEND_DST_RGB:
1503 case GL_BLEND_DST_ALPHA:
1504 case GL_BLEND_EQUATION_RGB:
1505 case GL_BLEND_EQUATION_ALPHA:
1506 case GL_STENCIL_WRITEMASK:
1507 case GL_STENCIL_BACK_WRITEMASK:
1508 case GL_STENCIL_CLEAR_VALUE:
1509 case GL_SUBPIXEL_BITS:
1510 case GL_MAX_TEXTURE_SIZE:
1511 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1512 case GL_SAMPLE_BUFFERS:
1513 case GL_SAMPLES:
1514 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1515 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1516 case GL_TEXTURE_BINDING_2D:
1517 case GL_TEXTURE_BINDING_CUBE_MAP:
1518 {
1519 *type = GL_INT;
1520 *numParams = 1;
1521 }
1522 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001523 case GL_MAX_SAMPLES_ANGLE:
1524 {
1525 if (getMaxSupportedSamples() != 0)
1526 {
1527 *type = GL_INT;
1528 *numParams = 1;
1529 }
1530 else
1531 {
1532 return false;
1533 }
1534 }
1535 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001536 case GL_MAX_VIEWPORT_DIMS:
1537 {
1538 *type = GL_INT;
1539 *numParams = 2;
1540 }
1541 break;
1542 case GL_VIEWPORT:
1543 case GL_SCISSOR_BOX:
1544 {
1545 *type = GL_INT;
1546 *numParams = 4;
1547 }
1548 break;
1549 case GL_SHADER_COMPILER:
1550 case GL_SAMPLE_COVERAGE_INVERT:
1551 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001552 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1553 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1554 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1555 case GL_SAMPLE_COVERAGE:
1556 case GL_SCISSOR_TEST:
1557 case GL_STENCIL_TEST:
1558 case GL_DEPTH_TEST:
1559 case GL_BLEND:
1560 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001561 {
1562 *type = GL_BOOL;
1563 *numParams = 1;
1564 }
1565 break;
1566 case GL_COLOR_WRITEMASK:
1567 {
1568 *type = GL_BOOL;
1569 *numParams = 4;
1570 }
1571 break;
1572 case GL_POLYGON_OFFSET_FACTOR:
1573 case GL_POLYGON_OFFSET_UNITS:
1574 case GL_SAMPLE_COVERAGE_VALUE:
1575 case GL_DEPTH_CLEAR_VALUE:
1576 case GL_LINE_WIDTH:
1577 {
1578 *type = GL_FLOAT;
1579 *numParams = 1;
1580 }
1581 break;
1582 case GL_ALIASED_LINE_WIDTH_RANGE:
1583 case GL_ALIASED_POINT_SIZE_RANGE:
1584 case GL_DEPTH_RANGE:
1585 {
1586 *type = GL_FLOAT;
1587 *numParams = 2;
1588 }
1589 break;
1590 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001591 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001592 {
1593 *type = GL_FLOAT;
1594 *numParams = 4;
1595 }
1596 break;
1597 default:
1598 return false;
1599 }
1600
1601 return true;
1602}
1603
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001604// Applies the render target surface, depth stencil surface, viewport rectangle and
1605// scissor rectangle to the Direct3D 9 device
1606bool Context::applyRenderTarget(bool ignoreViewport)
1607{
1608 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001609
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001610 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611
1612 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1613 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001614 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001615 }
1616
1617 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001618
1619 if (!renderTarget)
1620 {
1621 return false; // Context must be lost
1622 }
1623
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001624 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001626 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001627 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1628 if (renderTargetSerial != mAppliedRenderTargetSerial)
1629 {
1630 device->SetRenderTarget(0, renderTarget);
1631 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001632 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001633 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001634 }
1635
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001636 unsigned int depthbufferSerial = 0;
1637 unsigned int stencilbufferSerial = 0;
1638 if (framebufferObject->getDepthbufferType() != GL_NONE)
1639 {
1640 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001641 if (!depthStencil)
1642 {
1643 ERR("Depth stencil pointer unexpectedly null.");
1644 return false;
1645 }
1646
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001647 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1648 }
1649 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1650 {
1651 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001652 if (!depthStencil)
1653 {
1654 ERR("Depth stencil pointer unexpectedly null.");
1655 return false;
1656 }
1657
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001658 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1659 }
1660
1661 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001662 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001663 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001664 {
1665 device->SetDepthStencilSurface(depthStencil);
1666 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001667 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001668 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001669 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001671 if (!mRenderTargetDescInitialized || renderTargetChanged)
1672 {
1673 renderTarget->GetDesc(&mRenderTargetDesc);
1674 mRenderTargetDescInitialized = true;
1675 }
1676
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001678
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001679 float zNear = clamp01(mState.zNear);
1680 float zFar = clamp01(mState.zFar);
1681
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001682 if (ignoreViewport)
1683 {
1684 viewport.X = 0;
1685 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001686 viewport.Width = mRenderTargetDesc.Width;
1687 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 viewport.MinZ = 0.0f;
1689 viewport.MaxZ = 1.0f;
1690 }
1691 else
1692 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001693 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1694 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1695 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1696 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1697 viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height) - static_cast<LONG>(viewport.Y));
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001698 viewport.MinZ = zNear;
1699 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700 }
1701
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001702 if (viewport.Width <= 0 || viewport.Height <= 0)
1703 {
1704 return false; // Nothing to render
1705 }
1706
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001707 if (!mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
1708 {
1709 device->SetViewport(&viewport);
1710 mSetViewport = viewport;
1711 mViewportInitialized = true;
1712 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001713
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001714 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001716 if (mState.scissorTest)
1717 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001718 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1719 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1720 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1721 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1722 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001723 device->SetScissorRect(&rect);
1724 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1725 }
1726 else
1727 {
1728 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1729 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001730
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001731 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732 }
1733
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001734 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736 Program *programObject = getCurrentProgram();
1737
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001738 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001739 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001740 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001741
daniel@transgaming.com31754962010-11-28 02:02:52 +00001742 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001743 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1744 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1745 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001746 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001747
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001748 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001749 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001750 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001751
daniel@transgaming.com31754962010-11-28 02:02:52 +00001752 GLint depthRange = programObject->getDxDepthRangeLocation();
1753 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1754 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755 }
1756
1757 return true;
1758}
1759
1760// 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 +00001761void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762{
1763 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001764 Program *programObject = getCurrentProgram();
1765
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001766 Framebuffer *framebufferObject = getDrawFramebuffer();
1767
1768 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1769
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001770 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001771 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001772 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001774 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001775 GLint alwaysFront = !isTriangleMode(drawMode);
1776 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1777
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001778 egl::Display *display = getDisplay();
1779 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
1780 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1781 // Apparently some ATI cards have a bug where a draw with a zero color
1782 // write mask can cause later draws to have incorrect results. Instead,
1783 // set a nonzero color write mask but modify the blend state so that no
1784 // drawing is done.
1785 // http://code.google.com/p/angleproject/issues/detail?id=169
1786
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001787 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001788 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001789 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001790 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001791 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001792 }
1793 else
1794 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001795 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001796 }
1797
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001798 mCullStateDirty = false;
1799 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001801 if (mDepthStateDirty)
1802 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001803 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001805 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1806 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001807 }
1808 else
1809 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001810 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001812
1813 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 }
1815
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001816 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1817 {
1818 mBlendStateDirty = true;
1819 mMaskStateDirty = true;
1820 }
1821
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001822 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001823 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001824 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001825 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1827
1828 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1829 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1830 {
1831 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1832 }
1833 else
1834 {
1835 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1836 unorm<8>(mState.blendColor.alpha),
1837 unorm<8>(mState.blendColor.alpha),
1838 unorm<8>(mState.blendColor.alpha)));
1839 }
1840
1841 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1842 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1843 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1844
1845 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1846 mState.destBlendRGB != mState.destBlendAlpha ||
1847 mState.blendEquationRGB != mState.blendEquationAlpha)
1848 {
1849 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1850
1851 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1852 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1853 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001854 }
1855 else
1856 {
1857 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1858 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001859 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001860 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001861 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001862 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001863 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001864
1865 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866 }
1867
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001868 if (mStencilStateDirty || mFrontFaceDirty)
1869 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001870 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001871 {
1872 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1873 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1874
1875 // FIXME: Unsupported by D3D9
1876 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1877 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1878 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1879 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1880 mState.stencilRef != mState.stencilBackRef ||
1881 mState.stencilMask != mState.stencilBackMask)
1882 {
1883 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1884 return error(GL_INVALID_OPERATION);
1885 }
1886
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001887 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001888 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001889 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1890
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001891 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1892 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001893 es2dx::ConvertComparison(mState.stencilFunc));
1894
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001895 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1896 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001897
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001898 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001899 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001900 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001901 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001902 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001903 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1904
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001905 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1906 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001907 es2dx::ConvertComparison(mState.stencilBackFunc));
1908
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001909 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1910 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001911
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001912 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001913 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001914 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001915 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001916 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001917 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1918 }
1919 else
1920 {
1921 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1922 }
1923
1924 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001925 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001926 }
1927
1928 if (mMaskStateDirty)
1929 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001930 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1931 mState.colorMaskBlue, mState.colorMaskAlpha);
1932 if (colorMask == 0 && !zeroColorMaskAllowed)
1933 {
1934 // Enable green channel, but set blending so nothing will be drawn.
1935 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1936 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1937
1938 device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1939 device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1940 device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
1941 }
1942 else
1943 {
1944 device->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
1945 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001946 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1947
1948 mMaskStateDirty = false;
1949 }
1950
1951 if (mPolygonOffsetStateDirty)
1952 {
1953 if (mState.polygonOffsetFill)
1954 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001955 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001956 if (depthbuffer)
1957 {
1958 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1959 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1960 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1961 }
1962 }
1963 else
1964 {
1965 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1966 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1967 }
1968
1969 mPolygonOffsetStateDirty = false;
1970 }
1971
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001972 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001974 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001975 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001976 FIXME("Sample alpha to coverage is unimplemented.");
1977 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001978
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001979 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1980 if (mState.sampleCoverage)
1981 {
1982 unsigned int mask = 0;
1983 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001984 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001985 float threshold = 0.5f;
1986
1987 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001988 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001989 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001990
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001991 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001992 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001993 threshold += 1.0f;
1994 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001995 }
1996 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001997 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001998
1999 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002000 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002001 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002002 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002003
2004 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002005 }
2006 else
2007 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002008 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002009 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002010
2011 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002012 }
2013
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002014 if (mDitherStateDirty)
2015 {
2016 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
2017
2018 mDitherStateDirty = false;
2019 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002020}
2021
daniel@transgaming.com83921382011-01-08 05:46:00 +00002022GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002023{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002024 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002025
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002026 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002027 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002028 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002029 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002030 }
2031
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00002032 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002033}
2034
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002035// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002036GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002037{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002038 IDirect3DDevice9 *device = getDevice();
2039 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002040
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002041 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002042 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002043 if (indexInfo->serial != mAppliedIBSerial)
2044 {
2045 device->SetIndices(indexInfo->indexBuffer);
2046 mAppliedIBSerial = indexInfo->serial;
2047 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002048 }
2049
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002050 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051}
2052
2053// Applies the shaders and shader constants to the Direct3D 9 device
2054void Context::applyShaders()
2055{
2056 IDirect3DDevice9 *device = getDevice();
2057 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002058 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002059 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002060 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2061 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2062
2063 device->SetPixelShader(pixelShader);
2064 device->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002065 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002066 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002067 }
2068
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 programObject->applyUniforms();
2070}
2071
2072// Applies the textures and sampler states to the Direct3D 9 device
2073void Context::applyTextures()
2074{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002075 applyTextures(SAMPLER_PIXEL);
2076
2077 if (mSupportsVertexTexture)
2078 {
2079 applyTextures(SAMPLER_VERTEX);
2080 }
2081}
2082
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002083// For each Direct3D 9 sampler of either the pixel or vertex stage,
2084// looks up the corresponding OpenGL texture image unit and texture type,
2085// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002086void Context::applyTextures(SamplerType type)
2087{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 IDirect3DDevice9 *device = getDevice();
2089 Program *programObject = getCurrentProgram();
2090
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002091 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 +00002092
2093 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002095 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002096 int d3dSampler = (type == SAMPLER_PIXEL) ? samplerIndex : D3DVERTEXTEXTURESAMPLER0 + samplerIndex;
2097 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2098
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002099 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002101 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002102
2103 Texture *texture = getSamplerTexture(textureUnit, textureType);
2104
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002105 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002106 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002107 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2108
2109 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002110 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002111 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002112 {
2113 GLenum wrapS = texture->getWrapS();
2114 GLenum wrapT = texture->getWrapT();
2115 GLenum minFilter = texture->getMinFilter();
2116 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002118 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2119 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002121 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002122 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2123 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002124 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2125 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002128 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002129 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002130 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002131 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002132 }
2133 else
2134 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002135 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002136 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002137
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002138 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002139 texture->resetDirty();
2140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002142 else
2143 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002144 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002145 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002146 device->SetTexture(d3dSampler, NULL);
2147 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002148 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002149 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150 }
2151}
2152
2153void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2154{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002155 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002156
2157 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2158 {
2159 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2160 }
2161
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002162 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2163 {
2164 return error(GL_INVALID_OPERATION);
2165 }
2166
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002168
2169 if (!renderTarget)
2170 {
2171 return; // Context must be lost, return silently
2172 }
2173
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 IDirect3DDevice9 *device = getDevice();
2175
2176 D3DSURFACE_DESC desc;
2177 renderTarget->GetDesc(&desc);
2178
2179 IDirect3DSurface9 *systemSurface;
2180 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2181
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002182 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002183 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002184 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 return error(GL_OUT_OF_MEMORY);
2186 }
2187
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2189 {
2190 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002191 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192 }
2193
2194 result = device->GetRenderTargetData(renderTarget, systemSurface);
2195
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002196 if (FAILED(result))
2197 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002198 systemSurface->Release();
2199
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002200 switch (result)
2201 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002202 // It turns out that D3D will sometimes produce more error
2203 // codes than those documented.
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002204 case D3DERR_DRIVERINTERNALERROR:
2205 case D3DERR_DEVICELOST:
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002206 case D3DERR_DEVICEHUNG:
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002207 return error(GL_OUT_OF_MEMORY);
2208 default:
2209 UNREACHABLE();
2210 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002211 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002212 }
2213
2214 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002215 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2216 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2217 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2218 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2219 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220
2221 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2222
2223 if (FAILED(result))
2224 {
2225 UNREACHABLE();
2226 systemSurface->Release();
2227
2228 return; // No sensible error to generate
2229 }
2230
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002231 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002232 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002233 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002234 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002235 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002236
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002237 for (int j = 0; j < rect.bottom - rect.top; j++)
2238 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002239 if (desc.Format == D3DFMT_A8R8G8B8 &&
2240 format == GL_BGRA_EXT &&
2241 type == GL_UNSIGNED_BYTE)
2242 {
2243 // Fast path for EXT_read_format_bgra, given
2244 // an RGBA source buffer. Note that buffers with no
2245 // alpha go through the slow path below.
2246 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002247 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002248 (rect.right - rect.left) * 4);
2249 continue;
2250 }
2251
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 for (int i = 0; i < rect.right - rect.left; i++)
2253 {
2254 float r;
2255 float g;
2256 float b;
2257 float a;
2258
2259 switch (desc.Format)
2260 {
2261 case D3DFMT_R5G6B5:
2262 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002263 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264
2265 a = 1.0f;
2266 b = (rgb & 0x001F) * (1.0f / 0x001F);
2267 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2268 r = (rgb & 0xF800) * (1.0f / 0xF800);
2269 }
2270 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 case D3DFMT_A1R5G5B5:
2272 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002273 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274
2275 a = (argb & 0x8000) ? 1.0f : 0.0f;
2276 b = (argb & 0x001F) * (1.0f / 0x001F);
2277 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2278 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2279 }
2280 break;
2281 case D3DFMT_A8R8G8B8:
2282 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002283 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284
2285 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2286 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2287 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2288 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2289 }
2290 break;
2291 case D3DFMT_X8R8G8B8:
2292 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002293 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294
2295 a = 1.0f;
2296 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2297 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2298 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2299 }
2300 break;
2301 case D3DFMT_A2R10G10B10:
2302 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002303 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304
2305 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2306 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2307 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2308 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2309 }
2310 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002311 case D3DFMT_A32B32G32R32F:
2312 {
2313 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002314 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2315 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2316 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2317 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002318 }
2319 break;
2320 case D3DFMT_A16B16G16R16F:
2321 {
2322 // float formats in D3D are stored rgba, rather than the other way round
2323 float abgr[4];
2324
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002325 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002326
2327 a = abgr[3];
2328 b = abgr[2];
2329 g = abgr[1];
2330 r = abgr[0];
2331 }
2332 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333 default:
2334 UNIMPLEMENTED(); // FIXME
2335 UNREACHABLE();
2336 }
2337
2338 switch (format)
2339 {
2340 case GL_RGBA:
2341 switch (type)
2342 {
2343 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002344 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2345 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2346 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2347 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 break;
2349 default: UNREACHABLE();
2350 }
2351 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002352 case GL_BGRA_EXT:
2353 switch (type)
2354 {
2355 case GL_UNSIGNED_BYTE:
2356 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2357 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2358 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2359 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2360 break;
2361 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2362 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2363 // this type is packed as follows:
2364 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2365 // --------------------------------------------------------------------------------
2366 // | 4th | 3rd | 2nd | 1st component |
2367 // --------------------------------------------------------------------------------
2368 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2369 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2370 ((unsigned short)(15 * a + 0.5f) << 12)|
2371 ((unsigned short)(15 * r + 0.5f) << 8) |
2372 ((unsigned short)(15 * g + 0.5f) << 4) |
2373 ((unsigned short)(15 * b + 0.5f) << 0);
2374 break;
2375 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2376 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2377 // this type is packed as follows:
2378 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2379 // --------------------------------------------------------------------------------
2380 // | 4th | 3rd | 2nd | 1st component |
2381 // --------------------------------------------------------------------------------
2382 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2383 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2384 ((unsigned short)( a + 0.5f) << 15) |
2385 ((unsigned short)(31 * r + 0.5f) << 10) |
2386 ((unsigned short)(31 * g + 0.5f) << 5) |
2387 ((unsigned short)(31 * b + 0.5f) << 0);
2388 break;
2389 default: UNREACHABLE();
2390 }
2391 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002392 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393 switch (type)
2394 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002395 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002396 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2397 ((unsigned short)(31 * b + 0.5f) << 0) |
2398 ((unsigned short)(63 * g + 0.5f) << 5) |
2399 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 break;
2401 default: UNREACHABLE();
2402 }
2403 break;
2404 default: UNREACHABLE();
2405 }
2406 }
2407 }
2408
2409 systemSurface->UnlockRect();
2410
2411 systemSurface->Release();
2412}
2413
2414void Context::clear(GLbitfield mask)
2415{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002416 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002417
2418 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2419 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002420 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002421 }
2422
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002423 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424 IDirect3DDevice9 *device = getDevice();
2425 DWORD flags = 0;
2426
2427 if (mask & GL_COLOR_BUFFER_BIT)
2428 {
2429 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002430
2431 if (framebufferObject->getColorbufferType() != GL_NONE)
2432 {
2433 flags |= D3DCLEAR_TARGET;
2434 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 }
2436
2437 if (mask & GL_DEPTH_BUFFER_BIT)
2438 {
2439 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002440 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 {
2442 flags |= D3DCLEAR_ZBUFFER;
2443 }
2444 }
2445
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446 GLuint stencilUnmasked = 0x0;
2447
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002448 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002451 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002453 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002454 if (!depthStencil)
2455 {
2456 ERR("Depth stencil pointer unexpectedly null.");
2457 return;
2458 }
2459
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002460 D3DSURFACE_DESC desc;
2461 depthStencil->GetDesc(&desc);
2462
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002463 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002464 stencilUnmasked = (0x1 << stencilSize) - 1;
2465
2466 if (stencilUnmasked != 0x0)
2467 {
2468 flags |= D3DCLEAR_STENCIL;
2469 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002470 }
2471 }
2472
2473 if (mask != 0)
2474 {
2475 return error(GL_INVALID_VALUE);
2476 }
2477
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002478 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2479 {
2480 return;
2481 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002482
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002483 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002484 unorm<8>(mState.colorClearValue.red),
2485 unorm<8>(mState.colorClearValue.green),
2486 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002487 float depth = clamp01(mState.depthClearValue);
2488 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489
2490 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2491
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002492 if (!renderTarget)
2493 {
2494 return; // Context must be lost, return silently
2495 }
2496
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 D3DSURFACE_DESC desc;
2498 renderTarget->GetDesc(&desc);
2499
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002500 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501
2502 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002503 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002505 !(mState.colorMaskRed && mState.colorMaskGreen &&
2506 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507
2508 if (needMaskedColorClear || needMaskedStencilClear)
2509 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002510 // State which is altered in all paths from this point to the clear call is saved.
2511 // State which is altered in only some paths will be flagged dirty in the case that
2512 // that path is taken.
2513 HRESULT hr;
2514 if (mMaskedClearSavedState == NULL)
2515 {
2516 hr = device->BeginStateBlock();
2517 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2518
2519 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2520 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2521 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2522 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2523 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2524 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2525 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2526 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2527 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2528 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2529 device->SetPixelShader(NULL);
2530 device->SetVertexShader(NULL);
2531 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
jbauman@chromium.org23c9e312011-09-21 22:16:44 +00002532 device->SetStreamSource(0, NULL, 0, 0);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002533 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2534 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2535 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2536 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2537 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2538 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2539 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002540
2541 hr = device->EndStateBlock(&mMaskedClearSavedState);
2542 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2543 }
2544
2545 ASSERT(mMaskedClearSavedState != NULL);
2546
2547 if (mMaskedClearSavedState != NULL)
2548 {
2549 hr = mMaskedClearSavedState->Capture();
2550 ASSERT(SUCCEEDED(hr));
2551 }
2552
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2554 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2555 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2556 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2557 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2558 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2559 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2560 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2561
2562 if (flags & D3DCLEAR_TARGET)
2563 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002564 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565 }
2566 else
2567 {
2568 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2569 }
2570
2571 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2572 {
2573 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2574 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2575 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2576 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002577 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002578 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2580 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002581 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002582 }
2583 else
2584 {
2585 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2586 }
2587
2588 device->SetPixelShader(NULL);
2589 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002590 device->SetFVF(D3DFVF_XYZRHW);
2591 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2592 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2593 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2594 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2595 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2596 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2597 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002599 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2600 quad[0][0] = -0.5f;
2601 quad[0][1] = desc.Height - 0.5f;
2602 quad[0][2] = 0.0f;
2603 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002605 quad[1][0] = desc.Width - 0.5f;
2606 quad[1][1] = desc.Height - 0.5f;
2607 quad[1][2] = 0.0f;
2608 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002610 quad[2][0] = -0.5f;
2611 quad[2][1] = -0.5f;
2612 quad[2][2] = 0.0f;
2613 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002615 quad[3][0] = desc.Width - 0.5f;
2616 quad[3][1] = -0.5f;
2617 quad[3][2] = 0.0f;
2618 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002620 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002621 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622
2623 if (flags & D3DCLEAR_ZBUFFER)
2624 {
2625 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2626 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2627 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2628 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002629
2630 if (mMaskedClearSavedState != NULL)
2631 {
2632 mMaskedClearSavedState->Apply();
2633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002635 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002636 {
2637 device->Clear(0, NULL, flags, color, depth, stencil);
2638 }
2639}
2640
2641void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2642{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002643 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644 {
2645 return error(GL_INVALID_OPERATION);
2646 }
2647
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002648 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649 IDirect3DDevice9 *device = getDevice();
2650 D3DPRIMITIVETYPE primitiveType;
2651 int primitiveCount;
2652
2653 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2654 return error(GL_INVALID_ENUM);
2655
2656 if (primitiveCount <= 0)
2657 {
2658 return;
2659 }
2660
2661 if (!applyRenderTarget(false))
2662 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002663 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002664 }
2665
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002666 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002667
daniel@transgaming.com83921382011-01-08 05:46:00 +00002668 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002669 if (err != GL_NO_ERROR)
2670 {
2671 return error(err);
2672 }
2673
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002674 applyShaders();
2675 applyTextures();
2676
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002677 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002678 {
2679 return error(GL_INVALID_OPERATION);
2680 }
2681
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002682 if (!cullSkipsDraw(mode))
2683 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002684 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002685
2686 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002687
2688 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2689 {
2690 drawClosingLine(first, first + count - 1);
2691 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002692 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002693}
2694
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002695void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002696{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002697 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002698 {
2699 return error(GL_INVALID_OPERATION);
2700 }
2701
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002702 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 {
2704 return error(GL_INVALID_OPERATION);
2705 }
2706
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002707 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002708 IDirect3DDevice9 *device = getDevice();
2709 D3DPRIMITIVETYPE primitiveType;
2710 int primitiveCount;
2711
2712 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2713 return error(GL_INVALID_ENUM);
2714
2715 if (primitiveCount <= 0)
2716 {
2717 return;
2718 }
2719
2720 if (!applyRenderTarget(false))
2721 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002722 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002723 }
2724
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002725 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002726
2727 TranslatedIndexData indexInfo;
2728 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2729 if (err != GL_NO_ERROR)
2730 {
2731 return error(err);
2732 }
2733
daniel@transgaming.com83921382011-01-08 05:46:00 +00002734 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2735 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002736 if (err != GL_NO_ERROR)
2737 {
2738 return error(err);
2739 }
2740
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002741 applyShaders();
2742 applyTextures();
2743
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002744 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002745 {
2746 return error(GL_INVALID_OPERATION);
2747 }
2748
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002749 if (!cullSkipsDraw(mode))
2750 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002751 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002752
daniel@transgaming.com83921382011-01-08 05:46:00 +00002753 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002754
2755 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2756 {
2757 drawClosingLine(count, type, indices);
2758 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760}
2761
2762void Context::finish()
2763{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002764 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002765 IDirect3DDevice9 *device = getDevice();
2766 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002767 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002769 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2770 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002771 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002772 ERR("CreateQuery failed hr=%x\n", result);
2773 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2774 {
2775 return error(GL_OUT_OF_MEMORY);
2776 }
2777 ASSERT(false);
2778 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002779 }
2780
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002781 IDirect3DStateBlock9 *savedState = NULL;
2782 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2783 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002785 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002787
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002788 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002789 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002790 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002791 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002792 ASSERT(false);
2793 return;
2794 }
2795
2796 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2797 if (FAILED(result))
2798 {
2799 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2800 occlusionQuery->Release();
2801 savedState->Release();
2802 ASSERT(false);
2803 return;
2804 }
2805
2806 // Render something outside the render target
2807 device->SetPixelShader(NULL);
2808 device->SetVertexShader(NULL);
2809 device->SetFVF(D3DFVF_XYZRHW);
2810 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2811 display->startScene();
2812 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2813
2814 result = occlusionQuery->Issue(D3DISSUE_END);
2815 if (FAILED(result))
2816 {
2817 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2818 occlusionQuery->Release();
2819 savedState->Apply();
2820 savedState->Release();
2821 ASSERT(false);
2822 return;
2823 }
2824
2825 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2826 {
2827 // Keep polling, but allow other threads to do something useful first
2828 Sleep(0);
2829 }
2830
2831 occlusionQuery->Release();
2832 savedState->Apply();
2833 savedState->Release();
2834
2835 if (result == D3DERR_DEVICELOST)
2836 {
2837 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002838 }
2839}
2840
2841void Context::flush()
2842{
2843 IDirect3DDevice9 *device = getDevice();
2844 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002845 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002846
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002847 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2848 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002850 ERR("CreateQuery failed hr=%x\n", result);
2851 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2852 {
2853 return error(GL_OUT_OF_MEMORY);
2854 }
2855 ASSERT(false);
2856 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002857 }
2858
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002859 result = eventQuery->Issue(D3DISSUE_END);
2860 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002861 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002862 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2863 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002864 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002865 return;
2866 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002867
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002868 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2869 eventQuery->Release();
2870
2871 if (result == D3DERR_DEVICELOST)
2872 {
2873 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874 }
2875}
2876
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002877void Context::drawClosingLine(unsigned int first, unsigned int last)
2878{
2879 IDirect3DDevice9 *device = getDevice();
2880 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002881 bool succeeded = false;
2882 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002883
2884 if (supports32bitIndices())
2885 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002886 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002887
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002888 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002889 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002890 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2891 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002892
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002893 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2894
2895 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2896 if (data)
2897 {
2898 data[0] = last;
2899 data[1] = first;
2900 mClosingIB->unmap();
2901 offset /= 4;
2902 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002903 }
2904 }
2905 else
2906 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002907 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002908
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002909 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002910 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002911 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2912 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002913
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002914 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2915
2916 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2917 if (data)
2918 {
2919 data[0] = last;
2920 data[1] = first;
2921 mClosingIB->unmap();
2922 offset /= 2;
2923 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002924 }
2925 }
2926
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002927 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002928 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002929 device->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002930 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002931
jbauman@chromium.org2c199b12011-06-13 22:20:06 +00002932 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002933 }
2934 else
2935 {
2936 ERR("Could not create an index buffer for closing a line loop.");
2937 error(GL_OUT_OF_MEMORY);
2938 }
2939}
2940
2941void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2942{
2943 unsigned int first = 0;
2944 unsigned int last = 0;
2945
2946 if (mState.elementArrayBuffer.get())
2947 {
2948 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2949 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2950 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2951 }
2952
2953 switch (type)
2954 {
2955 case GL_UNSIGNED_BYTE:
2956 first = static_cast<const GLubyte*>(indices)[0];
2957 last = static_cast<const GLubyte*>(indices)[count - 1];
2958 break;
2959 case GL_UNSIGNED_SHORT:
2960 first = static_cast<const GLushort*>(indices)[0];
2961 last = static_cast<const GLushort*>(indices)[count - 1];
2962 break;
2963 case GL_UNSIGNED_INT:
2964 first = static_cast<const GLuint*>(indices)[0];
2965 last = static_cast<const GLuint*>(indices)[count - 1];
2966 break;
2967 default: UNREACHABLE();
2968 }
2969
2970 drawClosingLine(first, last);
2971}
2972
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002973void Context::recordInvalidEnum()
2974{
2975 mInvalidEnum = true;
2976}
2977
2978void Context::recordInvalidValue()
2979{
2980 mInvalidValue = true;
2981}
2982
2983void Context::recordInvalidOperation()
2984{
2985 mInvalidOperation = true;
2986}
2987
2988void Context::recordOutOfMemory()
2989{
2990 mOutOfMemory = true;
2991}
2992
2993void Context::recordInvalidFramebufferOperation()
2994{
2995 mInvalidFramebufferOperation = true;
2996}
2997
2998// Get one of the recorded errors and clear its flag, if any.
2999// [OpenGL ES 2.0.24] section 2.5 page 13.
3000GLenum Context::getError()
3001{
3002 if (mInvalidEnum)
3003 {
3004 mInvalidEnum = false;
3005
3006 return GL_INVALID_ENUM;
3007 }
3008
3009 if (mInvalidValue)
3010 {
3011 mInvalidValue = false;
3012
3013 return GL_INVALID_VALUE;
3014 }
3015
3016 if (mInvalidOperation)
3017 {
3018 mInvalidOperation = false;
3019
3020 return GL_INVALID_OPERATION;
3021 }
3022
3023 if (mOutOfMemory)
3024 {
3025 mOutOfMemory = false;
3026
3027 return GL_OUT_OF_MEMORY;
3028 }
3029
3030 if (mInvalidFramebufferOperation)
3031 {
3032 mInvalidFramebufferOperation = false;
3033
3034 return GL_INVALID_FRAMEBUFFER_OPERATION;
3035 }
3036
3037 return GL_NO_ERROR;
3038}
3039
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003040bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003041{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003042 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003043}
3044
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003045int Context::getMaximumVaryingVectors() const
3046{
3047 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3048}
3049
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003050unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003051{
3052 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3053}
3054
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003055unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003056{
3057 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3058}
3059
daniel@transgaming.com458da142010-11-28 02:03:02 +00003060int Context::getMaximumFragmentUniformVectors() const
3061{
3062 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3063}
3064
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003065int Context::getMaxSupportedSamples() const
3066{
3067 return mMaxSupportedSamples;
3068}
3069
3070int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3071{
3072 if (requested == 0)
3073 {
3074 return requested;
3075 }
3076
3077 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3078 if (itr == mMultiSampleSupport.end())
3079 {
3080 return -1;
3081 }
3082
3083 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3084 {
3085 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3086 {
3087 return i;
3088 }
3089 }
3090
3091 return -1;
3092}
3093
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003094bool Context::supportsEventQueries() const
3095{
3096 return mSupportsEventQueries;
3097}
3098
gman@chromium.org50c526d2011-08-10 05:19:44 +00003099bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003100{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003101 return mSupportsDXT1Textures;
3102}
3103
3104bool Context::supportsDXT3Textures() const
3105{
3106 return mSupportsDXT3Textures;
3107}
3108
3109bool Context::supportsDXT5Textures() const
3110{
3111 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003112}
3113
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003114bool Context::supportsFloatTextures() const
3115{
3116 return mSupportsFloatTextures;
3117}
3118
3119bool Context::supportsFloatLinearFilter() const
3120{
3121 return mSupportsFloatLinearFilter;
3122}
3123
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003124bool Context::supportsFloatRenderableTextures() const
3125{
3126 return mSupportsFloatRenderableTextures;
3127}
3128
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003129bool Context::supportsHalfFloatTextures() const
3130{
3131 return mSupportsHalfFloatTextures;
3132}
3133
3134bool Context::supportsHalfFloatLinearFilter() const
3135{
3136 return mSupportsHalfFloatLinearFilter;
3137}
3138
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003139bool Context::supportsHalfFloatRenderableTextures() const
3140{
3141 return mSupportsHalfFloatRenderableTextures;
3142}
3143
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003144int Context::getMaximumRenderbufferDimension() const
3145{
3146 return mMaxRenderbufferDimension;
3147}
3148
3149int Context::getMaximumTextureDimension() const
3150{
3151 return mMaxTextureDimension;
3152}
3153
3154int Context::getMaximumCubeTextureDimension() const
3155{
3156 return mMaxCubeTextureDimension;
3157}
3158
3159int Context::getMaximumTextureLevel() const
3160{
3161 return mMaxTextureLevel;
3162}
3163
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003164bool Context::supportsLuminanceTextures() const
3165{
3166 return mSupportsLuminanceTextures;
3167}
3168
3169bool Context::supportsLuminanceAlphaTextures() const
3170{
3171 return mSupportsLuminanceAlphaTextures;
3172}
3173
daniel@transgaming.com83921382011-01-08 05:46:00 +00003174bool Context::supports32bitIndices() const
3175{
3176 return mSupports32bitIndices;
3177}
3178
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003179bool Context::supportsNonPower2Texture() const
3180{
3181 return mSupportsNonPower2Texture;
3182}
3183
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003184void Context::detachBuffer(GLuint buffer)
3185{
3186 // [OpenGL ES 2.0.24] section 2.9 page 22:
3187 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3188 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3189
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003190 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003191 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003192 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003193 }
3194
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003195 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003196 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003197 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003198 }
3199
3200 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3201 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003202 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003203 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003204 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003205 }
3206 }
3207}
3208
3209void Context::detachTexture(GLuint texture)
3210{
3211 // [OpenGL ES 2.0.24] section 3.8 page 84:
3212 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3213 // rebound to texture object zero
3214
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003215 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003216 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003217 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003218 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003219 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003220 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003221 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003222 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003223 }
3224 }
3225
3226 // [OpenGL ES 2.0.24] section 4.4 page 112:
3227 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3228 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3229 // image was attached in the currently bound framebuffer.
3230
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003231 Framebuffer *readFramebuffer = getReadFramebuffer();
3232 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003233
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003234 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003235 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003236 readFramebuffer->detachTexture(texture);
3237 }
3238
3239 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3240 {
3241 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003242 }
3243}
3244
3245void Context::detachFramebuffer(GLuint framebuffer)
3246{
3247 // [OpenGL ES 2.0.24] section 4.4 page 107:
3248 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3249 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3250
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003251 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003252 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003253 bindReadFramebuffer(0);
3254 }
3255
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003256 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003257 {
3258 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003259 }
3260}
3261
3262void Context::detachRenderbuffer(GLuint renderbuffer)
3263{
3264 // [OpenGL ES 2.0.24] section 4.4 page 109:
3265 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3266 // had been executed with the target RENDERBUFFER and name of zero.
3267
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003268 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003269 {
3270 bindRenderbuffer(0);
3271 }
3272
3273 // [OpenGL ES 2.0.24] section 4.4 page 111:
3274 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3275 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3276 // point to which this image was attached in the currently bound framebuffer.
3277
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003278 Framebuffer *readFramebuffer = getReadFramebuffer();
3279 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003280
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003281 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003282 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003283 readFramebuffer->detachRenderbuffer(renderbuffer);
3284 }
3285
3286 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3287 {
3288 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003289 }
3290}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003291
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003292Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003293{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003294 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003295
3296 if (t == NULL)
3297 {
3298 static const GLubyte color[] = { 0, 0, 0, 255 };
3299
3300 switch (type)
3301 {
3302 default:
3303 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003304 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003305
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003306 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003307 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003308 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003309 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003310 t = incomplete2d;
3311 }
3312 break;
3313
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003314 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003315 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003316 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003317
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003318 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3319 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3320 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3321 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3322 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3323 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003324
3325 t = incompleteCube;
3326 }
3327 break;
3328 }
3329
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003330 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003331 }
3332
3333 return t;
3334}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003335
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003336bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003337{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003338 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003339}
3340
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003341bool Context::isTriangleMode(GLenum drawMode)
3342{
3343 switch (drawMode)
3344 {
3345 case GL_TRIANGLES:
3346 case GL_TRIANGLE_FAN:
3347 case GL_TRIANGLE_STRIP:
3348 return true;
3349 case GL_POINTS:
3350 case GL_LINES:
3351 case GL_LINE_LOOP:
3352 case GL_LINE_STRIP:
3353 return false;
3354 default: UNREACHABLE();
3355 }
3356
3357 return false;
3358}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003359
3360void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3361{
3362 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3363
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003364 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3365 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3366 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3367 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003368
daniel@transgaming.com83921382011-01-08 05:46:00 +00003369 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003370}
3371
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003372// keep list sorted in following order
3373// OES extensions
3374// EXT extensions
3375// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003376void Context::initExtensionString()
3377{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003378 mExtensionString = "";
3379
3380 // OES extensions
3381 if (supports32bitIndices())
3382 {
3383 mExtensionString += "GL_OES_element_index_uint ";
3384 }
3385
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003386 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003387 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003388 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003389
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003390 if (supportsHalfFloatTextures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003391 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003392 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003393 }
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003394 if (supportsHalfFloatLinearFilter())
3395 {
3396 mExtensionString += "GL_OES_texture_half_float_linear ";
3397 }
3398 if (supportsFloatTextures())
3399 {
3400 mExtensionString += "GL_OES_texture_float ";
3401 }
3402 if (supportsFloatLinearFilter())
3403 {
3404 mExtensionString += "GL_OES_texture_float_linear ";
3405 }
3406
3407 if (supportsNonPower2Texture())
3408 {
3409 mExtensionString += "GL_OES_texture_npot ";
3410 }
3411
3412 // Multi-vendor (EXT) extensions
3413 mExtensionString += "GL_EXT_read_format_bgra ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003414
gman@chromium.org50c526d2011-08-10 05:19:44 +00003415 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003416 {
3417 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3418 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003419
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003420 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003421
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003422 // ANGLE-specific extensions
3423 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003424 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003425 {
3426 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3427 }
3428
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003429 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003430 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003431 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3432 }
3433 if (supportsDXT5Textures())
3434 {
3435 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003436 }
zmo@google.coma574f782011-10-03 21:45:23 +00003437 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003438
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003439 // Other vendor-specific extensions
3440 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003441 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003442 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003443 }
3444
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003445 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3446 if (end != std::string::npos)
3447 {
3448 mExtensionString.resize(end+1);
3449 }
3450}
3451
3452const char *Context::getExtensionString() const
3453{
3454 return mExtensionString.c_str();
3455}
3456
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003457void Context::initRendererString()
3458{
3459 egl::Display *display = getDisplay();
3460 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
3461
3462 mRendererString = "ANGLE (";
3463 mRendererString += identifier->Description;
3464 mRendererString += ")";
3465}
3466
3467const char *Context::getRendererString() const
3468{
3469 return mRendererString.c_str();
3470}
3471
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003472void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3473 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3474 GLbitfield mask)
3475{
3476 IDirect3DDevice9 *device = getDevice();
3477
3478 Framebuffer *readFramebuffer = getReadFramebuffer();
3479 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3480
3481 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3482 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3483 {
3484 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3485 }
3486
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003487 if (drawFramebuffer->getSamples() != 0)
3488 {
3489 return error(GL_INVALID_OPERATION);
3490 }
3491
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003492 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3493 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3494 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3495 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3496
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003497 RECT sourceRect;
3498 RECT destRect;
3499
3500 if (srcX0 < srcX1)
3501 {
3502 sourceRect.left = srcX0;
3503 sourceRect.right = srcX1;
3504 destRect.left = dstX0;
3505 destRect.right = dstX1;
3506 }
3507 else
3508 {
3509 sourceRect.left = srcX1;
3510 destRect.left = dstX1;
3511 sourceRect.right = srcX0;
3512 destRect.right = dstX0;
3513 }
3514
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003515 if (srcY0 < srcY1)
3516 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003517 sourceRect.top = readBufferHeight - srcY1;
3518 destRect.top = drawBufferHeight - dstY1;
3519 sourceRect.bottom = readBufferHeight - srcY0;
3520 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003521 }
3522 else
3523 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003524 sourceRect.top = readBufferHeight - srcY0;
3525 destRect.top = drawBufferHeight - dstY0;
3526 sourceRect.bottom = readBufferHeight - srcY1;
3527 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003528 }
3529
3530 RECT sourceScissoredRect = sourceRect;
3531 RECT destScissoredRect = destRect;
3532
3533 if (mState.scissorTest)
3534 {
3535 // Only write to parts of the destination framebuffer which pass the scissor test
3536 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3537 // rect will be checked against scissorY, rather than the bottom.
3538 if (destRect.left < mState.scissorX)
3539 {
3540 int xDiff = mState.scissorX - destRect.left;
3541 destScissoredRect.left = mState.scissorX;
3542 sourceScissoredRect.left += xDiff;
3543 }
3544
3545 if (destRect.right > mState.scissorX + mState.scissorWidth)
3546 {
3547 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3548 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3549 sourceScissoredRect.right -= xDiff;
3550 }
3551
3552 if (destRect.top < mState.scissorY)
3553 {
3554 int yDiff = mState.scissorY - destRect.top;
3555 destScissoredRect.top = mState.scissorY;
3556 sourceScissoredRect.top += yDiff;
3557 }
3558
3559 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3560 {
3561 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3562 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3563 sourceScissoredRect.bottom -= yDiff;
3564 }
3565 }
3566
3567 bool blitRenderTarget = false;
3568 bool blitDepthStencil = false;
3569
3570 RECT sourceTrimmedRect = sourceScissoredRect;
3571 RECT destTrimmedRect = destScissoredRect;
3572
3573 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3574 // the actual draw and read surfaces.
3575 if (sourceTrimmedRect.left < 0)
3576 {
3577 int xDiff = 0 - sourceTrimmedRect.left;
3578 sourceTrimmedRect.left = 0;
3579 destTrimmedRect.left += xDiff;
3580 }
3581
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003582 if (sourceTrimmedRect.right > readBufferWidth)
3583 {
3584 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3585 sourceTrimmedRect.right = readBufferWidth;
3586 destTrimmedRect.right -= xDiff;
3587 }
3588
3589 if (sourceTrimmedRect.top < 0)
3590 {
3591 int yDiff = 0 - sourceTrimmedRect.top;
3592 sourceTrimmedRect.top = 0;
3593 destTrimmedRect.top += yDiff;
3594 }
3595
3596 if (sourceTrimmedRect.bottom > readBufferHeight)
3597 {
3598 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3599 sourceTrimmedRect.bottom = readBufferHeight;
3600 destTrimmedRect.bottom -= yDiff;
3601 }
3602
3603 if (destTrimmedRect.left < 0)
3604 {
3605 int xDiff = 0 - destTrimmedRect.left;
3606 destTrimmedRect.left = 0;
3607 sourceTrimmedRect.left += xDiff;
3608 }
3609
3610 if (destTrimmedRect.right > drawBufferWidth)
3611 {
3612 int xDiff = destTrimmedRect.right - drawBufferWidth;
3613 destTrimmedRect.right = drawBufferWidth;
3614 sourceTrimmedRect.right -= xDiff;
3615 }
3616
3617 if (destTrimmedRect.top < 0)
3618 {
3619 int yDiff = 0 - destTrimmedRect.top;
3620 destTrimmedRect.top = 0;
3621 sourceTrimmedRect.top += yDiff;
3622 }
3623
3624 if (destTrimmedRect.bottom > drawBufferHeight)
3625 {
3626 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3627 destTrimmedRect.bottom = drawBufferHeight;
3628 sourceTrimmedRect.bottom -= yDiff;
3629 }
3630
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003631 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003632 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3633 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3634 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3635 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003636 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3637 {
3638 partialBufferCopy = true;
3639 }
3640
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003641 if (mask & GL_COLOR_BUFFER_BIT)
3642 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003643 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3644 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3645 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3646 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3647 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003648 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3649 {
3650 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3651 return error(GL_INVALID_OPERATION);
3652 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003653
3654 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3655 {
3656 return error(GL_INVALID_OPERATION);
3657 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003658
3659 blitRenderTarget = true;
3660
3661 }
3662
3663 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3664 {
3665 DepthStencilbuffer *readDSBuffer = NULL;
3666 DepthStencilbuffer *drawDSBuffer = NULL;
3667
3668 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3669 // both a depth and stencil buffer, it will be the same buffer.
3670
3671 if (mask & GL_DEPTH_BUFFER_BIT)
3672 {
3673 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3674 {
3675 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3676 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3677 {
3678 return error(GL_INVALID_OPERATION);
3679 }
3680
3681 blitDepthStencil = true;
3682 readDSBuffer = readFramebuffer->getDepthbuffer();
3683 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3684 }
3685 }
3686
3687 if (mask & GL_STENCIL_BUFFER_BIT)
3688 {
3689 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3690 {
3691 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3692 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3693 {
3694 return error(GL_INVALID_OPERATION);
3695 }
3696
3697 blitDepthStencil = true;
3698 readDSBuffer = readFramebuffer->getStencilbuffer();
3699 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3700 }
3701 }
3702
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003703 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003704 {
3705 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3706 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3707 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003708
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003709 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3710 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003711 {
3712 return error(GL_INVALID_OPERATION);
3713 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003714 }
3715
3716 if (blitRenderTarget || blitDepthStencil)
3717 {
3718 egl::Display *display = getDisplay();
3719 display->endScene();
3720
3721 if (blitRenderTarget)
3722 {
3723 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3724 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3725
3726 if (FAILED(result))
3727 {
3728 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3729 return;
3730 }
3731 }
3732
3733 if (blitDepthStencil)
3734 {
3735 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3736
3737 if (FAILED(result))
3738 {
3739 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3740 return;
3741 }
3742 }
3743 }
3744}
3745
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003746VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3747{
3748 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3749 {
3750 mVertexDeclCache[i].vertexDeclaration = NULL;
3751 mVertexDeclCache[i].lruCount = 0;
3752 }
3753}
3754
3755VertexDeclarationCache::~VertexDeclarationCache()
3756{
3757 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3758 {
3759 if (mVertexDeclCache[i].vertexDeclaration)
3760 {
3761 mVertexDeclCache[i].vertexDeclaration->Release();
3762 }
3763 }
3764}
3765
3766GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3767{
3768 IDirect3DDevice9 *device = getDevice();
3769
3770 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3771 D3DVERTEXELEMENT9 *element = &elements[0];
3772
3773 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3774 {
3775 if (attributes[i].active)
3776 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003777 if (mAppliedVBs[i].serial != attributes[i].serial ||
3778 mAppliedVBs[i].stride != attributes[i].stride ||
3779 mAppliedVBs[i].offset != attributes[i].offset)
3780 {
3781 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3782 mAppliedVBs[i].serial = attributes[i].serial;
3783 mAppliedVBs[i].stride = attributes[i].stride;
3784 mAppliedVBs[i].offset = attributes[i].offset;
3785 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003786
3787 element->Stream = i;
3788 element->Offset = 0;
3789 element->Type = attributes[i].type;
3790 element->Method = D3DDECLMETHOD_DEFAULT;
3791 element->Usage = D3DDECLUSAGE_TEXCOORD;
3792 element->UsageIndex = program->getSemanticIndex(i);
3793 element++;
3794 }
3795 }
3796
3797 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3798 *(element++) = end;
3799
3800 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3801 {
3802 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3803 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3804 {
3805 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003806 if(entry->vertexDeclaration != mLastSetVDecl)
3807 {
3808 device->SetVertexDeclaration(entry->vertexDeclaration);
3809 mLastSetVDecl = entry->vertexDeclaration;
3810 }
3811
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003812 return GL_NO_ERROR;
3813 }
3814 }
3815
3816 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3817
3818 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3819 {
3820 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3821 {
3822 lastCache = &mVertexDeclCache[i];
3823 }
3824 }
3825
3826 if (lastCache->vertexDeclaration != NULL)
3827 {
3828 lastCache->vertexDeclaration->Release();
3829 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003830 // mLastSetVDecl is set to the replacement, so we don't have to worry
3831 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003832 }
3833
3834 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3835 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3836 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003837 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003838 lastCache->lruCount = ++mMaxLru;
3839
3840 return GL_NO_ERROR;
3841}
3842
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003843void VertexDeclarationCache::markStateDirty()
3844{
3845 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3846 {
3847 mAppliedVBs[i].serial = 0;
3848 }
3849
3850 mLastSetVDecl = NULL;
3851}
3852
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003853}
3854
3855extern "C"
3856{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003857gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003858{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003859 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003860}
3861
3862void glDestroyContext(gl::Context *context)
3863{
3864 delete context;
3865
3866 if (context == gl::getContext())
3867 {
3868 gl::makeCurrent(NULL, NULL, NULL);
3869 }
3870}
3871
3872void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3873{
3874 gl::makeCurrent(context, display, surface);
3875}
3876
3877gl::Context *glGetCurrentContext()
3878{
3879 return gl::getContext();
3880}
3881}