blob: 1d88b29a6441a83c91fbd60bb8c2dc4e64aaa675 [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;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000384 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000385}
386
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387void Context::setClearColor(float red, float green, float blue, float alpha)
388{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000389 mState.colorClearValue.red = red;
390 mState.colorClearValue.green = green;
391 mState.colorClearValue.blue = blue;
392 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393}
394
395void Context::setClearDepth(float depth)
396{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000397 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398}
399
400void Context::setClearStencil(int stencil)
401{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000402 mState.stencilClearValue = stencil;
403}
404
405void Context::setCullFace(bool enabled)
406{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000407 if (mState.cullFace != enabled)
408 {
409 mState.cullFace = enabled;
410 mCullStateDirty = true;
411 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000412}
413
414bool Context::isCullFaceEnabled() const
415{
416 return mState.cullFace;
417}
418
419void Context::setCullMode(GLenum mode)
420{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000421 if (mState.cullMode != mode)
422 {
423 mState.cullMode = mode;
424 mCullStateDirty = true;
425 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000426}
427
428void Context::setFrontFace(GLenum front)
429{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000430 if (mState.frontFace != front)
431 {
432 mState.frontFace = front;
433 mFrontFaceDirty = true;
434 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435}
436
437void Context::setDepthTest(bool enabled)
438{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000439 if (mState.depthTest != enabled)
440 {
441 mState.depthTest = enabled;
442 mDepthStateDirty = true;
443 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000444}
445
446bool Context::isDepthTestEnabled() const
447{
448 return mState.depthTest;
449}
450
451void Context::setDepthFunc(GLenum depthFunc)
452{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000453 if (mState.depthFunc != depthFunc)
454 {
455 mState.depthFunc = depthFunc;
456 mDepthStateDirty = true;
457 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000458}
459
460void Context::setDepthRange(float zNear, float zFar)
461{
462 mState.zNear = zNear;
463 mState.zFar = zFar;
464}
465
466void Context::setBlend(bool enabled)
467{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000468 if (mState.blend != enabled)
469 {
470 mState.blend = enabled;
471 mBlendStateDirty = true;
472 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000473}
474
475bool Context::isBlendEnabled() const
476{
477 return mState.blend;
478}
479
480void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
481{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000482 if (mState.sourceBlendRGB != sourceRGB ||
483 mState.sourceBlendAlpha != sourceAlpha ||
484 mState.destBlendRGB != destRGB ||
485 mState.destBlendAlpha != destAlpha)
486 {
487 mState.sourceBlendRGB = sourceRGB;
488 mState.destBlendRGB = destRGB;
489 mState.sourceBlendAlpha = sourceAlpha;
490 mState.destBlendAlpha = destAlpha;
491 mBlendStateDirty = true;
492 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000493}
494
495void Context::setBlendColor(float red, float green, float blue, float alpha)
496{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 if (mState.blendColor.red != red ||
498 mState.blendColor.green != green ||
499 mState.blendColor.blue != blue ||
500 mState.blendColor.alpha != alpha)
501 {
502 mState.blendColor.red = red;
503 mState.blendColor.green = green;
504 mState.blendColor.blue = blue;
505 mState.blendColor.alpha = alpha;
506 mBlendStateDirty = true;
507 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000508}
509
510void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
511{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000512 if (mState.blendEquationRGB != rgbEquation ||
513 mState.blendEquationAlpha != alphaEquation)
514 {
515 mState.blendEquationRGB = rgbEquation;
516 mState.blendEquationAlpha = alphaEquation;
517 mBlendStateDirty = true;
518 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000519}
520
521void Context::setStencilTest(bool enabled)
522{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000523 if (mState.stencilTest != enabled)
524 {
525 mState.stencilTest = enabled;
526 mStencilStateDirty = true;
527 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000528}
529
530bool Context::isStencilTestEnabled() const
531{
532 return mState.stencilTest;
533}
534
535void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
536{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000537 if (mState.stencilFunc != stencilFunc ||
538 mState.stencilRef != stencilRef ||
539 mState.stencilMask != stencilMask)
540 {
541 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000542 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000543 mState.stencilMask = stencilMask;
544 mStencilStateDirty = true;
545 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000546}
547
548void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
549{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000550 if (mState.stencilBackFunc != stencilBackFunc ||
551 mState.stencilBackRef != stencilBackRef ||
552 mState.stencilBackMask != stencilBackMask)
553 {
554 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000555 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000556 mState.stencilBackMask = stencilBackMask;
557 mStencilStateDirty = true;
558 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000559}
560
561void Context::setStencilWritemask(GLuint stencilWritemask)
562{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000563 if (mState.stencilWritemask != stencilWritemask)
564 {
565 mState.stencilWritemask = stencilWritemask;
566 mStencilStateDirty = true;
567 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000568}
569
570void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
571{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000572 if (mState.stencilBackWritemask != stencilBackWritemask)
573 {
574 mState.stencilBackWritemask = stencilBackWritemask;
575 mStencilStateDirty = true;
576 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000577}
578
579void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
580{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000581 if (mState.stencilFail != stencilFail ||
582 mState.stencilPassDepthFail != stencilPassDepthFail ||
583 mState.stencilPassDepthPass != stencilPassDepthPass)
584 {
585 mState.stencilFail = stencilFail;
586 mState.stencilPassDepthFail = stencilPassDepthFail;
587 mState.stencilPassDepthPass = stencilPassDepthPass;
588 mStencilStateDirty = true;
589 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000590}
591
592void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
593{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000594 if (mState.stencilBackFail != stencilBackFail ||
595 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
596 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
597 {
598 mState.stencilBackFail = stencilBackFail;
599 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
600 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
601 mStencilStateDirty = true;
602 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000603}
604
605void Context::setPolygonOffsetFill(bool enabled)
606{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000607 if (mState.polygonOffsetFill != enabled)
608 {
609 mState.polygonOffsetFill = enabled;
610 mPolygonOffsetStateDirty = true;
611 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000612}
613
614bool Context::isPolygonOffsetFillEnabled() const
615{
616 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000617
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000618}
619
620void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
621{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000622 if (mState.polygonOffsetFactor != factor ||
623 mState.polygonOffsetUnits != units)
624 {
625 mState.polygonOffsetFactor = factor;
626 mState.polygonOffsetUnits = units;
627 mPolygonOffsetStateDirty = true;
628 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000629}
630
631void Context::setSampleAlphaToCoverage(bool enabled)
632{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000633 if (mState.sampleAlphaToCoverage != enabled)
634 {
635 mState.sampleAlphaToCoverage = enabled;
636 mSampleStateDirty = true;
637 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000638}
639
640bool Context::isSampleAlphaToCoverageEnabled() const
641{
642 return mState.sampleAlphaToCoverage;
643}
644
645void Context::setSampleCoverage(bool enabled)
646{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000647 if (mState.sampleCoverage != enabled)
648 {
649 mState.sampleCoverage = enabled;
650 mSampleStateDirty = true;
651 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000652}
653
654bool Context::isSampleCoverageEnabled() const
655{
656 return mState.sampleCoverage;
657}
658
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000659void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000660{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000661 if (mState.sampleCoverageValue != value ||
662 mState.sampleCoverageInvert != invert)
663 {
664 mState.sampleCoverageValue = value;
665 mState.sampleCoverageInvert = invert;
666 mSampleStateDirty = true;
667 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000668}
669
670void Context::setScissorTest(bool enabled)
671{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000672 if (mState.scissorTest != enabled)
673 {
674 mState.scissorTest = enabled;
675 mScissorStateDirty = true;
676 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000677}
678
679bool Context::isScissorTestEnabled() const
680{
681 return mState.scissorTest;
682}
683
684void Context::setDither(bool enabled)
685{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000686 if (mState.dither != enabled)
687 {
688 mState.dither = enabled;
689 mDitherStateDirty = true;
690 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000691}
692
693bool Context::isDitherEnabled() const
694{
695 return mState.dither;
696}
697
698void Context::setLineWidth(GLfloat width)
699{
700 mState.lineWidth = width;
701}
702
703void Context::setGenerateMipmapHint(GLenum hint)
704{
705 mState.generateMipmapHint = hint;
706}
707
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000708void Context::setFragmentShaderDerivativeHint(GLenum hint)
709{
710 mState.fragmentShaderDerivativeHint = hint;
711 // TODO: Propagate the hint to shader translator so we can write
712 // ddx, ddx_coarse, or ddx_fine depending on the hint.
713 // Ignore for now. It is valid for implementations to ignore hint.
714}
715
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000716void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
717{
718 mState.viewportX = x;
719 mState.viewportY = y;
720 mState.viewportWidth = width;
721 mState.viewportHeight = height;
722}
723
724void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
725{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000726 if (mState.scissorX != x || mState.scissorY != y ||
727 mState.scissorWidth != width || mState.scissorHeight != height)
728 {
729 mState.scissorX = x;
730 mState.scissorY = y;
731 mState.scissorWidth = width;
732 mState.scissorHeight = height;
733 mScissorStateDirty = true;
734 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000735}
736
737void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
738{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000739 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
740 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
741 {
742 mState.colorMaskRed = red;
743 mState.colorMaskGreen = green;
744 mState.colorMaskBlue = blue;
745 mState.colorMaskAlpha = alpha;
746 mMaskStateDirty = true;
747 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000748}
749
750void Context::setDepthMask(bool mask)
751{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000752 if (mState.depthMask != mask)
753 {
754 mState.depthMask = mask;
755 mMaskStateDirty = true;
756 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000757}
758
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000759void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000760{
761 mState.activeSampler = active;
762}
763
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000764GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000765{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000766 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000767}
768
769GLuint Context::getDrawFramebufferHandle() const
770{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000771 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000772}
773
774GLuint Context::getRenderbufferHandle() const
775{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000776 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000777}
778
779GLuint Context::getArrayBufferHandle() const
780{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000781 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000782}
783
daniel@transgaming.com83921382011-01-08 05:46:00 +0000784void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000785{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000786 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000787}
788
daniel@transgaming.com83921382011-01-08 05:46:00 +0000789const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000790{
791 return mState.vertexAttribute[attribNum];
792}
793
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000794void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000795 GLsizei stride, const void *pointer)
796{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000797 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000798 mState.vertexAttribute[attribNum].mSize = size;
799 mState.vertexAttribute[attribNum].mType = type;
800 mState.vertexAttribute[attribNum].mNormalized = normalized;
801 mState.vertexAttribute[attribNum].mStride = stride;
802 mState.vertexAttribute[attribNum].mPointer = pointer;
803}
804
805const void *Context::getVertexAttribPointer(unsigned int attribNum) const
806{
807 return mState.vertexAttribute[attribNum].mPointer;
808}
809
daniel@transgaming.com83921382011-01-08 05:46:00 +0000810const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000811{
812 return mState.vertexAttribute;
813}
814
815void Context::setPackAlignment(GLint alignment)
816{
817 mState.packAlignment = alignment;
818}
819
820GLint Context::getPackAlignment() const
821{
822 return mState.packAlignment;
823}
824
825void Context::setUnpackAlignment(GLint alignment)
826{
827 mState.unpackAlignment = alignment;
828}
829
830GLint Context::getUnpackAlignment() const
831{
832 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833}
834
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835GLuint Context::createBuffer()
836{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000837 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838}
839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840GLuint Context::createProgram()
841{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000842 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843}
844
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000845GLuint Context::createShader(GLenum type)
846{
847 return mResourceManager->createShader(type);
848}
849
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850GLuint Context::createTexture()
851{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000852 return mResourceManager->createTexture();
853}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000855GLuint Context::createRenderbuffer()
856{
857 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000860// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861GLuint Context::createFramebuffer()
862{
benvanik@google.com1a233342011-04-28 19:44:39 +0000863 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000864
865 mFramebufferMap[handle] = NULL;
866
867 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868}
869
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000870GLuint Context::createFence()
871{
benvanik@google.com1a233342011-04-28 19:44:39 +0000872 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000873
874 mFenceMap[handle] = new Fence;
875
876 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000877}
878
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879void Context::deleteBuffer(GLuint buffer)
880{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000881 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882 {
883 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000885
886 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
889void Context::deleteShader(GLuint shader)
890{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000891 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892}
893
894void Context::deleteProgram(GLuint program)
895{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000896 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000897 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
900void Context::deleteTexture(GLuint texture)
901{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000902 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903 {
904 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000906
907 mResourceManager->deleteTexture(texture);
908}
909
910void Context::deleteRenderbuffer(GLuint renderbuffer)
911{
912 if (mResourceManager->getRenderbuffer(renderbuffer))
913 {
914 detachRenderbuffer(renderbuffer);
915 }
916
917 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918}
919
920void Context::deleteFramebuffer(GLuint framebuffer)
921{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000922 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
923
924 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925 {
926 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000927
benvanik@google.com1a233342011-04-28 19:44:39 +0000928 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000929 delete framebufferObject->second;
930 mFramebufferMap.erase(framebufferObject);
931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000933
934void Context::deleteFence(GLuint fence)
935{
936 FenceMap::iterator fenceObject = mFenceMap.find(fence);
937
938 if (fenceObject != mFenceMap.end())
939 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000940 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000941 delete fenceObject->second;
942 mFenceMap.erase(fenceObject);
943 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000944}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000946Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000948 return mResourceManager->getBuffer(handle);
949}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000951Shader *Context::getShader(GLuint handle)
952{
953 return mResourceManager->getShader(handle);
954}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000956Program *Context::getProgram(GLuint handle)
957{
958 return mResourceManager->getProgram(handle);
959}
960
961Texture *Context::getTexture(GLuint handle)
962{
963 return mResourceManager->getTexture(handle);
964}
965
966Renderbuffer *Context::getRenderbuffer(GLuint handle)
967{
968 return mResourceManager->getRenderbuffer(handle);
969}
970
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000971Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000972{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000973 return getFramebuffer(mState.readFramebuffer);
974}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000975
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000976Framebuffer *Context::getDrawFramebuffer()
977{
978 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979}
980
981void Context::bindArrayBuffer(unsigned int buffer)
982{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000983 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000985 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
988void Context::bindElementArrayBuffer(unsigned int buffer)
989{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000990 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000992 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
995void Context::bindTexture2D(GLuint texture)
996{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000997 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000999 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000}
1001
1002void Context::bindTextureCubeMap(GLuint texture)
1003{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001004 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001006 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007}
1008
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001009void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001011 if (!getFramebuffer(framebuffer))
1012 {
1013 mFramebufferMap[framebuffer] = new Framebuffer();
1014 }
1015
1016 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001017}
1018
1019void Context::bindDrawFramebuffer(GLuint framebuffer)
1020{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001021 if (!getFramebuffer(framebuffer))
1022 {
1023 mFramebufferMap[framebuffer] = new Framebuffer();
1024 }
1025
1026 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027}
1028
1029void Context::bindRenderbuffer(GLuint renderbuffer)
1030{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001031 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1032
1033 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034}
1035
1036void Context::useProgram(GLuint program)
1037{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001038 GLuint priorProgram = mState.currentProgram;
1039 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001040
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001041 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001043 Program *newProgram = mResourceManager->getProgram(program);
1044 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001045 mCachedCurrentProgram = NULL;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001046
1047 if (newProgram)
1048 {
1049 newProgram->addRef();
1050 }
1051
1052 if (oldProgram)
1053 {
1054 oldProgram->release();
1055 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057}
1058
1059void Context::setFramebufferZero(Framebuffer *buffer)
1060{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001061 delete mFramebufferMap[0];
1062 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063}
1064
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001065void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001067 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1068 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069}
1070
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001071Framebuffer *Context::getFramebuffer(unsigned int handle)
1072{
1073 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1074
1075 if (framebuffer == mFramebufferMap.end())
1076 {
1077 return NULL;
1078 }
1079 else
1080 {
1081 return framebuffer->second;
1082 }
1083}
1084
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001085Fence *Context::getFence(unsigned int handle)
1086{
1087 FenceMap::iterator fence = mFenceMap.find(handle);
1088
1089 if (fence == mFenceMap.end())
1090 {
1091 return NULL;
1092 }
1093 else
1094 {
1095 return fence->second;
1096 }
1097}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001098
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099Buffer *Context::getArrayBuffer()
1100{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001101 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102}
1103
1104Buffer *Context::getElementArrayBuffer()
1105{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001106 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107}
1108
1109Program *Context::getCurrentProgram()
1110{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001111 if (!mCachedCurrentProgram)
1112 {
1113 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1114 }
1115 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116}
1117
1118Texture2D *Context::getTexture2D()
1119{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001120 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121}
1122
1123TextureCubeMap *Context::getTextureCubeMap()
1124{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001125 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001126}
1127
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001128Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001130 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001131
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001132 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001133 {
1134 switch (type)
1135 {
1136 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001137 case TEXTURE_2D: return mTexture2DZero.get();
1138 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001139 }
1140 }
1141
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001142 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001143}
1144
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001145bool Context::getBooleanv(GLenum pname, GLboolean *params)
1146{
1147 switch (pname)
1148 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001149 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1150 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1151 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001152 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001153 params[0] = mState.colorMaskRed;
1154 params[1] = mState.colorMaskGreen;
1155 params[2] = mState.colorMaskBlue;
1156 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001157 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001158 case GL_CULL_FACE: *params = mState.cullFace; break;
1159 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1160 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1161 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1162 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1163 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1164 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1165 case GL_BLEND: *params = mState.blend; break;
1166 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001167 default:
1168 return false;
1169 }
1170
1171 return true;
1172}
1173
1174bool Context::getFloatv(GLenum pname, GLfloat *params)
1175{
1176 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1177 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1178 // GetIntegerv as its native query function. As it would require conversion in any
1179 // case, this should make no difference to the calling application.
1180 switch (pname)
1181 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001182 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1183 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1184 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1185 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1186 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001187 case GL_ALIASED_LINE_WIDTH_RANGE:
1188 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1189 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1190 break;
1191 case GL_ALIASED_POINT_SIZE_RANGE:
1192 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001193 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 +00001194 break;
1195 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001196 params[0] = mState.zNear;
1197 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001198 break;
1199 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001200 params[0] = mState.colorClearValue.red;
1201 params[1] = mState.colorClearValue.green;
1202 params[2] = mState.colorClearValue.blue;
1203 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001204 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001205 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001206 params[0] = mState.blendColor.red;
1207 params[1] = mState.blendColor.green;
1208 params[2] = mState.blendColor.blue;
1209 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001210 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001211 default:
1212 return false;
1213 }
1214
1215 return true;
1216}
1217
1218bool Context::getIntegerv(GLenum pname, GLint *params)
1219{
1220 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1221 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1222 // GetIntegerv as its native query function. As it would require conversion in any
1223 // case, this should make no difference to the calling application. You may find it in
1224 // Context::getFloatv.
1225 switch (pname)
1226 {
1227 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1228 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001229 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001230 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1231 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001232 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001233 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001234 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001235 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001236 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001237 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1238 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001239 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1240 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1241 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001242 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001243 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1244 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1245 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1246 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001247 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001248 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1249 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1250 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1251 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1252 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1253 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1254 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1255 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1256 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1257 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1258 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1259 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1260 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1261 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1262 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1263 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1264 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1265 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1266 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1267 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1268 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1269 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1270 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1271 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001272 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1273 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001274 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001275 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001276 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001277 case GL_MAX_SAMPLES_ANGLE:
1278 {
1279 GLsizei maxSamples = getMaxSupportedSamples();
1280 if (maxSamples != 0)
1281 {
1282 *params = maxSamples;
1283 }
1284 else
1285 {
1286 return false;
1287 }
1288
1289 break;
1290 }
1291 case GL_SAMPLE_BUFFERS:
1292 case GL_SAMPLES:
1293 {
1294 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1295 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1296 {
1297 switch (pname)
1298 {
1299 case GL_SAMPLE_BUFFERS:
1300 if (framebuffer->getSamples() != 0)
1301 {
1302 *params = 1;
1303 }
1304 else
1305 {
1306 *params = 0;
1307 }
1308 break;
1309 case GL_SAMPLES:
1310 *params = framebuffer->getSamples();
1311 break;
1312 }
1313 }
1314 else
1315 {
1316 *params = 0;
1317 }
1318 }
1319 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001320 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1321 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1322 case GL_MAX_VIEWPORT_DIMS:
1323 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001324 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001325 params[0] = maxDimension;
1326 params[1] = maxDimension;
1327 }
1328 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001329 case GL_COMPRESSED_TEXTURE_FORMATS:
1330 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001331 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001332 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001333 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1334 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1335 }
1336 if (supportsDXT3Textures())
1337 {
1338 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1339 }
1340 if (supportsDXT5Textures())
1341 {
1342 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001343 }
1344 }
1345 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001346 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001347 params[0] = mState.viewportX;
1348 params[1] = mState.viewportY;
1349 params[2] = mState.viewportWidth;
1350 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001351 break;
1352 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001353 params[0] = mState.scissorX;
1354 params[1] = mState.scissorY;
1355 params[2] = mState.scissorWidth;
1356 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001357 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001358 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1359 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001360 case GL_RED_BITS:
1361 case GL_GREEN_BITS:
1362 case GL_BLUE_BITS:
1363 case GL_ALPHA_BITS:
1364 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001365 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001366 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1367
1368 if (colorbuffer)
1369 {
1370 switch (pname)
1371 {
1372 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1373 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1374 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1375 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1376 }
1377 }
1378 else
1379 {
1380 *params = 0;
1381 }
1382 }
1383 break;
1384 case GL_DEPTH_BITS:
1385 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001386 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001387 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001388
1389 if (depthbuffer)
1390 {
1391 *params = depthbuffer->getDepthSize();
1392 }
1393 else
1394 {
1395 *params = 0;
1396 }
1397 }
1398 break;
1399 case GL_STENCIL_BITS:
1400 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001401 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001402 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001403
1404 if (stencilbuffer)
1405 {
1406 *params = stencilbuffer->getStencilSize();
1407 }
1408 else
1409 {
1410 *params = 0;
1411 }
1412 }
1413 break;
1414 case GL_TEXTURE_BINDING_2D:
1415 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001416 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001417 {
1418 error(GL_INVALID_OPERATION);
1419 return false;
1420 }
1421
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001422 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001423 }
1424 break;
1425 case GL_TEXTURE_BINDING_CUBE_MAP:
1426 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001427 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001428 {
1429 error(GL_INVALID_OPERATION);
1430 return false;
1431 }
1432
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001433 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001434 }
1435 break;
1436 default:
1437 return false;
1438 }
1439
1440 return true;
1441}
1442
1443bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1444{
1445 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1446 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1447 // to the fact that it is stored internally as a float, and so would require conversion
1448 // if returned from Context::getIntegerv. Since this conversion is already implemented
1449 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1450 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1451 // application.
1452 switch (pname)
1453 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001454 case GL_COMPRESSED_TEXTURE_FORMATS:
1455 {
1456 *type = GL_INT;
1457 *numParams = mNumCompressedTextureFormats;
1458 }
1459 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001460 case GL_SHADER_BINARY_FORMATS:
1461 {
1462 *type = GL_INT;
1463 *numParams = 0;
1464 }
1465 break;
1466 case GL_MAX_VERTEX_ATTRIBS:
1467 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1468 case GL_MAX_VARYING_VECTORS:
1469 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1470 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1471 case GL_MAX_TEXTURE_IMAGE_UNITS:
1472 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1473 case GL_MAX_RENDERBUFFER_SIZE:
1474 case GL_NUM_SHADER_BINARY_FORMATS:
1475 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1476 case GL_ARRAY_BUFFER_BINDING:
1477 case GL_FRAMEBUFFER_BINDING:
1478 case GL_RENDERBUFFER_BINDING:
1479 case GL_CURRENT_PROGRAM:
1480 case GL_PACK_ALIGNMENT:
1481 case GL_UNPACK_ALIGNMENT:
1482 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001483 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001484 case GL_RED_BITS:
1485 case GL_GREEN_BITS:
1486 case GL_BLUE_BITS:
1487 case GL_ALPHA_BITS:
1488 case GL_DEPTH_BITS:
1489 case GL_STENCIL_BITS:
1490 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1491 case GL_CULL_FACE_MODE:
1492 case GL_FRONT_FACE:
1493 case GL_ACTIVE_TEXTURE:
1494 case GL_STENCIL_FUNC:
1495 case GL_STENCIL_VALUE_MASK:
1496 case GL_STENCIL_REF:
1497 case GL_STENCIL_FAIL:
1498 case GL_STENCIL_PASS_DEPTH_FAIL:
1499 case GL_STENCIL_PASS_DEPTH_PASS:
1500 case GL_STENCIL_BACK_FUNC:
1501 case GL_STENCIL_BACK_VALUE_MASK:
1502 case GL_STENCIL_BACK_REF:
1503 case GL_STENCIL_BACK_FAIL:
1504 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1505 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1506 case GL_DEPTH_FUNC:
1507 case GL_BLEND_SRC_RGB:
1508 case GL_BLEND_SRC_ALPHA:
1509 case GL_BLEND_DST_RGB:
1510 case GL_BLEND_DST_ALPHA:
1511 case GL_BLEND_EQUATION_RGB:
1512 case GL_BLEND_EQUATION_ALPHA:
1513 case GL_STENCIL_WRITEMASK:
1514 case GL_STENCIL_BACK_WRITEMASK:
1515 case GL_STENCIL_CLEAR_VALUE:
1516 case GL_SUBPIXEL_BITS:
1517 case GL_MAX_TEXTURE_SIZE:
1518 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1519 case GL_SAMPLE_BUFFERS:
1520 case GL_SAMPLES:
1521 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1522 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1523 case GL_TEXTURE_BINDING_2D:
1524 case GL_TEXTURE_BINDING_CUBE_MAP:
1525 {
1526 *type = GL_INT;
1527 *numParams = 1;
1528 }
1529 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001530 case GL_MAX_SAMPLES_ANGLE:
1531 {
1532 if (getMaxSupportedSamples() != 0)
1533 {
1534 *type = GL_INT;
1535 *numParams = 1;
1536 }
1537 else
1538 {
1539 return false;
1540 }
1541 }
1542 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001543 case GL_MAX_VIEWPORT_DIMS:
1544 {
1545 *type = GL_INT;
1546 *numParams = 2;
1547 }
1548 break;
1549 case GL_VIEWPORT:
1550 case GL_SCISSOR_BOX:
1551 {
1552 *type = GL_INT;
1553 *numParams = 4;
1554 }
1555 break;
1556 case GL_SHADER_COMPILER:
1557 case GL_SAMPLE_COVERAGE_INVERT:
1558 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001559 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1560 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1561 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1562 case GL_SAMPLE_COVERAGE:
1563 case GL_SCISSOR_TEST:
1564 case GL_STENCIL_TEST:
1565 case GL_DEPTH_TEST:
1566 case GL_BLEND:
1567 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001568 {
1569 *type = GL_BOOL;
1570 *numParams = 1;
1571 }
1572 break;
1573 case GL_COLOR_WRITEMASK:
1574 {
1575 *type = GL_BOOL;
1576 *numParams = 4;
1577 }
1578 break;
1579 case GL_POLYGON_OFFSET_FACTOR:
1580 case GL_POLYGON_OFFSET_UNITS:
1581 case GL_SAMPLE_COVERAGE_VALUE:
1582 case GL_DEPTH_CLEAR_VALUE:
1583 case GL_LINE_WIDTH:
1584 {
1585 *type = GL_FLOAT;
1586 *numParams = 1;
1587 }
1588 break;
1589 case GL_ALIASED_LINE_WIDTH_RANGE:
1590 case GL_ALIASED_POINT_SIZE_RANGE:
1591 case GL_DEPTH_RANGE:
1592 {
1593 *type = GL_FLOAT;
1594 *numParams = 2;
1595 }
1596 break;
1597 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001598 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001599 {
1600 *type = GL_FLOAT;
1601 *numParams = 4;
1602 }
1603 break;
1604 default:
1605 return false;
1606 }
1607
1608 return true;
1609}
1610
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611// Applies the render target surface, depth stencil surface, viewport rectangle and
1612// scissor rectangle to the Direct3D 9 device
1613bool Context::applyRenderTarget(bool ignoreViewport)
1614{
1615 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001616
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001617 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618
1619 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1620 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001621 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001622 }
1623
1624 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001625
1626 if (!renderTarget)
1627 {
1628 return false; // Context must be lost
1629 }
1630
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001631 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001633 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001634 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1635 if (renderTargetSerial != mAppliedRenderTargetSerial)
1636 {
1637 device->SetRenderTarget(0, renderTarget);
1638 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001639 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 +00001640 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001641 }
1642
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001643 unsigned int depthbufferSerial = 0;
1644 unsigned int stencilbufferSerial = 0;
1645 if (framebufferObject->getDepthbufferType() != GL_NONE)
1646 {
1647 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001648 if (!depthStencil)
1649 {
1650 ERR("Depth stencil pointer unexpectedly null.");
1651 return false;
1652 }
1653
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001654 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1655 }
1656 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1657 {
1658 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001659 if (!depthStencil)
1660 {
1661 ERR("Depth stencil pointer unexpectedly null.");
1662 return false;
1663 }
1664
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001665 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1666 }
1667
1668 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001669 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001670 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001671 {
1672 device->SetDepthStencilSurface(depthStencil);
1673 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001674 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001675 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001676 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001678 if (!mRenderTargetDescInitialized || renderTargetChanged)
1679 {
1680 renderTarget->GetDesc(&mRenderTargetDesc);
1681 mRenderTargetDescInitialized = true;
1682 }
1683
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001686 float zNear = clamp01(mState.zNear);
1687 float zFar = clamp01(mState.zFar);
1688
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689 if (ignoreViewport)
1690 {
1691 viewport.X = 0;
1692 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001693 viewport.Width = mRenderTargetDesc.Width;
1694 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695 viewport.MinZ = 0.0f;
1696 viewport.MaxZ = 1.0f;
1697 }
1698 else
1699 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001700 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1701 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1702 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1703 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1704 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 +00001705 viewport.MinZ = zNear;
1706 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707 }
1708
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001709 if (viewport.Width <= 0 || viewport.Height <= 0)
1710 {
1711 return false; // Nothing to render
1712 }
1713
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001714 if (!mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
1715 {
1716 device->SetViewport(&viewport);
1717 mSetViewport = viewport;
1718 mViewportInitialized = true;
1719 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001720
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001721 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001723 if (mState.scissorTest)
1724 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001725 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1726 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1727 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1728 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1729 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001730 device->SetScissorRect(&rect);
1731 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1732 }
1733 else
1734 {
1735 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1736 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001737
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001738 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 }
1740
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001741 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 Program *programObject = getCurrentProgram();
1744
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001745 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001746 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001747 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001748
daniel@transgaming.com31754962010-11-28 02:02:52 +00001749 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001750 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1751 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1752 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001753 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001754
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001755 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001756 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001757 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001758
daniel@transgaming.com31754962010-11-28 02:02:52 +00001759 GLint depthRange = programObject->getDxDepthRangeLocation();
1760 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1761 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762 }
1763
1764 return true;
1765}
1766
1767// 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 +00001768void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769{
1770 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001771 Program *programObject = getCurrentProgram();
1772
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001773 Framebuffer *framebufferObject = getDrawFramebuffer();
1774
1775 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1776
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001777 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001778 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001779 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001780
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001781 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001782 GLint alwaysFront = !isTriangleMode(drawMode);
1783 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1784
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001785 egl::Display *display = getDisplay();
1786 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
1787 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1788 // Apparently some ATI cards have a bug where a draw with a zero color
1789 // write mask can cause later draws to have incorrect results. Instead,
1790 // set a nonzero color write mask but modify the blend state so that no
1791 // drawing is done.
1792 // http://code.google.com/p/angleproject/issues/detail?id=169
1793
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001794 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001796 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001797 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001798 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001799 }
1800 else
1801 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001802 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803 }
1804
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001805 mCullStateDirty = false;
1806 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001807
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001808 if (mDepthStateDirty)
1809 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001810 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001812 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1813 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 }
1815 else
1816 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001817 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001819
1820 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821 }
1822
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001823 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1824 {
1825 mBlendStateDirty = true;
1826 mMaskStateDirty = true;
1827 }
1828
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001829 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001831 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001832 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001833 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1834
1835 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1836 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1837 {
1838 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1839 }
1840 else
1841 {
1842 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1843 unorm<8>(mState.blendColor.alpha),
1844 unorm<8>(mState.blendColor.alpha),
1845 unorm<8>(mState.blendColor.alpha)));
1846 }
1847
1848 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1849 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1850 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1851
1852 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1853 mState.destBlendRGB != mState.destBlendAlpha ||
1854 mState.blendEquationRGB != mState.blendEquationAlpha)
1855 {
1856 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1857
1858 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1859 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1860 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001861 }
1862 else
1863 {
1864 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1865 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001866 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001867 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001868 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001869 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001870 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001871
1872 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873 }
1874
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001875 if (mStencilStateDirty || mFrontFaceDirty)
1876 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001877 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001878 {
1879 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1880 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1881
1882 // FIXME: Unsupported by D3D9
1883 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1884 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1885 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1886 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1887 mState.stencilRef != mState.stencilBackRef ||
1888 mState.stencilMask != mState.stencilBackMask)
1889 {
1890 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1891 return error(GL_INVALID_OPERATION);
1892 }
1893
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001894 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001895 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001896 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1897
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001898 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1899 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001900 es2dx::ConvertComparison(mState.stencilFunc));
1901
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001902 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1903 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001904
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001905 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001906 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001907 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001908 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001909 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001910 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1911
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001912 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1913 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001914 es2dx::ConvertComparison(mState.stencilBackFunc));
1915
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001916 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1917 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001918
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001919 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001920 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001921 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001922 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001923 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001924 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1925 }
1926 else
1927 {
1928 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1929 }
1930
1931 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001932 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001933 }
1934
1935 if (mMaskStateDirty)
1936 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001937 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1938 mState.colorMaskBlue, mState.colorMaskAlpha);
1939 if (colorMask == 0 && !zeroColorMaskAllowed)
1940 {
1941 // Enable green channel, but set blending so nothing will be drawn.
1942 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1943 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1944
1945 device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1946 device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1947 device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
1948 }
1949 else
1950 {
1951 device->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
1952 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001953 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1954
1955 mMaskStateDirty = false;
1956 }
1957
1958 if (mPolygonOffsetStateDirty)
1959 {
1960 if (mState.polygonOffsetFill)
1961 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001962 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001963 if (depthbuffer)
1964 {
1965 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1966 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1967 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1968 }
1969 }
1970 else
1971 {
1972 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1973 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1974 }
1975
1976 mPolygonOffsetStateDirty = false;
1977 }
1978
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001979 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001981 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001982 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001983 FIXME("Sample alpha to coverage is unimplemented.");
1984 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001985
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001986 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1987 if (mState.sampleCoverage)
1988 {
1989 unsigned int mask = 0;
1990 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001991 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001992 float threshold = 0.5f;
1993
1994 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001995 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001996 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001997
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001998 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001999 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002000 threshold += 1.0f;
2001 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002002 }
2003 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002004 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002005
2006 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002007 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002008 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002009 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002010
2011 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002012 }
2013 else
2014 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002015 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002016 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002017
2018 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019 }
2020
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002021 if (mDitherStateDirty)
2022 {
2023 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
2024
2025 mDitherStateDirty = false;
2026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027}
2028
daniel@transgaming.com83921382011-01-08 05:46:00 +00002029GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002030{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002031 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002032
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002033 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002034 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002035 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002036 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002037 }
2038
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00002039 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002040}
2041
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002042// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002043GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002044{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002045 IDirect3DDevice9 *device = getDevice();
2046 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002047
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002048 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002049 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002050 if (indexInfo->serial != mAppliedIBSerial)
2051 {
2052 device->SetIndices(indexInfo->indexBuffer);
2053 mAppliedIBSerial = indexInfo->serial;
2054 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002055 }
2056
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002057 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058}
2059
2060// Applies the shaders and shader constants to the Direct3D 9 device
2061void Context::applyShaders()
2062{
2063 IDirect3DDevice9 *device = getDevice();
2064 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002065 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002066 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002067 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2068 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2069
2070 device->SetPixelShader(pixelShader);
2071 device->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002072 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002073 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002074 }
2075
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076 programObject->applyUniforms();
2077}
2078
2079// Applies the textures and sampler states to the Direct3D 9 device
2080void Context::applyTextures()
2081{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002082 applyTextures(SAMPLER_PIXEL);
2083
2084 if (mSupportsVertexTexture)
2085 {
2086 applyTextures(SAMPLER_VERTEX);
2087 }
2088}
2089
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002090// For each Direct3D 9 sampler of either the pixel or vertex stage,
2091// looks up the corresponding OpenGL texture image unit and texture type,
2092// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002093void Context::applyTextures(SamplerType type)
2094{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095 IDirect3DDevice9 *device = getDevice();
2096 Program *programObject = getCurrentProgram();
2097
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002098 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 +00002099
2100 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002102 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002103 int d3dSampler = (type == SAMPLER_PIXEL) ? samplerIndex : D3DVERTEXTEXTURESAMPLER0 + samplerIndex;
2104 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2105
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002106 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002108 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002109
2110 Texture *texture = getSamplerTexture(textureUnit, textureType);
2111
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002112 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002113 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002114 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2115
2116 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002117 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002118 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002119 {
2120 GLenum wrapS = texture->getWrapS();
2121 GLenum wrapT = texture->getWrapT();
2122 GLenum minFilter = texture->getMinFilter();
2123 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002125 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2126 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002128 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002129 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2130 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002131 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2132 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002133 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002135 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002136 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002137 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002138 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002139 }
2140 else
2141 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002142 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002143 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002144
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002145 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002146 texture->resetDirty();
2147 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002149 else
2150 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002151 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002152 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002153 device->SetTexture(d3dSampler, NULL);
2154 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002155 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002156 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157 }
2158}
2159
2160void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2161{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002162 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002163
2164 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2165 {
2166 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2167 }
2168
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002169 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2170 {
2171 return error(GL_INVALID_OPERATION);
2172 }
2173
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002175
2176 if (!renderTarget)
2177 {
2178 return; // Context must be lost, return silently
2179 }
2180
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 IDirect3DDevice9 *device = getDevice();
2182
2183 D3DSURFACE_DESC desc;
2184 renderTarget->GetDesc(&desc);
2185
2186 IDirect3DSurface9 *systemSurface;
2187 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2188
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002189 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002191 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192 return error(GL_OUT_OF_MEMORY);
2193 }
2194
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002195 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2196 {
2197 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002198 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 }
2200
2201 result = device->GetRenderTargetData(renderTarget, systemSurface);
2202
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203 if (FAILED(result))
2204 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205 systemSurface->Release();
2206
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002207 switch (result)
2208 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002209 // It turns out that D3D will sometimes produce more error
2210 // codes than those documented.
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002211 case D3DERR_DRIVERINTERNALERROR:
2212 case D3DERR_DEVICELOST:
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002213 case D3DERR_DEVICEHUNG:
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002214 return error(GL_OUT_OF_MEMORY);
2215 default:
2216 UNREACHABLE();
2217 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002218 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002219 }
2220
2221 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002222 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2223 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2224 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2225 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2226 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002227
2228 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2229
2230 if (FAILED(result))
2231 {
2232 UNREACHABLE();
2233 systemSurface->Release();
2234
2235 return; // No sensible error to generate
2236 }
2237
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002238 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002239 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002240 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002241 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002242 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 for (int j = 0; j < rect.bottom - rect.top; j++)
2245 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002246 if (desc.Format == D3DFMT_A8R8G8B8 &&
2247 format == GL_BGRA_EXT &&
2248 type == GL_UNSIGNED_BYTE)
2249 {
2250 // Fast path for EXT_read_format_bgra, given
2251 // an RGBA source buffer. Note that buffers with no
2252 // alpha go through the slow path below.
2253 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002254 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002255 (rect.right - rect.left) * 4);
2256 continue;
2257 }
2258
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 for (int i = 0; i < rect.right - rect.left; i++)
2260 {
2261 float r;
2262 float g;
2263 float b;
2264 float a;
2265
2266 switch (desc.Format)
2267 {
2268 case D3DFMT_R5G6B5:
2269 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002270 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271
2272 a = 1.0f;
2273 b = (rgb & 0x001F) * (1.0f / 0x001F);
2274 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2275 r = (rgb & 0xF800) * (1.0f / 0xF800);
2276 }
2277 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 case D3DFMT_A1R5G5B5:
2279 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002280 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281
2282 a = (argb & 0x8000) ? 1.0f : 0.0f;
2283 b = (argb & 0x001F) * (1.0f / 0x001F);
2284 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2285 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2286 }
2287 break;
2288 case D3DFMT_A8R8G8B8:
2289 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002290 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291
2292 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2293 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2294 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2295 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2296 }
2297 break;
2298 case D3DFMT_X8R8G8B8:
2299 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002300 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
2302 a = 1.0f;
2303 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2304 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2305 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2306 }
2307 break;
2308 case D3DFMT_A2R10G10B10:
2309 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002310 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311
2312 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2313 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2314 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2315 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2316 }
2317 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002318 case D3DFMT_A32B32G32R32F:
2319 {
2320 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002321 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2322 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2323 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2324 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002325 }
2326 break;
2327 case D3DFMT_A16B16G16R16F:
2328 {
2329 // float formats in D3D are stored rgba, rather than the other way round
2330 float abgr[4];
2331
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002332 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002333
2334 a = abgr[3];
2335 b = abgr[2];
2336 g = abgr[1];
2337 r = abgr[0];
2338 }
2339 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002340 default:
2341 UNIMPLEMENTED(); // FIXME
2342 UNREACHABLE();
2343 }
2344
2345 switch (format)
2346 {
2347 case GL_RGBA:
2348 switch (type)
2349 {
2350 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002351 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2352 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2353 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2354 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 break;
2356 default: UNREACHABLE();
2357 }
2358 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002359 case GL_BGRA_EXT:
2360 switch (type)
2361 {
2362 case GL_UNSIGNED_BYTE:
2363 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2364 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2365 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2366 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2367 break;
2368 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2369 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2370 // this type is packed as follows:
2371 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2372 // --------------------------------------------------------------------------------
2373 // | 4th | 3rd | 2nd | 1st component |
2374 // --------------------------------------------------------------------------------
2375 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2376 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2377 ((unsigned short)(15 * a + 0.5f) << 12)|
2378 ((unsigned short)(15 * r + 0.5f) << 8) |
2379 ((unsigned short)(15 * g + 0.5f) << 4) |
2380 ((unsigned short)(15 * b + 0.5f) << 0);
2381 break;
2382 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2383 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2384 // this type is packed as follows:
2385 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2386 // --------------------------------------------------------------------------------
2387 // | 4th | 3rd | 2nd | 1st component |
2388 // --------------------------------------------------------------------------------
2389 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2390 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2391 ((unsigned short)( a + 0.5f) << 15) |
2392 ((unsigned short)(31 * r + 0.5f) << 10) |
2393 ((unsigned short)(31 * g + 0.5f) << 5) |
2394 ((unsigned short)(31 * b + 0.5f) << 0);
2395 break;
2396 default: UNREACHABLE();
2397 }
2398 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002399 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 switch (type)
2401 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002402 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002403 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2404 ((unsigned short)(31 * b + 0.5f) << 0) |
2405 ((unsigned short)(63 * g + 0.5f) << 5) |
2406 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407 break;
2408 default: UNREACHABLE();
2409 }
2410 break;
2411 default: UNREACHABLE();
2412 }
2413 }
2414 }
2415
2416 systemSurface->UnlockRect();
2417
2418 systemSurface->Release();
2419}
2420
2421void Context::clear(GLbitfield mask)
2422{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002423 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002424
2425 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2426 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002427 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002428 }
2429
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002430 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 IDirect3DDevice9 *device = getDevice();
2432 DWORD flags = 0;
2433
2434 if (mask & GL_COLOR_BUFFER_BIT)
2435 {
2436 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002437
2438 if (framebufferObject->getColorbufferType() != GL_NONE)
2439 {
2440 flags |= D3DCLEAR_TARGET;
2441 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442 }
2443
2444 if (mask & GL_DEPTH_BUFFER_BIT)
2445 {
2446 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002447 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448 {
2449 flags |= D3DCLEAR_ZBUFFER;
2450 }
2451 }
2452
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453 GLuint stencilUnmasked = 0x0;
2454
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002455 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002457 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002458 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002459 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002460 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002461 if (!depthStencil)
2462 {
2463 ERR("Depth stencil pointer unexpectedly null.");
2464 return;
2465 }
2466
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002467 D3DSURFACE_DESC desc;
2468 depthStencil->GetDesc(&desc);
2469
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002470 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002471 stencilUnmasked = (0x1 << stencilSize) - 1;
2472
2473 if (stencilUnmasked != 0x0)
2474 {
2475 flags |= D3DCLEAR_STENCIL;
2476 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477 }
2478 }
2479
2480 if (mask != 0)
2481 {
2482 return error(GL_INVALID_VALUE);
2483 }
2484
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002485 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2486 {
2487 return;
2488 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002490 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002491 unorm<8>(mState.colorClearValue.red),
2492 unorm<8>(mState.colorClearValue.green),
2493 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002494 float depth = clamp01(mState.depthClearValue);
2495 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496
2497 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2498
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002499 if (!renderTarget)
2500 {
2501 return; // Context must be lost, return silently
2502 }
2503
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504 D3DSURFACE_DESC desc;
2505 renderTarget->GetDesc(&desc);
2506
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002507 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508
2509 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002510 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002511 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002512 !(mState.colorMaskRed && mState.colorMaskGreen &&
2513 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514
2515 if (needMaskedColorClear || needMaskedStencilClear)
2516 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002517 // State which is altered in all paths from this point to the clear call is saved.
2518 // State which is altered in only some paths will be flagged dirty in the case that
2519 // that path is taken.
2520 HRESULT hr;
2521 if (mMaskedClearSavedState == NULL)
2522 {
2523 hr = device->BeginStateBlock();
2524 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2525
2526 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2527 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2528 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2529 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2530 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2531 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2532 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2533 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2534 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2535 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2536 device->SetPixelShader(NULL);
2537 device->SetVertexShader(NULL);
2538 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
jbauman@chromium.org23c9e312011-09-21 22:16:44 +00002539 device->SetStreamSource(0, NULL, 0, 0);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002540 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2541 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2542 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2543 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2544 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2545 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2546 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002547
2548 hr = device->EndStateBlock(&mMaskedClearSavedState);
2549 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2550 }
2551
2552 ASSERT(mMaskedClearSavedState != NULL);
2553
2554 if (mMaskedClearSavedState != NULL)
2555 {
2556 hr = mMaskedClearSavedState->Capture();
2557 ASSERT(SUCCEEDED(hr));
2558 }
2559
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002560 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2561 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2562 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2563 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2564 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2565 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2566 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2567 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2568
2569 if (flags & D3DCLEAR_TARGET)
2570 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002571 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002572 }
2573 else
2574 {
2575 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2576 }
2577
2578 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2579 {
2580 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2581 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2582 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2583 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002584 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002585 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002586 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2587 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002588 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589 }
2590 else
2591 {
2592 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2593 }
2594
2595 device->SetPixelShader(NULL);
2596 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002597 device->SetFVF(D3DFVF_XYZRHW);
2598 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2599 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2600 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2601 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2602 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2603 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2604 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002605
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002606 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2607 quad[0][0] = -0.5f;
2608 quad[0][1] = desc.Height - 0.5f;
2609 quad[0][2] = 0.0f;
2610 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002612 quad[1][0] = desc.Width - 0.5f;
2613 quad[1][1] = desc.Height - 0.5f;
2614 quad[1][2] = 0.0f;
2615 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002617 quad[2][0] = -0.5f;
2618 quad[2][1] = -0.5f;
2619 quad[2][2] = 0.0f;
2620 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002622 quad[3][0] = desc.Width - 0.5f;
2623 quad[3][1] = -0.5f;
2624 quad[3][2] = 0.0f;
2625 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002627 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002628 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629
2630 if (flags & D3DCLEAR_ZBUFFER)
2631 {
2632 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2633 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2634 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2635 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002636
2637 if (mMaskedClearSavedState != NULL)
2638 {
2639 mMaskedClearSavedState->Apply();
2640 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002642 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002643 {
2644 device->Clear(0, NULL, flags, color, depth, stencil);
2645 }
2646}
2647
2648void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2649{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002650 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651 {
2652 return error(GL_INVALID_OPERATION);
2653 }
2654
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002655 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656 IDirect3DDevice9 *device = getDevice();
2657 D3DPRIMITIVETYPE primitiveType;
2658 int primitiveCount;
2659
2660 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2661 return error(GL_INVALID_ENUM);
2662
2663 if (primitiveCount <= 0)
2664 {
2665 return;
2666 }
2667
2668 if (!applyRenderTarget(false))
2669 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002670 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671 }
2672
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002673 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002674
daniel@transgaming.com83921382011-01-08 05:46:00 +00002675 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002676 if (err != GL_NO_ERROR)
2677 {
2678 return error(err);
2679 }
2680
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 applyShaders();
2682 applyTextures();
2683
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002684 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002685 {
2686 return error(GL_INVALID_OPERATION);
2687 }
2688
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002689 if (!cullSkipsDraw(mode))
2690 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002691 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002692
2693 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002694
2695 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2696 {
2697 drawClosingLine(first, first + count - 1);
2698 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002699 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700}
2701
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002702void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002704 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 {
2706 return error(GL_INVALID_OPERATION);
2707 }
2708
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002709 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710 {
2711 return error(GL_INVALID_OPERATION);
2712 }
2713
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002714 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715 IDirect3DDevice9 *device = getDevice();
2716 D3DPRIMITIVETYPE primitiveType;
2717 int primitiveCount;
2718
2719 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2720 return error(GL_INVALID_ENUM);
2721
2722 if (primitiveCount <= 0)
2723 {
2724 return;
2725 }
2726
2727 if (!applyRenderTarget(false))
2728 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002729 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730 }
2731
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002732 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002733
2734 TranslatedIndexData indexInfo;
2735 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2736 if (err != GL_NO_ERROR)
2737 {
2738 return error(err);
2739 }
2740
daniel@transgaming.com83921382011-01-08 05:46:00 +00002741 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2742 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002743 if (err != GL_NO_ERROR)
2744 {
2745 return error(err);
2746 }
2747
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 applyShaders();
2749 applyTextures();
2750
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002751 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002752 {
2753 return error(GL_INVALID_OPERATION);
2754 }
2755
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002756 if (!cullSkipsDraw(mode))
2757 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002758 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002759
daniel@transgaming.com83921382011-01-08 05:46:00 +00002760 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002761
2762 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2763 {
2764 drawClosingLine(count, type, indices);
2765 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002766 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002767}
2768
2769void Context::finish()
2770{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002771 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002772 IDirect3DDevice9 *device = getDevice();
2773 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002774 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002776 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2777 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002779 ERR("CreateQuery failed hr=%x\n", result);
2780 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2781 {
2782 return error(GL_OUT_OF_MEMORY);
2783 }
2784 ASSERT(false);
2785 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786 }
2787
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002788 IDirect3DStateBlock9 *savedState = NULL;
2789 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2790 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002791 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002792 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002793 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002794
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002795 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002796 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002797 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002798 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002799 ASSERT(false);
2800 return;
2801 }
2802
2803 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2804 if (FAILED(result))
2805 {
2806 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2807 occlusionQuery->Release();
2808 savedState->Release();
2809 ASSERT(false);
2810 return;
2811 }
2812
2813 // Render something outside the render target
2814 device->SetPixelShader(NULL);
2815 device->SetVertexShader(NULL);
2816 device->SetFVF(D3DFVF_XYZRHW);
2817 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2818 display->startScene();
2819 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2820
2821 result = occlusionQuery->Issue(D3DISSUE_END);
2822 if (FAILED(result))
2823 {
2824 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2825 occlusionQuery->Release();
2826 savedState->Apply();
2827 savedState->Release();
2828 ASSERT(false);
2829 return;
2830 }
2831
2832 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2833 {
2834 // Keep polling, but allow other threads to do something useful first
2835 Sleep(0);
2836 }
2837
2838 occlusionQuery->Release();
2839 savedState->Apply();
2840 savedState->Release();
2841
2842 if (result == D3DERR_DEVICELOST)
2843 {
2844 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002845 }
2846}
2847
2848void Context::flush()
2849{
2850 IDirect3DDevice9 *device = getDevice();
2851 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002852 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002853
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002854 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2855 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002856 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002857 ERR("CreateQuery failed hr=%x\n", result);
2858 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2859 {
2860 return error(GL_OUT_OF_MEMORY);
2861 }
2862 ASSERT(false);
2863 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002864 }
2865
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002866 result = eventQuery->Issue(D3DISSUE_END);
2867 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002868 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002869 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2870 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002872 return;
2873 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002874
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002875 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2876 eventQuery->Release();
2877
2878 if (result == D3DERR_DEVICELOST)
2879 {
2880 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002881 }
2882}
2883
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002884void Context::drawClosingLine(unsigned int first, unsigned int last)
2885{
2886 IDirect3DDevice9 *device = getDevice();
2887 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002888 bool succeeded = false;
2889 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002890
2891 if (supports32bitIndices())
2892 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002893 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002894
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002895 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002896 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002897 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2898 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002899
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002900 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2901
2902 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2903 if (data)
2904 {
2905 data[0] = last;
2906 data[1] = first;
2907 mClosingIB->unmap();
2908 offset /= 4;
2909 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002910 }
2911 }
2912 else
2913 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002914 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002915
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002916 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002917 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002918 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2919 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002920
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002921 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2922
2923 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2924 if (data)
2925 {
2926 data[0] = last;
2927 data[1] = first;
2928 mClosingIB->unmap();
2929 offset /= 2;
2930 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002931 }
2932 }
2933
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002934 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002935 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002936 device->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002937 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002938
jbauman@chromium.org2c199b12011-06-13 22:20:06 +00002939 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002940 }
2941 else
2942 {
2943 ERR("Could not create an index buffer for closing a line loop.");
2944 error(GL_OUT_OF_MEMORY);
2945 }
2946}
2947
2948void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2949{
2950 unsigned int first = 0;
2951 unsigned int last = 0;
2952
2953 if (mState.elementArrayBuffer.get())
2954 {
2955 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2956 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2957 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2958 }
2959
2960 switch (type)
2961 {
2962 case GL_UNSIGNED_BYTE:
2963 first = static_cast<const GLubyte*>(indices)[0];
2964 last = static_cast<const GLubyte*>(indices)[count - 1];
2965 break;
2966 case GL_UNSIGNED_SHORT:
2967 first = static_cast<const GLushort*>(indices)[0];
2968 last = static_cast<const GLushort*>(indices)[count - 1];
2969 break;
2970 case GL_UNSIGNED_INT:
2971 first = static_cast<const GLuint*>(indices)[0];
2972 last = static_cast<const GLuint*>(indices)[count - 1];
2973 break;
2974 default: UNREACHABLE();
2975 }
2976
2977 drawClosingLine(first, last);
2978}
2979
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002980void Context::recordInvalidEnum()
2981{
2982 mInvalidEnum = true;
2983}
2984
2985void Context::recordInvalidValue()
2986{
2987 mInvalidValue = true;
2988}
2989
2990void Context::recordInvalidOperation()
2991{
2992 mInvalidOperation = true;
2993}
2994
2995void Context::recordOutOfMemory()
2996{
2997 mOutOfMemory = true;
2998}
2999
3000void Context::recordInvalidFramebufferOperation()
3001{
3002 mInvalidFramebufferOperation = true;
3003}
3004
3005// Get one of the recorded errors and clear its flag, if any.
3006// [OpenGL ES 2.0.24] section 2.5 page 13.
3007GLenum Context::getError()
3008{
3009 if (mInvalidEnum)
3010 {
3011 mInvalidEnum = false;
3012
3013 return GL_INVALID_ENUM;
3014 }
3015
3016 if (mInvalidValue)
3017 {
3018 mInvalidValue = false;
3019
3020 return GL_INVALID_VALUE;
3021 }
3022
3023 if (mInvalidOperation)
3024 {
3025 mInvalidOperation = false;
3026
3027 return GL_INVALID_OPERATION;
3028 }
3029
3030 if (mOutOfMemory)
3031 {
3032 mOutOfMemory = false;
3033
3034 return GL_OUT_OF_MEMORY;
3035 }
3036
3037 if (mInvalidFramebufferOperation)
3038 {
3039 mInvalidFramebufferOperation = false;
3040
3041 return GL_INVALID_FRAMEBUFFER_OPERATION;
3042 }
3043
3044 return GL_NO_ERROR;
3045}
3046
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003047bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003048{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003049 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003050}
3051
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003052int Context::getMaximumVaryingVectors() const
3053{
3054 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3055}
3056
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003057unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003058{
3059 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3060}
3061
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003062unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003063{
3064 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3065}
3066
daniel@transgaming.com458da142010-11-28 02:03:02 +00003067int Context::getMaximumFragmentUniformVectors() const
3068{
3069 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3070}
3071
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003072int Context::getMaxSupportedSamples() const
3073{
3074 return mMaxSupportedSamples;
3075}
3076
3077int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3078{
3079 if (requested == 0)
3080 {
3081 return requested;
3082 }
3083
3084 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3085 if (itr == mMultiSampleSupport.end())
3086 {
3087 return -1;
3088 }
3089
3090 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3091 {
3092 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3093 {
3094 return i;
3095 }
3096 }
3097
3098 return -1;
3099}
3100
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003101bool Context::supportsEventQueries() const
3102{
3103 return mSupportsEventQueries;
3104}
3105
gman@chromium.org50c526d2011-08-10 05:19:44 +00003106bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003107{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003108 return mSupportsDXT1Textures;
3109}
3110
3111bool Context::supportsDXT3Textures() const
3112{
3113 return mSupportsDXT3Textures;
3114}
3115
3116bool Context::supportsDXT5Textures() const
3117{
3118 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003119}
3120
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003121bool Context::supportsFloatTextures() const
3122{
3123 return mSupportsFloatTextures;
3124}
3125
3126bool Context::supportsFloatLinearFilter() const
3127{
3128 return mSupportsFloatLinearFilter;
3129}
3130
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003131bool Context::supportsFloatRenderableTextures() const
3132{
3133 return mSupportsFloatRenderableTextures;
3134}
3135
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003136bool Context::supportsHalfFloatTextures() const
3137{
3138 return mSupportsHalfFloatTextures;
3139}
3140
3141bool Context::supportsHalfFloatLinearFilter() const
3142{
3143 return mSupportsHalfFloatLinearFilter;
3144}
3145
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003146bool Context::supportsHalfFloatRenderableTextures() const
3147{
3148 return mSupportsHalfFloatRenderableTextures;
3149}
3150
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003151int Context::getMaximumRenderbufferDimension() const
3152{
3153 return mMaxRenderbufferDimension;
3154}
3155
3156int Context::getMaximumTextureDimension() const
3157{
3158 return mMaxTextureDimension;
3159}
3160
3161int Context::getMaximumCubeTextureDimension() const
3162{
3163 return mMaxCubeTextureDimension;
3164}
3165
3166int Context::getMaximumTextureLevel() const
3167{
3168 return mMaxTextureLevel;
3169}
3170
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003171bool Context::supportsLuminanceTextures() const
3172{
3173 return mSupportsLuminanceTextures;
3174}
3175
3176bool Context::supportsLuminanceAlphaTextures() const
3177{
3178 return mSupportsLuminanceAlphaTextures;
3179}
3180
daniel@transgaming.com83921382011-01-08 05:46:00 +00003181bool Context::supports32bitIndices() const
3182{
3183 return mSupports32bitIndices;
3184}
3185
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003186bool Context::supportsNonPower2Texture() const
3187{
3188 return mSupportsNonPower2Texture;
3189}
3190
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003191void Context::detachBuffer(GLuint buffer)
3192{
3193 // [OpenGL ES 2.0.24] section 2.9 page 22:
3194 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3195 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3196
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003197 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003198 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003199 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003200 }
3201
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003202 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003203 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003204 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003205 }
3206
3207 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3208 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003209 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003210 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003211 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003212 }
3213 }
3214}
3215
3216void Context::detachTexture(GLuint texture)
3217{
3218 // [OpenGL ES 2.0.24] section 3.8 page 84:
3219 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3220 // rebound to texture object zero
3221
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003222 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003223 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003224 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003225 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003226 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003227 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003228 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003229 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003230 }
3231 }
3232
3233 // [OpenGL ES 2.0.24] section 4.4 page 112:
3234 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3235 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3236 // image was attached in the currently bound framebuffer.
3237
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003238 Framebuffer *readFramebuffer = getReadFramebuffer();
3239 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003240
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003241 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003242 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003243 readFramebuffer->detachTexture(texture);
3244 }
3245
3246 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3247 {
3248 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003249 }
3250}
3251
3252void Context::detachFramebuffer(GLuint framebuffer)
3253{
3254 // [OpenGL ES 2.0.24] section 4.4 page 107:
3255 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3256 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3257
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003258 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003259 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003260 bindReadFramebuffer(0);
3261 }
3262
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003263 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003264 {
3265 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003266 }
3267}
3268
3269void Context::detachRenderbuffer(GLuint renderbuffer)
3270{
3271 // [OpenGL ES 2.0.24] section 4.4 page 109:
3272 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3273 // had been executed with the target RENDERBUFFER and name of zero.
3274
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003275 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003276 {
3277 bindRenderbuffer(0);
3278 }
3279
3280 // [OpenGL ES 2.0.24] section 4.4 page 111:
3281 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3282 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3283 // point to which this image was attached in the currently bound framebuffer.
3284
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003285 Framebuffer *readFramebuffer = getReadFramebuffer();
3286 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003287
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003288 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003289 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003290 readFramebuffer->detachRenderbuffer(renderbuffer);
3291 }
3292
3293 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3294 {
3295 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003296 }
3297}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003298
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003299Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003300{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003301 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003302
3303 if (t == NULL)
3304 {
3305 static const GLubyte color[] = { 0, 0, 0, 255 };
3306
3307 switch (type)
3308 {
3309 default:
3310 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003311 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003312
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003313 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003314 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003315 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003316 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003317 t = incomplete2d;
3318 }
3319 break;
3320
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003321 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003322 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003323 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003324
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003325 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3326 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3327 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3328 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3329 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3330 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003331
3332 t = incompleteCube;
3333 }
3334 break;
3335 }
3336
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003337 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003338 }
3339
3340 return t;
3341}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003342
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003343bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003344{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003345 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003346}
3347
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003348bool Context::isTriangleMode(GLenum drawMode)
3349{
3350 switch (drawMode)
3351 {
3352 case GL_TRIANGLES:
3353 case GL_TRIANGLE_FAN:
3354 case GL_TRIANGLE_STRIP:
3355 return true;
3356 case GL_POINTS:
3357 case GL_LINES:
3358 case GL_LINE_LOOP:
3359 case GL_LINE_STRIP:
3360 return false;
3361 default: UNREACHABLE();
3362 }
3363
3364 return false;
3365}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003366
3367void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3368{
3369 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3370
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003371 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3372 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3373 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3374 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003375
daniel@transgaming.com83921382011-01-08 05:46:00 +00003376 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003377}
3378
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003379// keep list sorted in following order
3380// OES extensions
3381// EXT extensions
3382// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003383void Context::initExtensionString()
3384{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003385 mExtensionString = "";
3386
3387 // OES extensions
3388 if (supports32bitIndices())
3389 {
3390 mExtensionString += "GL_OES_element_index_uint ";
3391 }
3392
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003393 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003394 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003395 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003396
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003397 if (supportsHalfFloatTextures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003398 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003399 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003400 }
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003401 if (supportsHalfFloatLinearFilter())
3402 {
3403 mExtensionString += "GL_OES_texture_half_float_linear ";
3404 }
3405 if (supportsFloatTextures())
3406 {
3407 mExtensionString += "GL_OES_texture_float ";
3408 }
3409 if (supportsFloatLinearFilter())
3410 {
3411 mExtensionString += "GL_OES_texture_float_linear ";
3412 }
3413
3414 if (supportsNonPower2Texture())
3415 {
3416 mExtensionString += "GL_OES_texture_npot ";
3417 }
3418
3419 // Multi-vendor (EXT) extensions
3420 mExtensionString += "GL_EXT_read_format_bgra ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003421
gman@chromium.org50c526d2011-08-10 05:19:44 +00003422 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003423 {
3424 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3425 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003426
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003427 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003428
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003429 // ANGLE-specific extensions
3430 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003431 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003432 {
3433 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3434 }
3435
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003436 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003437 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003438 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3439 }
3440 if (supportsDXT5Textures())
3441 {
3442 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003443 }
zmo@google.coma574f782011-10-03 21:45:23 +00003444 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003445
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003446 // Other vendor-specific extensions
3447 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003448 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003449 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003450 }
3451
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003452 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3453 if (end != std::string::npos)
3454 {
3455 mExtensionString.resize(end+1);
3456 }
3457}
3458
3459const char *Context::getExtensionString() const
3460{
3461 return mExtensionString.c_str();
3462}
3463
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003464void Context::initRendererString()
3465{
3466 egl::Display *display = getDisplay();
3467 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
3468
3469 mRendererString = "ANGLE (";
3470 mRendererString += identifier->Description;
3471 mRendererString += ")";
3472}
3473
3474const char *Context::getRendererString() const
3475{
3476 return mRendererString.c_str();
3477}
3478
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003479void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3480 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3481 GLbitfield mask)
3482{
3483 IDirect3DDevice9 *device = getDevice();
3484
3485 Framebuffer *readFramebuffer = getReadFramebuffer();
3486 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3487
3488 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3489 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3490 {
3491 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3492 }
3493
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003494 if (drawFramebuffer->getSamples() != 0)
3495 {
3496 return error(GL_INVALID_OPERATION);
3497 }
3498
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003499 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3500 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3501 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3502 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3503
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003504 RECT sourceRect;
3505 RECT destRect;
3506
3507 if (srcX0 < srcX1)
3508 {
3509 sourceRect.left = srcX0;
3510 sourceRect.right = srcX1;
3511 destRect.left = dstX0;
3512 destRect.right = dstX1;
3513 }
3514 else
3515 {
3516 sourceRect.left = srcX1;
3517 destRect.left = dstX1;
3518 sourceRect.right = srcX0;
3519 destRect.right = dstX0;
3520 }
3521
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003522 if (srcY0 < srcY1)
3523 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003524 sourceRect.top = readBufferHeight - srcY1;
3525 destRect.top = drawBufferHeight - dstY1;
3526 sourceRect.bottom = readBufferHeight - srcY0;
3527 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003528 }
3529 else
3530 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003531 sourceRect.top = readBufferHeight - srcY0;
3532 destRect.top = drawBufferHeight - dstY0;
3533 sourceRect.bottom = readBufferHeight - srcY1;
3534 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003535 }
3536
3537 RECT sourceScissoredRect = sourceRect;
3538 RECT destScissoredRect = destRect;
3539
3540 if (mState.scissorTest)
3541 {
3542 // Only write to parts of the destination framebuffer which pass the scissor test
3543 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3544 // rect will be checked against scissorY, rather than the bottom.
3545 if (destRect.left < mState.scissorX)
3546 {
3547 int xDiff = mState.scissorX - destRect.left;
3548 destScissoredRect.left = mState.scissorX;
3549 sourceScissoredRect.left += xDiff;
3550 }
3551
3552 if (destRect.right > mState.scissorX + mState.scissorWidth)
3553 {
3554 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3555 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3556 sourceScissoredRect.right -= xDiff;
3557 }
3558
3559 if (destRect.top < mState.scissorY)
3560 {
3561 int yDiff = mState.scissorY - destRect.top;
3562 destScissoredRect.top = mState.scissorY;
3563 sourceScissoredRect.top += yDiff;
3564 }
3565
3566 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3567 {
3568 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3569 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3570 sourceScissoredRect.bottom -= yDiff;
3571 }
3572 }
3573
3574 bool blitRenderTarget = false;
3575 bool blitDepthStencil = false;
3576
3577 RECT sourceTrimmedRect = sourceScissoredRect;
3578 RECT destTrimmedRect = destScissoredRect;
3579
3580 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3581 // the actual draw and read surfaces.
3582 if (sourceTrimmedRect.left < 0)
3583 {
3584 int xDiff = 0 - sourceTrimmedRect.left;
3585 sourceTrimmedRect.left = 0;
3586 destTrimmedRect.left += xDiff;
3587 }
3588
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003589 if (sourceTrimmedRect.right > readBufferWidth)
3590 {
3591 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3592 sourceTrimmedRect.right = readBufferWidth;
3593 destTrimmedRect.right -= xDiff;
3594 }
3595
3596 if (sourceTrimmedRect.top < 0)
3597 {
3598 int yDiff = 0 - sourceTrimmedRect.top;
3599 sourceTrimmedRect.top = 0;
3600 destTrimmedRect.top += yDiff;
3601 }
3602
3603 if (sourceTrimmedRect.bottom > readBufferHeight)
3604 {
3605 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3606 sourceTrimmedRect.bottom = readBufferHeight;
3607 destTrimmedRect.bottom -= yDiff;
3608 }
3609
3610 if (destTrimmedRect.left < 0)
3611 {
3612 int xDiff = 0 - destTrimmedRect.left;
3613 destTrimmedRect.left = 0;
3614 sourceTrimmedRect.left += xDiff;
3615 }
3616
3617 if (destTrimmedRect.right > drawBufferWidth)
3618 {
3619 int xDiff = destTrimmedRect.right - drawBufferWidth;
3620 destTrimmedRect.right = drawBufferWidth;
3621 sourceTrimmedRect.right -= xDiff;
3622 }
3623
3624 if (destTrimmedRect.top < 0)
3625 {
3626 int yDiff = 0 - destTrimmedRect.top;
3627 destTrimmedRect.top = 0;
3628 sourceTrimmedRect.top += yDiff;
3629 }
3630
3631 if (destTrimmedRect.bottom > drawBufferHeight)
3632 {
3633 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3634 destTrimmedRect.bottom = drawBufferHeight;
3635 sourceTrimmedRect.bottom -= yDiff;
3636 }
3637
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003638 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003639 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3640 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3641 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3642 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003643 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3644 {
3645 partialBufferCopy = true;
3646 }
3647
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003648 if (mask & GL_COLOR_BUFFER_BIT)
3649 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003650 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3651 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3652 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3653 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3654 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003655 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3656 {
3657 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3658 return error(GL_INVALID_OPERATION);
3659 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003660
3661 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3662 {
3663 return error(GL_INVALID_OPERATION);
3664 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003665
3666 blitRenderTarget = true;
3667
3668 }
3669
3670 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3671 {
3672 DepthStencilbuffer *readDSBuffer = NULL;
3673 DepthStencilbuffer *drawDSBuffer = NULL;
3674
3675 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3676 // both a depth and stencil buffer, it will be the same buffer.
3677
3678 if (mask & GL_DEPTH_BUFFER_BIT)
3679 {
3680 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3681 {
3682 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3683 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3684 {
3685 return error(GL_INVALID_OPERATION);
3686 }
3687
3688 blitDepthStencil = true;
3689 readDSBuffer = readFramebuffer->getDepthbuffer();
3690 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3691 }
3692 }
3693
3694 if (mask & GL_STENCIL_BUFFER_BIT)
3695 {
3696 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3697 {
3698 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3699 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3700 {
3701 return error(GL_INVALID_OPERATION);
3702 }
3703
3704 blitDepthStencil = true;
3705 readDSBuffer = readFramebuffer->getStencilbuffer();
3706 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3707 }
3708 }
3709
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003710 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003711 {
3712 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3713 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3714 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003715
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003716 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3717 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003718 {
3719 return error(GL_INVALID_OPERATION);
3720 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003721 }
3722
3723 if (blitRenderTarget || blitDepthStencil)
3724 {
3725 egl::Display *display = getDisplay();
3726 display->endScene();
3727
3728 if (blitRenderTarget)
3729 {
3730 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3731 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3732
3733 if (FAILED(result))
3734 {
3735 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3736 return;
3737 }
3738 }
3739
3740 if (blitDepthStencil)
3741 {
3742 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3743
3744 if (FAILED(result))
3745 {
3746 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3747 return;
3748 }
3749 }
3750 }
3751}
3752
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003753VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3754{
3755 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3756 {
3757 mVertexDeclCache[i].vertexDeclaration = NULL;
3758 mVertexDeclCache[i].lruCount = 0;
3759 }
3760}
3761
3762VertexDeclarationCache::~VertexDeclarationCache()
3763{
3764 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3765 {
3766 if (mVertexDeclCache[i].vertexDeclaration)
3767 {
3768 mVertexDeclCache[i].vertexDeclaration->Release();
3769 }
3770 }
3771}
3772
3773GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3774{
3775 IDirect3DDevice9 *device = getDevice();
3776
3777 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3778 D3DVERTEXELEMENT9 *element = &elements[0];
3779
3780 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3781 {
3782 if (attributes[i].active)
3783 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003784 if (mAppliedVBs[i].serial != attributes[i].serial ||
3785 mAppliedVBs[i].stride != attributes[i].stride ||
3786 mAppliedVBs[i].offset != attributes[i].offset)
3787 {
3788 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3789 mAppliedVBs[i].serial = attributes[i].serial;
3790 mAppliedVBs[i].stride = attributes[i].stride;
3791 mAppliedVBs[i].offset = attributes[i].offset;
3792 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003793
3794 element->Stream = i;
3795 element->Offset = 0;
3796 element->Type = attributes[i].type;
3797 element->Method = D3DDECLMETHOD_DEFAULT;
3798 element->Usage = D3DDECLUSAGE_TEXCOORD;
3799 element->UsageIndex = program->getSemanticIndex(i);
3800 element++;
3801 }
3802 }
3803
3804 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3805 *(element++) = end;
3806
3807 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3808 {
3809 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3810 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3811 {
3812 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003813 if(entry->vertexDeclaration != mLastSetVDecl)
3814 {
3815 device->SetVertexDeclaration(entry->vertexDeclaration);
3816 mLastSetVDecl = entry->vertexDeclaration;
3817 }
3818
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003819 return GL_NO_ERROR;
3820 }
3821 }
3822
3823 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3824
3825 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3826 {
3827 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3828 {
3829 lastCache = &mVertexDeclCache[i];
3830 }
3831 }
3832
3833 if (lastCache->vertexDeclaration != NULL)
3834 {
3835 lastCache->vertexDeclaration->Release();
3836 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003837 // mLastSetVDecl is set to the replacement, so we don't have to worry
3838 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003839 }
3840
3841 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3842 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3843 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003844 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003845 lastCache->lruCount = ++mMaxLru;
3846
3847 return GL_NO_ERROR;
3848}
3849
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003850void VertexDeclarationCache::markStateDirty()
3851{
3852 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3853 {
3854 mAppliedVBs[i].serial = 0;
3855 }
3856
3857 mLastSetVDecl = NULL;
3858}
3859
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003860}
3861
3862extern "C"
3863{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003864gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003865{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003866 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003867}
3868
3869void glDestroyContext(gl::Context *context)
3870{
3871 delete context;
3872
3873 if (context == gl::getContext())
3874 {
3875 gl::makeCurrent(NULL, NULL, NULL);
3876 }
3877}
3878
3879void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3880{
3881 gl::makeCurrent(context, display, surface);
3882}
3883
3884gl::Context *glGetCurrentContext()
3885{
3886 return gl::getContext();
3887}
3888}