blob: 1cb738abe55a939c071904a9acb9e926bde49587 [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{
daniel@transgaming.comc941e252011-10-26 02:32:31 +000043 mDisplay = NULL;
44 mDevice = NULL;
45
benvanik@google.com1a233342011-04-28 19:44:39 +000046 mFenceHandleAllocator.setBaseHandle(0);
47
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000049
daniel@transgaming.com428d1582010-05-04 03:35:25 +000050 mState.depthClearValue = 1.0f;
51 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052
daniel@transgaming.com428d1582010-05-04 03:35:25 +000053 mState.cullFace = false;
54 mState.cullMode = GL_BACK;
55 mState.frontFace = GL_CCW;
56 mState.depthTest = false;
57 mState.depthFunc = GL_LESS;
58 mState.blend = false;
59 mState.sourceBlendRGB = GL_ONE;
60 mState.sourceBlendAlpha = GL_ONE;
61 mState.destBlendRGB = GL_ZERO;
62 mState.destBlendAlpha = GL_ZERO;
63 mState.blendEquationRGB = GL_FUNC_ADD;
64 mState.blendEquationAlpha = GL_FUNC_ADD;
65 mState.blendColor.red = 0;
66 mState.blendColor.green = 0;
67 mState.blendColor.blue = 0;
68 mState.blendColor.alpha = 0;
69 mState.stencilTest = false;
70 mState.stencilFunc = GL_ALWAYS;
71 mState.stencilRef = 0;
72 mState.stencilMask = -1;
73 mState.stencilWritemask = -1;
74 mState.stencilBackFunc = GL_ALWAYS;
75 mState.stencilBackRef = 0;
76 mState.stencilBackMask = - 1;
77 mState.stencilBackWritemask = -1;
78 mState.stencilFail = GL_KEEP;
79 mState.stencilPassDepthFail = GL_KEEP;
80 mState.stencilPassDepthPass = GL_KEEP;
81 mState.stencilBackFail = GL_KEEP;
82 mState.stencilBackPassDepthFail = GL_KEEP;
83 mState.stencilBackPassDepthPass = GL_KEEP;
84 mState.polygonOffsetFill = false;
85 mState.polygonOffsetFactor = 0.0f;
86 mState.polygonOffsetUnits = 0.0f;
87 mState.sampleAlphaToCoverage = false;
88 mState.sampleCoverage = false;
89 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000090 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000091 mState.scissorTest = false;
92 mState.dither = true;
93 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000094 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000097
daniel@transgaming.com428d1582010-05-04 03:35:25 +000098 mState.viewportX = 0;
99 mState.viewportY = 0;
100 mState.viewportWidth = config->mDisplayMode.Width;
101 mState.viewportHeight = config->mDisplayMode.Height;
102 mState.zNear = 0.0f;
103 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000105 mState.scissorX = 0;
106 mState.scissorY = 0;
107 mState.scissorWidth = config->mDisplayMode.Width;
108 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000110 mState.colorMaskRed = true;
111 mState.colorMaskGreen = true;
112 mState.colorMaskBlue = true;
113 mState.colorMaskAlpha = true;
114 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000115
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000116 if (shareContext != NULL)
117 {
118 mResourceManager = shareContext->mResourceManager;
119 mResourceManager->addRef();
120 }
121 else
122 {
123 mResourceManager = new ResourceManager();
124 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000125
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126 // [OpenGL ES 2.0.24] section 3.7 page 83:
127 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
128 // and cube map texture state vectors respectively associated with them.
129 // In order that access to these initial textures not be lost, they are treated as texture
130 // objects all of whose names are 0.
131
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000132 mTexture2DZero.set(new Texture2D(0));
133 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000135 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000136 bindArrayBuffer(0);
137 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138 bindTextureCubeMap(0);
139 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000140 bindReadFramebuffer(0);
141 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142 bindRenderbuffer(0);
143
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000144 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000146 mState.packAlignment = 4;
147 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000148
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000149 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000150 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000151 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000152 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000153
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154 mInvalidEnum = false;
155 mInvalidValue = false;
156 mInvalidOperation = false;
157 mOutOfMemory = false;
158 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000159
160 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000161
gman@chromium.org50c526d2011-08-10 05:19:44 +0000162 mSupportsDXT1Textures = false;
163 mSupportsDXT3Textures = false;
164 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000165 mSupportsEventQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000166 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000167 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000168 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000169 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170}
171
172Context::~Context()
173{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000174 if (mState.currentProgram != 0)
175 {
176 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
177 if (programObject)
178 {
179 programObject->release();
180 }
181 mState.currentProgram = 0;
182 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000184 while (!mFramebufferMap.empty())
185 {
186 deleteFramebuffer(mFramebufferMap.begin()->first);
187 }
188
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000189 while (!mFenceMap.empty())
190 {
191 deleteFence(mFenceMap.begin()->first);
192 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000193
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000194 while (!mMultiSampleSupport.empty())
195 {
196 delete [] mMultiSampleSupport.begin()->second;
197 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
198 }
199
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000200 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000201 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000202 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000203 {
204 mState.samplerTexture[type][sampler].set(NULL);
205 }
206 }
207
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000208 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000209 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000210 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000211 }
212
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000213 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
214 {
215 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
216 }
217
218 mState.arrayBuffer.set(NULL);
219 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000220 mState.renderbuffer.set(NULL);
221
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000222 mTexture2DZero.set(NULL);
223 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000225 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000226 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000227 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000228 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000229
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000230 if (mMaskedClearSavedState)
231 {
232 mMaskedClearSavedState->Release();
233 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000234
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000235 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236}
237
238void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
239{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000240 mDisplay = display;
241 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000242
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000243 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000244 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000245 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000246
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000247 mVertexDataManager = new VertexDataManager(this, mDevice);
248 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000249 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000250
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000251 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000252 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
253 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000254
255 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
256 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
257 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
258 mMaxRenderbufferDimension = mMaxTextureDimension;
259 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
260 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
261 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
262
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000263 const D3DFORMAT renderBufferFormats[] =
264 {
265 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000266 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000267 D3DFMT_R5G6B5,
268 D3DFMT_D24S8
269 };
270
271 int max = 0;
272 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
273 {
274 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000275 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000276 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
277
278 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
279 {
280 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
281 {
282 max = j;
283 }
284 }
285 }
286
287 mMaxSupportedSamples = max;
288
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000289 mSupportsEventQueries = mDisplay->getEventQuerySupport();
290 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
291 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
292 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
293 mSupportsFloatTextures = mDisplay->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
294 mSupportsHalfFloatTextures = mDisplay->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
295 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
296 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000297
daniel@transgaming.com83921382011-01-08 05:46:00 +0000298 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
299
gman@chromium.org50c526d2011-08-10 05:19:44 +0000300 mNumCompressedTextureFormats = 0;
301 if (supportsDXT1Textures())
302 {
303 mNumCompressedTextureFormats += 2;
304 }
305 if (supportsDXT3Textures())
306 {
307 mNumCompressedTextureFormats += 1;
308 }
309 if (supportsDXT5Textures())
310 {
311 mNumCompressedTextureFormats += 1;
312 }
313
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000314 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000315 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000316
317 mState.viewportX = 0;
318 mState.viewportY = 0;
319 mState.viewportWidth = surface->getWidth();
320 mState.viewportHeight = surface->getHeight();
321
322 mState.scissorX = 0;
323 mState.scissorY = 0;
324 mState.scissorWidth = surface->getWidth();
325 mState.scissorHeight = surface->getHeight();
326
327 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000328 }
329
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
331 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000332 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000335 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000336 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337
338 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000340 if (defaultRenderTarget)
341 {
342 defaultRenderTarget->Release();
343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000345 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000347 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000349
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000350 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351}
352
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000353// 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 +0000354void Context::markAllStateDirty()
355{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000356 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
357 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000358 mAppliedTextureSerialPS[t] = 0;
359 }
360
361 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
362 {
363 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000364 }
365
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000366 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000367 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000368 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000369 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000370 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000371 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000372 mViewportInitialized = false;
373 mRenderTargetDescInitialized = false;
374
375 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000376
377 mClearStateDirty = true;
378 mCullStateDirty = true;
379 mDepthStateDirty = true;
380 mMaskStateDirty = true;
381 mBlendStateDirty = true;
382 mStencilStateDirty = true;
383 mPolygonOffsetStateDirty = true;
384 mScissorStateDirty = true;
385 mSampleStateDirty = true;
386 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000387 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000388 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000389 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000390}
391
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392void Context::setClearColor(float red, float green, float blue, float alpha)
393{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000394 mState.colorClearValue.red = red;
395 mState.colorClearValue.green = green;
396 mState.colorClearValue.blue = blue;
397 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398}
399
400void Context::setClearDepth(float depth)
401{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000402 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
405void Context::setClearStencil(int stencil)
406{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000407 mState.stencilClearValue = stencil;
408}
409
410void Context::setCullFace(bool enabled)
411{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000412 if (mState.cullFace != enabled)
413 {
414 mState.cullFace = enabled;
415 mCullStateDirty = true;
416 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000417}
418
419bool Context::isCullFaceEnabled() const
420{
421 return mState.cullFace;
422}
423
424void Context::setCullMode(GLenum mode)
425{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000426 if (mState.cullMode != mode)
427 {
428 mState.cullMode = mode;
429 mCullStateDirty = true;
430 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000431}
432
433void Context::setFrontFace(GLenum front)
434{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000435 if (mState.frontFace != front)
436 {
437 mState.frontFace = front;
438 mFrontFaceDirty = true;
439 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000440}
441
442void Context::setDepthTest(bool enabled)
443{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000444 if (mState.depthTest != enabled)
445 {
446 mState.depthTest = enabled;
447 mDepthStateDirty = true;
448 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000449}
450
451bool Context::isDepthTestEnabled() const
452{
453 return mState.depthTest;
454}
455
456void Context::setDepthFunc(GLenum depthFunc)
457{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000458 if (mState.depthFunc != depthFunc)
459 {
460 mState.depthFunc = depthFunc;
461 mDepthStateDirty = true;
462 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000463}
464
465void Context::setDepthRange(float zNear, float zFar)
466{
467 mState.zNear = zNear;
468 mState.zFar = zFar;
469}
470
471void Context::setBlend(bool enabled)
472{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000473 if (mState.blend != enabled)
474 {
475 mState.blend = enabled;
476 mBlendStateDirty = true;
477 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000478}
479
480bool Context::isBlendEnabled() const
481{
482 return mState.blend;
483}
484
485void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
486{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000487 if (mState.sourceBlendRGB != sourceRGB ||
488 mState.sourceBlendAlpha != sourceAlpha ||
489 mState.destBlendRGB != destRGB ||
490 mState.destBlendAlpha != destAlpha)
491 {
492 mState.sourceBlendRGB = sourceRGB;
493 mState.destBlendRGB = destRGB;
494 mState.sourceBlendAlpha = sourceAlpha;
495 mState.destBlendAlpha = destAlpha;
496 mBlendStateDirty = true;
497 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000498}
499
500void Context::setBlendColor(float red, float green, float blue, float alpha)
501{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000502 if (mState.blendColor.red != red ||
503 mState.blendColor.green != green ||
504 mState.blendColor.blue != blue ||
505 mState.blendColor.alpha != alpha)
506 {
507 mState.blendColor.red = red;
508 mState.blendColor.green = green;
509 mState.blendColor.blue = blue;
510 mState.blendColor.alpha = alpha;
511 mBlendStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
516{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 if (mState.blendEquationRGB != rgbEquation ||
518 mState.blendEquationAlpha != alphaEquation)
519 {
520 mState.blendEquationRGB = rgbEquation;
521 mState.blendEquationAlpha = alphaEquation;
522 mBlendStateDirty = true;
523 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000524}
525
526void Context::setStencilTest(bool enabled)
527{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000528 if (mState.stencilTest != enabled)
529 {
530 mState.stencilTest = enabled;
531 mStencilStateDirty = true;
532 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000533}
534
535bool Context::isStencilTestEnabled() const
536{
537 return mState.stencilTest;
538}
539
540void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
541{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000542 if (mState.stencilFunc != stencilFunc ||
543 mState.stencilRef != stencilRef ||
544 mState.stencilMask != stencilMask)
545 {
546 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000547 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000548 mState.stencilMask = stencilMask;
549 mStencilStateDirty = true;
550 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000551}
552
553void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
554{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000555 if (mState.stencilBackFunc != stencilBackFunc ||
556 mState.stencilBackRef != stencilBackRef ||
557 mState.stencilBackMask != stencilBackMask)
558 {
559 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000560 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000561 mState.stencilBackMask = stencilBackMask;
562 mStencilStateDirty = true;
563 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000564}
565
566void Context::setStencilWritemask(GLuint stencilWritemask)
567{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000568 if (mState.stencilWritemask != stencilWritemask)
569 {
570 mState.stencilWritemask = stencilWritemask;
571 mStencilStateDirty = true;
572 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000573}
574
575void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
576{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000577 if (mState.stencilBackWritemask != stencilBackWritemask)
578 {
579 mState.stencilBackWritemask = stencilBackWritemask;
580 mStencilStateDirty = true;
581 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000582}
583
584void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
585{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000586 if (mState.stencilFail != stencilFail ||
587 mState.stencilPassDepthFail != stencilPassDepthFail ||
588 mState.stencilPassDepthPass != stencilPassDepthPass)
589 {
590 mState.stencilFail = stencilFail;
591 mState.stencilPassDepthFail = stencilPassDepthFail;
592 mState.stencilPassDepthPass = stencilPassDepthPass;
593 mStencilStateDirty = true;
594 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000595}
596
597void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
598{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000599 if (mState.stencilBackFail != stencilBackFail ||
600 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
601 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
602 {
603 mState.stencilBackFail = stencilBackFail;
604 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
605 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
606 mStencilStateDirty = true;
607 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000608}
609
610void Context::setPolygonOffsetFill(bool enabled)
611{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000612 if (mState.polygonOffsetFill != enabled)
613 {
614 mState.polygonOffsetFill = enabled;
615 mPolygonOffsetStateDirty = true;
616 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000617}
618
619bool Context::isPolygonOffsetFillEnabled() const
620{
621 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000622
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000623}
624
625void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
626{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000627 if (mState.polygonOffsetFactor != factor ||
628 mState.polygonOffsetUnits != units)
629 {
630 mState.polygonOffsetFactor = factor;
631 mState.polygonOffsetUnits = units;
632 mPolygonOffsetStateDirty = true;
633 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000634}
635
636void Context::setSampleAlphaToCoverage(bool enabled)
637{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000638 if (mState.sampleAlphaToCoverage != enabled)
639 {
640 mState.sampleAlphaToCoverage = enabled;
641 mSampleStateDirty = true;
642 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000643}
644
645bool Context::isSampleAlphaToCoverageEnabled() const
646{
647 return mState.sampleAlphaToCoverage;
648}
649
650void Context::setSampleCoverage(bool enabled)
651{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000652 if (mState.sampleCoverage != enabled)
653 {
654 mState.sampleCoverage = enabled;
655 mSampleStateDirty = true;
656 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000657}
658
659bool Context::isSampleCoverageEnabled() const
660{
661 return mState.sampleCoverage;
662}
663
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000664void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000665{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000666 if (mState.sampleCoverageValue != value ||
667 mState.sampleCoverageInvert != invert)
668 {
669 mState.sampleCoverageValue = value;
670 mState.sampleCoverageInvert = invert;
671 mSampleStateDirty = true;
672 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000673}
674
675void Context::setScissorTest(bool enabled)
676{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000677 if (mState.scissorTest != enabled)
678 {
679 mState.scissorTest = enabled;
680 mScissorStateDirty = true;
681 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000682}
683
684bool Context::isScissorTestEnabled() const
685{
686 return mState.scissorTest;
687}
688
689void Context::setDither(bool enabled)
690{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000691 if (mState.dither != enabled)
692 {
693 mState.dither = enabled;
694 mDitherStateDirty = true;
695 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000696}
697
698bool Context::isDitherEnabled() const
699{
700 return mState.dither;
701}
702
703void Context::setLineWidth(GLfloat width)
704{
705 mState.lineWidth = width;
706}
707
708void Context::setGenerateMipmapHint(GLenum hint)
709{
710 mState.generateMipmapHint = hint;
711}
712
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000713void Context::setFragmentShaderDerivativeHint(GLenum hint)
714{
715 mState.fragmentShaderDerivativeHint = hint;
716 // TODO: Propagate the hint to shader translator so we can write
717 // ddx, ddx_coarse, or ddx_fine depending on the hint.
718 // Ignore for now. It is valid for implementations to ignore hint.
719}
720
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000721void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
722{
723 mState.viewportX = x;
724 mState.viewportY = y;
725 mState.viewportWidth = width;
726 mState.viewportHeight = height;
727}
728
729void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
730{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000731 if (mState.scissorX != x || mState.scissorY != y ||
732 mState.scissorWidth != width || mState.scissorHeight != height)
733 {
734 mState.scissorX = x;
735 mState.scissorY = y;
736 mState.scissorWidth = width;
737 mState.scissorHeight = height;
738 mScissorStateDirty = true;
739 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000740}
741
742void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
743{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000744 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
745 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
746 {
747 mState.colorMaskRed = red;
748 mState.colorMaskGreen = green;
749 mState.colorMaskBlue = blue;
750 mState.colorMaskAlpha = alpha;
751 mMaskStateDirty = true;
752 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000753}
754
755void Context::setDepthMask(bool mask)
756{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000757 if (mState.depthMask != mask)
758 {
759 mState.depthMask = mask;
760 mMaskStateDirty = true;
761 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000762}
763
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000764void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000765{
766 mState.activeSampler = active;
767}
768
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000769GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000770{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000771 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000772}
773
774GLuint Context::getDrawFramebufferHandle() const
775{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000776 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000777}
778
779GLuint Context::getRenderbufferHandle() const
780{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000781 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000782}
783
784GLuint Context::getArrayBufferHandle() const
785{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000786 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000787}
788
daniel@transgaming.com83921382011-01-08 05:46:00 +0000789void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000790{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000791 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000792}
793
daniel@transgaming.com83921382011-01-08 05:46:00 +0000794const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000795{
796 return mState.vertexAttribute[attribNum];
797}
798
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000799void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000800 GLsizei stride, const void *pointer)
801{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000802 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000803 mState.vertexAttribute[attribNum].mSize = size;
804 mState.vertexAttribute[attribNum].mType = type;
805 mState.vertexAttribute[attribNum].mNormalized = normalized;
806 mState.vertexAttribute[attribNum].mStride = stride;
807 mState.vertexAttribute[attribNum].mPointer = pointer;
808}
809
810const void *Context::getVertexAttribPointer(unsigned int attribNum) const
811{
812 return mState.vertexAttribute[attribNum].mPointer;
813}
814
daniel@transgaming.com83921382011-01-08 05:46:00 +0000815const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000816{
817 return mState.vertexAttribute;
818}
819
820void Context::setPackAlignment(GLint alignment)
821{
822 mState.packAlignment = alignment;
823}
824
825GLint Context::getPackAlignment() const
826{
827 return mState.packAlignment;
828}
829
830void Context::setUnpackAlignment(GLint alignment)
831{
832 mState.unpackAlignment = alignment;
833}
834
835GLint Context::getUnpackAlignment() const
836{
837 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838}
839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840GLuint Context::createBuffer()
841{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000842 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843}
844
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845GLuint Context::createProgram()
846{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000847 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000850GLuint Context::createShader(GLenum type)
851{
852 return mResourceManager->createShader(type);
853}
854
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855GLuint Context::createTexture()
856{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000857 return mResourceManager->createTexture();
858}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000860GLuint Context::createRenderbuffer()
861{
862 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863}
864
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000865// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866GLuint Context::createFramebuffer()
867{
benvanik@google.com1a233342011-04-28 19:44:39 +0000868 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000869
870 mFramebufferMap[handle] = NULL;
871
872 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873}
874
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000875GLuint Context::createFence()
876{
benvanik@google.com1a233342011-04-28 19:44:39 +0000877 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000878
879 mFenceMap[handle] = new Fence;
880
881 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000882}
883
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884void Context::deleteBuffer(GLuint buffer)
885{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000886 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887 {
888 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000890
891 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892}
893
894void Context::deleteShader(GLuint shader)
895{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000896 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
898
899void Context::deleteProgram(GLuint program)
900{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000901 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000902 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903}
904
905void Context::deleteTexture(GLuint texture)
906{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908 {
909 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000911
912 mResourceManager->deleteTexture(texture);
913}
914
915void Context::deleteRenderbuffer(GLuint renderbuffer)
916{
917 if (mResourceManager->getRenderbuffer(renderbuffer))
918 {
919 detachRenderbuffer(renderbuffer);
920 }
921
922 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923}
924
925void Context::deleteFramebuffer(GLuint framebuffer)
926{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000927 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
928
929 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930 {
931 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000932
benvanik@google.com1a233342011-04-28 19:44:39 +0000933 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000934 delete framebufferObject->second;
935 mFramebufferMap.erase(framebufferObject);
936 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000938
939void Context::deleteFence(GLuint fence)
940{
941 FenceMap::iterator fenceObject = mFenceMap.find(fence);
942
943 if (fenceObject != mFenceMap.end())
944 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000945 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000946 delete fenceObject->second;
947 mFenceMap.erase(fenceObject);
948 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000949}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000951Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000953 return mResourceManager->getBuffer(handle);
954}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000956Shader *Context::getShader(GLuint handle)
957{
958 return mResourceManager->getShader(handle);
959}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000961Program *Context::getProgram(GLuint handle)
962{
963 return mResourceManager->getProgram(handle);
964}
965
966Texture *Context::getTexture(GLuint handle)
967{
968 return mResourceManager->getTexture(handle);
969}
970
971Renderbuffer *Context::getRenderbuffer(GLuint handle)
972{
973 return mResourceManager->getRenderbuffer(handle);
974}
975
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000976Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000977{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000978 return getFramebuffer(mState.readFramebuffer);
979}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000980
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000981Framebuffer *Context::getDrawFramebuffer()
982{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +0000983 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984}
985
986void Context::bindArrayBuffer(unsigned int buffer)
987{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000988 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000990 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991}
992
993void Context::bindElementArrayBuffer(unsigned int buffer)
994{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000995 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000997 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998}
999
1000void Context::bindTexture2D(GLuint texture)
1001{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001002 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001004 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005}
1006
1007void Context::bindTextureCubeMap(GLuint texture)
1008{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001009 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001011 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012}
1013
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001014void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001016 if (!getFramebuffer(framebuffer))
1017 {
1018 mFramebufferMap[framebuffer] = new Framebuffer();
1019 }
1020
1021 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001022}
1023
1024void Context::bindDrawFramebuffer(GLuint framebuffer)
1025{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001026 if (!getFramebuffer(framebuffer))
1027 {
1028 mFramebufferMap[framebuffer] = new Framebuffer();
1029 }
1030
1031 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001032
1033 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034}
1035
1036void Context::bindRenderbuffer(GLuint renderbuffer)
1037{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001038 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1039
1040 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041}
1042
1043void Context::useProgram(GLuint program)
1044{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001045 GLuint priorProgram = mState.currentProgram;
1046 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001047
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001048 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001050 Program *newProgram = mResourceManager->getProgram(program);
1051 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001052 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001053 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001054
1055 if (newProgram)
1056 {
1057 newProgram->addRef();
1058 }
1059
1060 if (oldProgram)
1061 {
1062 oldProgram->release();
1063 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065}
1066
1067void Context::setFramebufferZero(Framebuffer *buffer)
1068{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001069 delete mFramebufferMap[0];
1070 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001071 if (mState.drawFramebuffer == 0)
1072 {
1073 mBoundDrawFramebuffer = buffer;
1074 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075}
1076
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001077void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001079 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1080 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081}
1082
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001083Framebuffer *Context::getFramebuffer(unsigned int handle)
1084{
1085 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1086
1087 if (framebuffer == mFramebufferMap.end())
1088 {
1089 return NULL;
1090 }
1091 else
1092 {
1093 return framebuffer->second;
1094 }
1095}
1096
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001097Fence *Context::getFence(unsigned int handle)
1098{
1099 FenceMap::iterator fence = mFenceMap.find(handle);
1100
1101 if (fence == mFenceMap.end())
1102 {
1103 return NULL;
1104 }
1105 else
1106 {
1107 return fence->second;
1108 }
1109}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001110
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111Buffer *Context::getArrayBuffer()
1112{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001113 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114}
1115
1116Buffer *Context::getElementArrayBuffer()
1117{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001118 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119}
1120
1121Program *Context::getCurrentProgram()
1122{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001123 if (!mCachedCurrentProgram)
1124 {
1125 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1126 }
1127 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128}
1129
1130Texture2D *Context::getTexture2D()
1131{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001132 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001133}
1134
1135TextureCubeMap *Context::getTextureCubeMap()
1136{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001137 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001138}
1139
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001140Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001142 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001143
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001144 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001145 {
1146 switch (type)
1147 {
1148 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001149 case TEXTURE_2D: return mTexture2DZero.get();
1150 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001151 }
1152 }
1153
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001154 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001155}
1156
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001157bool Context::getBooleanv(GLenum pname, GLboolean *params)
1158{
1159 switch (pname)
1160 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1162 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1163 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001164 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001165 params[0] = mState.colorMaskRed;
1166 params[1] = mState.colorMaskGreen;
1167 params[2] = mState.colorMaskBlue;
1168 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001169 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001170 case GL_CULL_FACE: *params = mState.cullFace; break;
1171 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1172 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1173 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1174 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1175 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1176 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1177 case GL_BLEND: *params = mState.blend; break;
1178 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001179 default:
1180 return false;
1181 }
1182
1183 return true;
1184}
1185
1186bool Context::getFloatv(GLenum pname, GLfloat *params)
1187{
1188 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1189 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1190 // GetIntegerv as its native query function. As it would require conversion in any
1191 // case, this should make no difference to the calling application.
1192 switch (pname)
1193 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001194 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1195 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1196 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1197 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1198 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001199 case GL_ALIASED_LINE_WIDTH_RANGE:
1200 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1201 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1202 break;
1203 case GL_ALIASED_POINT_SIZE_RANGE:
1204 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001205 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 +00001206 break;
1207 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001208 params[0] = mState.zNear;
1209 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001210 break;
1211 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001212 params[0] = mState.colorClearValue.red;
1213 params[1] = mState.colorClearValue.green;
1214 params[2] = mState.colorClearValue.blue;
1215 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001216 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001217 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001218 params[0] = mState.blendColor.red;
1219 params[1] = mState.blendColor.green;
1220 params[2] = mState.blendColor.blue;
1221 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001222 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001223 default:
1224 return false;
1225 }
1226
1227 return true;
1228}
1229
1230bool Context::getIntegerv(GLenum pname, GLint *params)
1231{
1232 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1233 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1234 // GetIntegerv as its native query function. As it would require conversion in any
1235 // case, this should make no difference to the calling application. You may find it in
1236 // Context::getFloatv.
1237 switch (pname)
1238 {
1239 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1240 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001241 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001242 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1243 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001244 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001245 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001246 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001247 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001248 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001249 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1250 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001251 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1252 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1253 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001254 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001255 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1256 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1257 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1258 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001259 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001260 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1261 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1262 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1263 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1264 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1265 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1266 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1267 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1268 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1269 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1270 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1271 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1272 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1273 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1274 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1275 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1276 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1277 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1278 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1279 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1280 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1281 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1282 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1283 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001284 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1285 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001286 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001287 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001288 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001289 case GL_MAX_SAMPLES_ANGLE:
1290 {
1291 GLsizei maxSamples = getMaxSupportedSamples();
1292 if (maxSamples != 0)
1293 {
1294 *params = maxSamples;
1295 }
1296 else
1297 {
1298 return false;
1299 }
1300
1301 break;
1302 }
1303 case GL_SAMPLE_BUFFERS:
1304 case GL_SAMPLES:
1305 {
1306 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1307 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1308 {
1309 switch (pname)
1310 {
1311 case GL_SAMPLE_BUFFERS:
1312 if (framebuffer->getSamples() != 0)
1313 {
1314 *params = 1;
1315 }
1316 else
1317 {
1318 *params = 0;
1319 }
1320 break;
1321 case GL_SAMPLES:
1322 *params = framebuffer->getSamples();
1323 break;
1324 }
1325 }
1326 else
1327 {
1328 *params = 0;
1329 }
1330 }
1331 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001332 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1333 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1334 case GL_MAX_VIEWPORT_DIMS:
1335 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001336 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001337 params[0] = maxDimension;
1338 params[1] = maxDimension;
1339 }
1340 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001341 case GL_COMPRESSED_TEXTURE_FORMATS:
1342 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001343 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001344 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001345 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1346 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1347 }
1348 if (supportsDXT3Textures())
1349 {
1350 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1351 }
1352 if (supportsDXT5Textures())
1353 {
1354 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001355 }
1356 }
1357 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001358 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001359 params[0] = mState.viewportX;
1360 params[1] = mState.viewportY;
1361 params[2] = mState.viewportWidth;
1362 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001363 break;
1364 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001365 params[0] = mState.scissorX;
1366 params[1] = mState.scissorY;
1367 params[2] = mState.scissorWidth;
1368 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001369 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001370 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1371 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001372 case GL_RED_BITS:
1373 case GL_GREEN_BITS:
1374 case GL_BLUE_BITS:
1375 case GL_ALPHA_BITS:
1376 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001377 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001378 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1379
1380 if (colorbuffer)
1381 {
1382 switch (pname)
1383 {
1384 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1385 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1386 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1387 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1388 }
1389 }
1390 else
1391 {
1392 *params = 0;
1393 }
1394 }
1395 break;
1396 case GL_DEPTH_BITS:
1397 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001398 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001399 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001400
1401 if (depthbuffer)
1402 {
1403 *params = depthbuffer->getDepthSize();
1404 }
1405 else
1406 {
1407 *params = 0;
1408 }
1409 }
1410 break;
1411 case GL_STENCIL_BITS:
1412 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001413 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001414 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001415
1416 if (stencilbuffer)
1417 {
1418 *params = stencilbuffer->getStencilSize();
1419 }
1420 else
1421 {
1422 *params = 0;
1423 }
1424 }
1425 break;
1426 case GL_TEXTURE_BINDING_2D:
1427 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001428 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001429 {
1430 error(GL_INVALID_OPERATION);
1431 return false;
1432 }
1433
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001434 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001435 }
1436 break;
1437 case GL_TEXTURE_BINDING_CUBE_MAP:
1438 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001439 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001440 {
1441 error(GL_INVALID_OPERATION);
1442 return false;
1443 }
1444
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001445 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001446 }
1447 break;
1448 default:
1449 return false;
1450 }
1451
1452 return true;
1453}
1454
1455bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1456{
1457 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1458 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1459 // to the fact that it is stored internally as a float, and so would require conversion
1460 // if returned from Context::getIntegerv. Since this conversion is already implemented
1461 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1462 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1463 // application.
1464 switch (pname)
1465 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001466 case GL_COMPRESSED_TEXTURE_FORMATS:
1467 {
1468 *type = GL_INT;
1469 *numParams = mNumCompressedTextureFormats;
1470 }
1471 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001472 case GL_SHADER_BINARY_FORMATS:
1473 {
1474 *type = GL_INT;
1475 *numParams = 0;
1476 }
1477 break;
1478 case GL_MAX_VERTEX_ATTRIBS:
1479 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1480 case GL_MAX_VARYING_VECTORS:
1481 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1482 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1483 case GL_MAX_TEXTURE_IMAGE_UNITS:
1484 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1485 case GL_MAX_RENDERBUFFER_SIZE:
1486 case GL_NUM_SHADER_BINARY_FORMATS:
1487 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1488 case GL_ARRAY_BUFFER_BINDING:
1489 case GL_FRAMEBUFFER_BINDING:
1490 case GL_RENDERBUFFER_BINDING:
1491 case GL_CURRENT_PROGRAM:
1492 case GL_PACK_ALIGNMENT:
1493 case GL_UNPACK_ALIGNMENT:
1494 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001495 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001496 case GL_RED_BITS:
1497 case GL_GREEN_BITS:
1498 case GL_BLUE_BITS:
1499 case GL_ALPHA_BITS:
1500 case GL_DEPTH_BITS:
1501 case GL_STENCIL_BITS:
1502 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1503 case GL_CULL_FACE_MODE:
1504 case GL_FRONT_FACE:
1505 case GL_ACTIVE_TEXTURE:
1506 case GL_STENCIL_FUNC:
1507 case GL_STENCIL_VALUE_MASK:
1508 case GL_STENCIL_REF:
1509 case GL_STENCIL_FAIL:
1510 case GL_STENCIL_PASS_DEPTH_FAIL:
1511 case GL_STENCIL_PASS_DEPTH_PASS:
1512 case GL_STENCIL_BACK_FUNC:
1513 case GL_STENCIL_BACK_VALUE_MASK:
1514 case GL_STENCIL_BACK_REF:
1515 case GL_STENCIL_BACK_FAIL:
1516 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1517 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1518 case GL_DEPTH_FUNC:
1519 case GL_BLEND_SRC_RGB:
1520 case GL_BLEND_SRC_ALPHA:
1521 case GL_BLEND_DST_RGB:
1522 case GL_BLEND_DST_ALPHA:
1523 case GL_BLEND_EQUATION_RGB:
1524 case GL_BLEND_EQUATION_ALPHA:
1525 case GL_STENCIL_WRITEMASK:
1526 case GL_STENCIL_BACK_WRITEMASK:
1527 case GL_STENCIL_CLEAR_VALUE:
1528 case GL_SUBPIXEL_BITS:
1529 case GL_MAX_TEXTURE_SIZE:
1530 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1531 case GL_SAMPLE_BUFFERS:
1532 case GL_SAMPLES:
1533 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1534 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1535 case GL_TEXTURE_BINDING_2D:
1536 case GL_TEXTURE_BINDING_CUBE_MAP:
1537 {
1538 *type = GL_INT;
1539 *numParams = 1;
1540 }
1541 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001542 case GL_MAX_SAMPLES_ANGLE:
1543 {
1544 if (getMaxSupportedSamples() != 0)
1545 {
1546 *type = GL_INT;
1547 *numParams = 1;
1548 }
1549 else
1550 {
1551 return false;
1552 }
1553 }
1554 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001555 case GL_MAX_VIEWPORT_DIMS:
1556 {
1557 *type = GL_INT;
1558 *numParams = 2;
1559 }
1560 break;
1561 case GL_VIEWPORT:
1562 case GL_SCISSOR_BOX:
1563 {
1564 *type = GL_INT;
1565 *numParams = 4;
1566 }
1567 break;
1568 case GL_SHADER_COMPILER:
1569 case GL_SAMPLE_COVERAGE_INVERT:
1570 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001571 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1572 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1573 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1574 case GL_SAMPLE_COVERAGE:
1575 case GL_SCISSOR_TEST:
1576 case GL_STENCIL_TEST:
1577 case GL_DEPTH_TEST:
1578 case GL_BLEND:
1579 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001580 {
1581 *type = GL_BOOL;
1582 *numParams = 1;
1583 }
1584 break;
1585 case GL_COLOR_WRITEMASK:
1586 {
1587 *type = GL_BOOL;
1588 *numParams = 4;
1589 }
1590 break;
1591 case GL_POLYGON_OFFSET_FACTOR:
1592 case GL_POLYGON_OFFSET_UNITS:
1593 case GL_SAMPLE_COVERAGE_VALUE:
1594 case GL_DEPTH_CLEAR_VALUE:
1595 case GL_LINE_WIDTH:
1596 {
1597 *type = GL_FLOAT;
1598 *numParams = 1;
1599 }
1600 break;
1601 case GL_ALIASED_LINE_WIDTH_RANGE:
1602 case GL_ALIASED_POINT_SIZE_RANGE:
1603 case GL_DEPTH_RANGE:
1604 {
1605 *type = GL_FLOAT;
1606 *numParams = 2;
1607 }
1608 break;
1609 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001610 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001611 {
1612 *type = GL_FLOAT;
1613 *numParams = 4;
1614 }
1615 break;
1616 default:
1617 return false;
1618 }
1619
1620 return true;
1621}
1622
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001623// Applies the render target surface, depth stencil surface, viewport rectangle and
1624// scissor rectangle to the Direct3D 9 device
1625bool Context::applyRenderTarget(bool ignoreViewport)
1626{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001627 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001628
1629 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1630 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001631 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632 }
1633
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001634 IDirect3DSurface9 *renderTarget = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001635 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001636
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001637 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001638 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1639 if (renderTargetSerial != mAppliedRenderTargetSerial)
1640 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001641 renderTarget = framebufferObject->getRenderTarget();
1642
1643 if (!renderTarget)
1644 {
1645 return false; // Context must be lost
1646 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001647 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001648 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001649 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 +00001650 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001651 }
1652
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001653 unsigned int depthbufferSerial = 0;
1654 unsigned int stencilbufferSerial = 0;
1655 if (framebufferObject->getDepthbufferType() != GL_NONE)
1656 {
1657 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001658 if (!depthStencil)
1659 {
1660 ERR("Depth stencil pointer unexpectedly null.");
1661 return false;
1662 }
1663
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001664 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1665 }
1666 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1667 {
1668 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001669 if (!depthStencil)
1670 {
1671 ERR("Depth stencil pointer unexpectedly null.");
1672 return false;
1673 }
1674
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001675 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1676 }
1677
1678 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001679 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001680 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001681 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001682 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001683 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001684 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001685 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001686 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001688 if (!mRenderTargetDescInitialized || renderTargetChanged)
1689 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001690 if (!renderTarget)
1691 {
1692 renderTarget = framebufferObject->getRenderTarget();
1693
1694 if (!renderTarget)
1695 {
1696 return false; // Context must be lost
1697 }
1698 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001699 renderTarget->GetDesc(&mRenderTargetDesc);
1700 mRenderTargetDescInitialized = true;
1701 }
1702
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001703 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001705 float zNear = clamp01(mState.zNear);
1706 float zFar = clamp01(mState.zFar);
1707
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001708 if (ignoreViewport)
1709 {
1710 viewport.X = 0;
1711 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001712 viewport.Width = mRenderTargetDesc.Width;
1713 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714 viewport.MinZ = 0.0f;
1715 viewport.MaxZ = 1.0f;
1716 }
1717 else
1718 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001719 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1720 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1721 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1722 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1723 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 +00001724 viewport.MinZ = zNear;
1725 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001726 }
1727
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001728 if (viewport.Width <= 0 || viewport.Height <= 0)
1729 {
1730 return false; // Nothing to render
1731 }
1732
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001733 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001734 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001735 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001736 mSetViewport = viewport;
1737 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001738 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001743 if (mState.scissorTest)
1744 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001745 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1746 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1747 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1748 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1749 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001750 mDevice->SetScissorRect(&rect);
1751 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001752 }
1753 else
1754 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001755 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001757
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001758 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 }
1760
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001761 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 Program *programObject = getCurrentProgram();
1764
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001765 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001766 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001767 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001768
daniel@transgaming.com31754962010-11-28 02:02:52 +00001769 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001770 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1771 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1772 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001773 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001774
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001775 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001776 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001777 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001778
daniel@transgaming.com31754962010-11-28 02:02:52 +00001779 GLint depthRange = programObject->getDxDepthRangeLocation();
1780 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1781 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001782 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783 }
1784
1785 return true;
1786}
1787
1788// 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 +00001789void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001790{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001791 Program *programObject = getCurrentProgram();
1792
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001793 Framebuffer *framebufferObject = getDrawFramebuffer();
1794
1795 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1796
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001797 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001798 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001799 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001801 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001802 GLint alwaysFront = !isTriangleMode(drawMode);
1803 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1804
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001805 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001806 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1807 // Apparently some ATI cards have a bug where a draw with a zero color
1808 // write mask can cause later draws to have incorrect results. Instead,
1809 // set a nonzero color write mask but modify the blend state so that no
1810 // drawing is done.
1811 // http://code.google.com/p/angleproject/issues/detail?id=169
1812
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001813 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001815 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001817 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818 }
1819 else
1820 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001821 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822 }
1823
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001824 mCullStateDirty = false;
1825 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001827 if (mDepthStateDirty)
1828 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001829 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001831 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1832 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833 }
1834 else
1835 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001836 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001838
1839 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840 }
1841
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001842 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1843 {
1844 mBlendStateDirty = true;
1845 mMaskStateDirty = true;
1846 }
1847
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001849 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001850 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001851 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001852 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001853
1854 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1855 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1856 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001857 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001858 }
1859 else
1860 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001861 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001862 unorm<8>(mState.blendColor.alpha),
1863 unorm<8>(mState.blendColor.alpha),
1864 unorm<8>(mState.blendColor.alpha)));
1865 }
1866
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001867 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1868 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1869 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001870
1871 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1872 mState.destBlendRGB != mState.destBlendAlpha ||
1873 mState.blendEquationRGB != mState.blendEquationAlpha)
1874 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001875 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001876
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001877 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1878 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1879 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001880 }
1881 else
1882 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001883 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001884 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001885 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001886 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001887 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001888 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001889 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001890
1891 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892 }
1893
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001894 if (mStencilStateDirty || mFrontFaceDirty)
1895 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001896 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001897 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001898 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1899 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001900
1901 // FIXME: Unsupported by D3D9
1902 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1903 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1904 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1905 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1906 mState.stencilRef != mState.stencilBackRef ||
1907 mState.stencilMask != mState.stencilBackMask)
1908 {
1909 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1910 return error(GL_INVALID_OPERATION);
1911 }
1912
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001913 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001914 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001915 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1916
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001917 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1918 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001919 es2dx::ConvertComparison(mState.stencilFunc));
1920
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001921 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1922 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001923
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001924 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001925 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001926 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001927 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001928 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001929 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1930
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001931 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1932 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001933 es2dx::ConvertComparison(mState.stencilBackFunc));
1934
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001935 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1936 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001937
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001938 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001939 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001940 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001941 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001942 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001943 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1944 }
1945 else
1946 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001947 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001948 }
1949
1950 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001951 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001952 }
1953
1954 if (mMaskStateDirty)
1955 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001956 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1957 mState.colorMaskBlue, mState.colorMaskAlpha);
1958 if (colorMask == 0 && !zeroColorMaskAllowed)
1959 {
1960 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001961 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1962 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001963
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001964 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1965 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1966 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001967 }
1968 else
1969 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001970 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001971 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001972 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001973
1974 mMaskStateDirty = false;
1975 }
1976
1977 if (mPolygonOffsetStateDirty)
1978 {
1979 if (mState.polygonOffsetFill)
1980 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001981 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001982 if (depthbuffer)
1983 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001984 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001985 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001986 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001987 }
1988 }
1989 else
1990 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001991 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1992 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001993 }
1994
1995 mPolygonOffsetStateDirty = false;
1996 }
1997
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001998 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002000 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002001 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002002 FIXME("Sample alpha to coverage is unimplemented.");
2003 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002004
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002005 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002006 if (mState.sampleCoverage)
2007 {
2008 unsigned int mask = 0;
2009 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002010 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002011 float threshold = 0.5f;
2012
2013 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002014 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002015 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002016
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002017 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002018 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002019 threshold += 1.0f;
2020 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002021 }
2022 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002023 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002024
2025 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002026 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002027 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002028 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002029
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002030 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002031 }
2032 else
2033 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002034 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002035 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002036
2037 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038 }
2039
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002040 if (mDitherStateDirty)
2041 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002042 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002043
2044 mDitherStateDirty = false;
2045 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046}
2047
daniel@transgaming.com83921382011-01-08 05:46:00 +00002048GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002049{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002050 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002051
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002052 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002053 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002054 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002055 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002056 }
2057
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002058 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002059}
2060
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002061// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002062GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002063{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002064 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002065
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002066 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002067 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002068 if (indexInfo->serial != mAppliedIBSerial)
2069 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002070 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002071 mAppliedIBSerial = indexInfo->serial;
2072 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002073 }
2074
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002075 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076}
2077
2078// Applies the shaders and shader constants to the Direct3D 9 device
2079void Context::applyShaders()
2080{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002082 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002083 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002084 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2085 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2086
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002087 mDevice->SetPixelShader(pixelShader);
2088 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002089 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002090 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002091 }
2092
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 programObject->applyUniforms();
2094}
2095
2096// Applies the textures and sampler states to the Direct3D 9 device
2097void Context::applyTextures()
2098{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002099 applyTextures(SAMPLER_PIXEL);
2100
2101 if (mSupportsVertexTexture)
2102 {
2103 applyTextures(SAMPLER_VERTEX);
2104 }
2105}
2106
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002107// For each Direct3D 9 sampler of either the pixel or vertex stage,
2108// looks up the corresponding OpenGL texture image unit and texture type,
2109// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002110void Context::applyTextures(SamplerType type)
2111{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 Program *programObject = getCurrentProgram();
2113
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002114 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002115 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2116 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002117 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002118
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002119 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002121 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002122 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002123
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002124 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002126 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002127
2128 Texture *texture = getSamplerTexture(textureUnit, textureType);
2129
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002130 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002131 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002132 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2133
2134 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002135 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002136 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002137 {
2138 GLenum wrapS = texture->getWrapS();
2139 GLenum wrapT = texture->getWrapT();
2140 GLenum minFilter = texture->getMinFilter();
2141 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002143 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2144 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002146 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002147 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2148 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002149 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2150 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002151 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002153 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002154 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002155 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002156 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002157 }
2158 else
2159 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002160 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002161 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002162
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002163 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002164 texture->resetDirty();
2165 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002167 else
2168 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002169 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002170 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002171 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002172 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002173 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002174 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002176
2177 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2178 {
2179 if (appliedTextureSerial[samplerIndex] != 0)
2180 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002181 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002182 appliedTextureSerial[samplerIndex] = 0;
2183 }
2184 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185}
2186
2187void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2188{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002189 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002190
2191 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2192 {
2193 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2194 }
2195
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002196 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2197 {
2198 return error(GL_INVALID_OPERATION);
2199 }
2200
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002202
2203 if (!renderTarget)
2204 {
2205 return; // Context must be lost, return silently
2206 }
2207
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002208 D3DSURFACE_DESC desc;
2209 renderTarget->GetDesc(&desc);
2210
2211 IDirect3DSurface9 *systemSurface;
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002212 HRESULT result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002214 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002215 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002216 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002217 return error(GL_OUT_OF_MEMORY);
2218 }
2219
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2221 {
2222 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002223 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002224 }
2225
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002226 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002227
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002228 if (FAILED(result))
2229 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002230 systemSurface->Release();
2231
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002232 switch (result)
2233 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002234 // It turns out that D3D will sometimes produce more error
2235 // codes than those documented.
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002236 case D3DERR_DRIVERINTERNALERROR:
2237 case D3DERR_DEVICELOST:
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002238 case D3DERR_DEVICEHUNG:
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002239 return error(GL_OUT_OF_MEMORY);
2240 default:
2241 UNREACHABLE();
2242 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002243 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 }
2245
2246 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002247 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2248 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2249 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2250 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2251 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252
2253 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2254
2255 if (FAILED(result))
2256 {
2257 UNREACHABLE();
2258 systemSurface->Release();
2259
2260 return; // No sensible error to generate
2261 }
2262
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002263 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002265 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002266 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002267 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002268
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 for (int j = 0; j < rect.bottom - rect.top; j++)
2270 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002271 if (desc.Format == D3DFMT_A8R8G8B8 &&
2272 format == GL_BGRA_EXT &&
2273 type == GL_UNSIGNED_BYTE)
2274 {
2275 // Fast path for EXT_read_format_bgra, given
2276 // an RGBA source buffer. Note that buffers with no
2277 // alpha go through the slow path below.
2278 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002279 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002280 (rect.right - rect.left) * 4);
2281 continue;
2282 }
2283
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284 for (int i = 0; i < rect.right - rect.left; i++)
2285 {
2286 float r;
2287 float g;
2288 float b;
2289 float a;
2290
2291 switch (desc.Format)
2292 {
2293 case D3DFMT_R5G6B5:
2294 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002295 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296
2297 a = 1.0f;
2298 b = (rgb & 0x001F) * (1.0f / 0x001F);
2299 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2300 r = (rgb & 0xF800) * (1.0f / 0xF800);
2301 }
2302 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 case D3DFMT_A1R5G5B5:
2304 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002305 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306
2307 a = (argb & 0x8000) ? 1.0f : 0.0f;
2308 b = (argb & 0x001F) * (1.0f / 0x001F);
2309 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2310 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2311 }
2312 break;
2313 case D3DFMT_A8R8G8B8:
2314 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002315 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316
2317 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2318 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2319 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2320 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2321 }
2322 break;
2323 case D3DFMT_X8R8G8B8:
2324 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002325 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326
2327 a = 1.0f;
2328 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2329 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2330 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2331 }
2332 break;
2333 case D3DFMT_A2R10G10B10:
2334 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002335 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336
2337 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2338 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2339 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2340 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2341 }
2342 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002343 case D3DFMT_A32B32G32R32F:
2344 {
2345 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002346 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2347 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2348 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2349 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002350 }
2351 break;
2352 case D3DFMT_A16B16G16R16F:
2353 {
2354 // float formats in D3D are stored rgba, rather than the other way round
2355 float abgr[4];
2356
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002357 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002358
2359 a = abgr[3];
2360 b = abgr[2];
2361 g = abgr[1];
2362 r = abgr[0];
2363 }
2364 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002365 default:
2366 UNIMPLEMENTED(); // FIXME
2367 UNREACHABLE();
2368 }
2369
2370 switch (format)
2371 {
2372 case GL_RGBA:
2373 switch (type)
2374 {
2375 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002376 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2377 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2378 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2379 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380 break;
2381 default: UNREACHABLE();
2382 }
2383 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002384 case GL_BGRA_EXT:
2385 switch (type)
2386 {
2387 case GL_UNSIGNED_BYTE:
2388 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2389 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2390 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2391 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2392 break;
2393 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2394 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2395 // this type is packed as follows:
2396 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2397 // --------------------------------------------------------------------------------
2398 // | 4th | 3rd | 2nd | 1st component |
2399 // --------------------------------------------------------------------------------
2400 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2401 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2402 ((unsigned short)(15 * a + 0.5f) << 12)|
2403 ((unsigned short)(15 * r + 0.5f) << 8) |
2404 ((unsigned short)(15 * g + 0.5f) << 4) |
2405 ((unsigned short)(15 * b + 0.5f) << 0);
2406 break;
2407 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2408 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2409 // this type is packed as follows:
2410 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2411 // --------------------------------------------------------------------------------
2412 // | 4th | 3rd | 2nd | 1st component |
2413 // --------------------------------------------------------------------------------
2414 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2415 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2416 ((unsigned short)( a + 0.5f) << 15) |
2417 ((unsigned short)(31 * r + 0.5f) << 10) |
2418 ((unsigned short)(31 * g + 0.5f) << 5) |
2419 ((unsigned short)(31 * b + 0.5f) << 0);
2420 break;
2421 default: UNREACHABLE();
2422 }
2423 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002424 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425 switch (type)
2426 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002427 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002428 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2429 ((unsigned short)(31 * b + 0.5f) << 0) |
2430 ((unsigned short)(63 * g + 0.5f) << 5) |
2431 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 break;
2433 default: UNREACHABLE();
2434 }
2435 break;
2436 default: UNREACHABLE();
2437 }
2438 }
2439 }
2440
2441 systemSurface->UnlockRect();
2442
2443 systemSurface->Release();
2444}
2445
2446void Context::clear(GLbitfield mask)
2447{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002448 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002449
2450 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2451 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002452 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002453 }
2454
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002455 DWORD flags = 0;
2456
2457 if (mask & GL_COLOR_BUFFER_BIT)
2458 {
2459 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002460
2461 if (framebufferObject->getColorbufferType() != GL_NONE)
2462 {
2463 flags |= D3DCLEAR_TARGET;
2464 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 }
2466
2467 if (mask & GL_DEPTH_BUFFER_BIT)
2468 {
2469 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002470 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471 {
2472 flags |= D3DCLEAR_ZBUFFER;
2473 }
2474 }
2475
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 GLuint stencilUnmasked = 0x0;
2477
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002478 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002481 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002482 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002483 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002484 if (!depthStencil)
2485 {
2486 ERR("Depth stencil pointer unexpectedly null.");
2487 return;
2488 }
2489
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002490 D3DSURFACE_DESC desc;
2491 depthStencil->GetDesc(&desc);
2492
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002493 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002494 stencilUnmasked = (0x1 << stencilSize) - 1;
2495
2496 if (stencilUnmasked != 0x0)
2497 {
2498 flags |= D3DCLEAR_STENCIL;
2499 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002500 }
2501 }
2502
2503 if (mask != 0)
2504 {
2505 return error(GL_INVALID_VALUE);
2506 }
2507
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002508 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2509 {
2510 return;
2511 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002513 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002514 unorm<8>(mState.colorClearValue.red),
2515 unorm<8>(mState.colorClearValue.green),
2516 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002517 float depth = clamp01(mState.depthClearValue);
2518 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002519
2520 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2521
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002522 if (!renderTarget)
2523 {
2524 return; // Context must be lost, return silently
2525 }
2526
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527 D3DSURFACE_DESC desc;
2528 renderTarget->GetDesc(&desc);
2529
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002530 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531
2532 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002533 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002535 !(mState.colorMaskRed && mState.colorMaskGreen &&
2536 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537
2538 if (needMaskedColorClear || needMaskedStencilClear)
2539 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002540 // State which is altered in all paths from this point to the clear call is saved.
2541 // State which is altered in only some paths will be flagged dirty in the case that
2542 // that path is taken.
2543 HRESULT hr;
2544 if (mMaskedClearSavedState == NULL)
2545 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002546 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002547 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2548
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002549 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2550 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2551 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2552 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2553 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2554 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2555 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2556 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2557 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2558 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2559 mDevice->SetPixelShader(NULL);
2560 mDevice->SetVertexShader(NULL);
2561 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2562 mDevice->SetStreamSource(0, NULL, 0, 0);
2563 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2564 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2565 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2566 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2567 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2568 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2569 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002570
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002571 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002572 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2573 }
2574
2575 ASSERT(mMaskedClearSavedState != NULL);
2576
2577 if (mMaskedClearSavedState != NULL)
2578 {
2579 hr = mMaskedClearSavedState->Capture();
2580 ASSERT(SUCCEEDED(hr));
2581 }
2582
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002583 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2584 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2585 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2586 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2587 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2588 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2589 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2590 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591
2592 if (flags & D3DCLEAR_TARGET)
2593 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002594 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595 }
2596 else
2597 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002598 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599 }
2600
2601 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2602 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002603 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2604 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2605 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2606 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2607 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2608 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2609 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2610 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002611 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002612 }
2613 else
2614 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002615 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 }
2617
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002618 mDevice->SetPixelShader(NULL);
2619 mDevice->SetVertexShader(NULL);
2620 mDevice->SetFVF(D3DFVF_XYZRHW);
2621 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2622 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2623 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2624 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2625 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2626 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2627 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002628
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002629 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2630 quad[0][0] = -0.5f;
2631 quad[0][1] = desc.Height - 0.5f;
2632 quad[0][2] = 0.0f;
2633 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002635 quad[1][0] = desc.Width - 0.5f;
2636 quad[1][1] = desc.Height - 0.5f;
2637 quad[1][2] = 0.0f;
2638 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002639
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002640 quad[2][0] = -0.5f;
2641 quad[2][1] = -0.5f;
2642 quad[2][2] = 0.0f;
2643 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002645 quad[3][0] = desc.Width - 0.5f;
2646 quad[3][1] = -0.5f;
2647 quad[3][2] = 0.0f;
2648 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002650 mDisplay->startScene();
2651 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002652
2653 if (flags & D3DCLEAR_ZBUFFER)
2654 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002655 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2656 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2657 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002658 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002659
2660 if (mMaskedClearSavedState != NULL)
2661 {
2662 mMaskedClearSavedState->Apply();
2663 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002664 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002665 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002667 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002668 }
2669}
2670
2671void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2672{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002673 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002674 {
2675 return error(GL_INVALID_OPERATION);
2676 }
2677
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002678 D3DPRIMITIVETYPE primitiveType;
2679 int primitiveCount;
2680
2681 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2682 return error(GL_INVALID_ENUM);
2683
2684 if (primitiveCount <= 0)
2685 {
2686 return;
2687 }
2688
2689 if (!applyRenderTarget(false))
2690 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002691 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 }
2693
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002694 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002695
daniel@transgaming.com83921382011-01-08 05:46:00 +00002696 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002697 if (err != GL_NO_ERROR)
2698 {
2699 return error(err);
2700 }
2701
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702 applyShaders();
2703 applyTextures();
2704
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002705 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002706 {
2707 return error(GL_INVALID_OPERATION);
2708 }
2709
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002710 if (!cullSkipsDraw(mode))
2711 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002712 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002713
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002714 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002715
2716 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2717 {
2718 drawClosingLine(first, first + count - 1);
2719 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002720 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721}
2722
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002723void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002725 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726 {
2727 return error(GL_INVALID_OPERATION);
2728 }
2729
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002730 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731 {
2732 return error(GL_INVALID_OPERATION);
2733 }
2734
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735 D3DPRIMITIVETYPE primitiveType;
2736 int primitiveCount;
2737
2738 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2739 return error(GL_INVALID_ENUM);
2740
2741 if (primitiveCount <= 0)
2742 {
2743 return;
2744 }
2745
2746 if (!applyRenderTarget(false))
2747 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002748 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002749 }
2750
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002751 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002752
2753 TranslatedIndexData indexInfo;
2754 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2755 if (err != GL_NO_ERROR)
2756 {
2757 return error(err);
2758 }
2759
daniel@transgaming.com83921382011-01-08 05:46:00 +00002760 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2761 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002762 if (err != GL_NO_ERROR)
2763 {
2764 return error(err);
2765 }
2766
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002767 applyShaders();
2768 applyTextures();
2769
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002770 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002771 {
2772 return error(GL_INVALID_OPERATION);
2773 }
2774
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002775 if (!cullSkipsDraw(mode))
2776 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002777 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002778
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002779 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002780
2781 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2782 {
2783 drawClosingLine(count, type, indices);
2784 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002785 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786}
2787
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002788// Implements glFlush when block is false, glFinish when block is true
2789void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002790{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002791 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002792 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002793
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002794 result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002795 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002797 ERR("CreateQuery failed hr=%x\n", result);
2798 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2799 {
2800 return error(GL_OUT_OF_MEMORY);
2801 }
2802 ASSERT(false);
2803 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804 }
2805
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002806 result = eventQuery->Issue(D3DISSUE_END);
2807 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002809 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2810 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002811 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002812 return;
2813 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002814
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002815 do
2816 {
2817 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2818
2819 if(block && result == S_FALSE)
2820 {
2821 // Keep polling, but allow other threads to do something useful first
2822 Sleep(0);
2823 }
2824 }
2825 while(block && result == S_FALSE);
2826
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002827 eventQuery->Release();
2828
2829 if (result == D3DERR_DEVICELOST)
2830 {
2831 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002832 }
2833}
2834
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002835void Context::drawClosingLine(unsigned int first, unsigned int last)
2836{
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002837 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002838 bool succeeded = false;
2839 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002840
2841 if (supports32bitIndices())
2842 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002843 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002844
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002845 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002846 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002847 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002848 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002849
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002850 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2851
2852 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2853 if (data)
2854 {
2855 data[0] = last;
2856 data[1] = first;
2857 mClosingIB->unmap();
2858 offset /= 4;
2859 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002860 }
2861 }
2862 else
2863 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002864 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002865
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002866 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002867 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002868 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002869 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002870
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002871 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2872
2873 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2874 if (data)
2875 {
2876 data[0] = last;
2877 data[1] = first;
2878 mClosingIB->unmap();
2879 offset /= 2;
2880 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002881 }
2882 }
2883
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002884 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002885 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002886 mDevice->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002887 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002888
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002889 mDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002890 }
2891 else
2892 {
2893 ERR("Could not create an index buffer for closing a line loop.");
2894 error(GL_OUT_OF_MEMORY);
2895 }
2896}
2897
2898void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2899{
2900 unsigned int first = 0;
2901 unsigned int last = 0;
2902
2903 if (mState.elementArrayBuffer.get())
2904 {
2905 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2906 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2907 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2908 }
2909
2910 switch (type)
2911 {
2912 case GL_UNSIGNED_BYTE:
2913 first = static_cast<const GLubyte*>(indices)[0];
2914 last = static_cast<const GLubyte*>(indices)[count - 1];
2915 break;
2916 case GL_UNSIGNED_SHORT:
2917 first = static_cast<const GLushort*>(indices)[0];
2918 last = static_cast<const GLushort*>(indices)[count - 1];
2919 break;
2920 case GL_UNSIGNED_INT:
2921 first = static_cast<const GLuint*>(indices)[0];
2922 last = static_cast<const GLuint*>(indices)[count - 1];
2923 break;
2924 default: UNREACHABLE();
2925 }
2926
2927 drawClosingLine(first, last);
2928}
2929
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002930void Context::recordInvalidEnum()
2931{
2932 mInvalidEnum = true;
2933}
2934
2935void Context::recordInvalidValue()
2936{
2937 mInvalidValue = true;
2938}
2939
2940void Context::recordInvalidOperation()
2941{
2942 mInvalidOperation = true;
2943}
2944
2945void Context::recordOutOfMemory()
2946{
2947 mOutOfMemory = true;
2948}
2949
2950void Context::recordInvalidFramebufferOperation()
2951{
2952 mInvalidFramebufferOperation = true;
2953}
2954
2955// Get one of the recorded errors and clear its flag, if any.
2956// [OpenGL ES 2.0.24] section 2.5 page 13.
2957GLenum Context::getError()
2958{
2959 if (mInvalidEnum)
2960 {
2961 mInvalidEnum = false;
2962
2963 return GL_INVALID_ENUM;
2964 }
2965
2966 if (mInvalidValue)
2967 {
2968 mInvalidValue = false;
2969
2970 return GL_INVALID_VALUE;
2971 }
2972
2973 if (mInvalidOperation)
2974 {
2975 mInvalidOperation = false;
2976
2977 return GL_INVALID_OPERATION;
2978 }
2979
2980 if (mOutOfMemory)
2981 {
2982 mOutOfMemory = false;
2983
2984 return GL_OUT_OF_MEMORY;
2985 }
2986
2987 if (mInvalidFramebufferOperation)
2988 {
2989 mInvalidFramebufferOperation = false;
2990
2991 return GL_INVALID_FRAMEBUFFER_OPERATION;
2992 }
2993
2994 return GL_NO_ERROR;
2995}
2996
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002997bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002998{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002999 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003000}
3001
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003002int Context::getMaximumVaryingVectors() const
3003{
3004 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3005}
3006
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003007unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003008{
3009 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3010}
3011
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003012unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003013{
3014 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3015}
3016
daniel@transgaming.com458da142010-11-28 02:03:02 +00003017int Context::getMaximumFragmentUniformVectors() const
3018{
3019 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3020}
3021
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003022int Context::getMaxSupportedSamples() const
3023{
3024 return mMaxSupportedSamples;
3025}
3026
3027int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3028{
3029 if (requested == 0)
3030 {
3031 return requested;
3032 }
3033
3034 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3035 if (itr == mMultiSampleSupport.end())
3036 {
3037 return -1;
3038 }
3039
3040 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3041 {
3042 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3043 {
3044 return i;
3045 }
3046 }
3047
3048 return -1;
3049}
3050
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003051bool Context::supportsEventQueries() const
3052{
3053 return mSupportsEventQueries;
3054}
3055
gman@chromium.org50c526d2011-08-10 05:19:44 +00003056bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003057{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003058 return mSupportsDXT1Textures;
3059}
3060
3061bool Context::supportsDXT3Textures() const
3062{
3063 return mSupportsDXT3Textures;
3064}
3065
3066bool Context::supportsDXT5Textures() const
3067{
3068 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003069}
3070
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003071bool Context::supportsFloatTextures() const
3072{
3073 return mSupportsFloatTextures;
3074}
3075
3076bool Context::supportsFloatLinearFilter() const
3077{
3078 return mSupportsFloatLinearFilter;
3079}
3080
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003081bool Context::supportsFloatRenderableTextures() const
3082{
3083 return mSupportsFloatRenderableTextures;
3084}
3085
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003086bool Context::supportsHalfFloatTextures() const
3087{
3088 return mSupportsHalfFloatTextures;
3089}
3090
3091bool Context::supportsHalfFloatLinearFilter() const
3092{
3093 return mSupportsHalfFloatLinearFilter;
3094}
3095
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003096bool Context::supportsHalfFloatRenderableTextures() const
3097{
3098 return mSupportsHalfFloatRenderableTextures;
3099}
3100
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003101int Context::getMaximumRenderbufferDimension() const
3102{
3103 return mMaxRenderbufferDimension;
3104}
3105
3106int Context::getMaximumTextureDimension() const
3107{
3108 return mMaxTextureDimension;
3109}
3110
3111int Context::getMaximumCubeTextureDimension() const
3112{
3113 return mMaxCubeTextureDimension;
3114}
3115
3116int Context::getMaximumTextureLevel() const
3117{
3118 return mMaxTextureLevel;
3119}
3120
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003121bool Context::supportsLuminanceTextures() const
3122{
3123 return mSupportsLuminanceTextures;
3124}
3125
3126bool Context::supportsLuminanceAlphaTextures() const
3127{
3128 return mSupportsLuminanceAlphaTextures;
3129}
3130
daniel@transgaming.com83921382011-01-08 05:46:00 +00003131bool Context::supports32bitIndices() const
3132{
3133 return mSupports32bitIndices;
3134}
3135
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003136bool Context::supportsNonPower2Texture() const
3137{
3138 return mSupportsNonPower2Texture;
3139}
3140
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003141void Context::detachBuffer(GLuint buffer)
3142{
3143 // [OpenGL ES 2.0.24] section 2.9 page 22:
3144 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3145 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3146
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003147 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003148 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003149 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003150 }
3151
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003152 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003153 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003154 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003155 }
3156
3157 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3158 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003159 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003160 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003161 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003162 }
3163 }
3164}
3165
3166void Context::detachTexture(GLuint texture)
3167{
3168 // [OpenGL ES 2.0.24] section 3.8 page 84:
3169 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3170 // rebound to texture object zero
3171
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003172 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003173 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003174 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003175 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003176 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003177 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003178 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003180 }
3181 }
3182
3183 // [OpenGL ES 2.0.24] section 4.4 page 112:
3184 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3185 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3186 // image was attached in the currently bound framebuffer.
3187
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003188 Framebuffer *readFramebuffer = getReadFramebuffer();
3189 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003190
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003191 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003192 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003193 readFramebuffer->detachTexture(texture);
3194 }
3195
3196 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3197 {
3198 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003199 }
3200}
3201
3202void Context::detachFramebuffer(GLuint framebuffer)
3203{
3204 // [OpenGL ES 2.0.24] section 4.4 page 107:
3205 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3206 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3207
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003208 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003209 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003210 bindReadFramebuffer(0);
3211 }
3212
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003213 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003214 {
3215 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003216 }
3217}
3218
3219void Context::detachRenderbuffer(GLuint renderbuffer)
3220{
3221 // [OpenGL ES 2.0.24] section 4.4 page 109:
3222 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3223 // had been executed with the target RENDERBUFFER and name of zero.
3224
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003225 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003226 {
3227 bindRenderbuffer(0);
3228 }
3229
3230 // [OpenGL ES 2.0.24] section 4.4 page 111:
3231 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3232 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3233 // point to which this image was attached in the currently bound framebuffer.
3234
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003235 Framebuffer *readFramebuffer = getReadFramebuffer();
3236 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003237
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003238 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003239 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003240 readFramebuffer->detachRenderbuffer(renderbuffer);
3241 }
3242
3243 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3244 {
3245 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003246 }
3247}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003248
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003249Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003250{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003251 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003252
3253 if (t == NULL)
3254 {
3255 static const GLubyte color[] = { 0, 0, 0, 255 };
3256
3257 switch (type)
3258 {
3259 default:
3260 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003261 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003262
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003263 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003264 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003265 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003266 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003267 t = incomplete2d;
3268 }
3269 break;
3270
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003271 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003272 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003273 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003274
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003275 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3276 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3277 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3278 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3279 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3280 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003281
3282 t = incompleteCube;
3283 }
3284 break;
3285 }
3286
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003287 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003288 }
3289
3290 return t;
3291}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003292
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003293bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003294{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003295 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003296}
3297
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003298bool Context::isTriangleMode(GLenum drawMode)
3299{
3300 switch (drawMode)
3301 {
3302 case GL_TRIANGLES:
3303 case GL_TRIANGLE_FAN:
3304 case GL_TRIANGLE_STRIP:
3305 return true;
3306 case GL_POINTS:
3307 case GL_LINES:
3308 case GL_LINE_LOOP:
3309 case GL_LINE_STRIP:
3310 return false;
3311 default: UNREACHABLE();
3312 }
3313
3314 return false;
3315}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003316
3317void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3318{
3319 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3320
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003321 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3322 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3323 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3324 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003325
daniel@transgaming.com83921382011-01-08 05:46:00 +00003326 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003327}
3328
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003329// keep list sorted in following order
3330// OES extensions
3331// EXT extensions
3332// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003333void Context::initExtensionString()
3334{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003335 mExtensionString = "";
3336
3337 // OES extensions
3338 if (supports32bitIndices())
3339 {
3340 mExtensionString += "GL_OES_element_index_uint ";
3341 }
3342
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003343 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003344 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003345 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003346
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003347 if (supportsHalfFloatTextures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003348 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003349 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003350 }
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003351 if (supportsHalfFloatLinearFilter())
3352 {
3353 mExtensionString += "GL_OES_texture_half_float_linear ";
3354 }
3355 if (supportsFloatTextures())
3356 {
3357 mExtensionString += "GL_OES_texture_float ";
3358 }
3359 if (supportsFloatLinearFilter())
3360 {
3361 mExtensionString += "GL_OES_texture_float_linear ";
3362 }
3363
3364 if (supportsNonPower2Texture())
3365 {
3366 mExtensionString += "GL_OES_texture_npot ";
3367 }
3368
3369 // Multi-vendor (EXT) extensions
3370 mExtensionString += "GL_EXT_read_format_bgra ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003371
gman@chromium.org50c526d2011-08-10 05:19:44 +00003372 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003373 {
3374 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3375 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003376
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003377 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003378
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003379 // ANGLE-specific extensions
3380 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003381 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003382 {
3383 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3384 }
3385
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003386 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003387 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003388 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3389 }
3390 if (supportsDXT5Textures())
3391 {
3392 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003393 }
zmo@google.coma574f782011-10-03 21:45:23 +00003394 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003395
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003396 // Other vendor-specific extensions
3397 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003398 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003399 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003400 }
3401
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003402 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3403 if (end != std::string::npos)
3404 {
3405 mExtensionString.resize(end+1);
3406 }
3407}
3408
3409const char *Context::getExtensionString() const
3410{
3411 return mExtensionString.c_str();
3412}
3413
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003414void Context::initRendererString()
3415{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003416 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003417
3418 mRendererString = "ANGLE (";
3419 mRendererString += identifier->Description;
3420 mRendererString += ")";
3421}
3422
3423const char *Context::getRendererString() const
3424{
3425 return mRendererString.c_str();
3426}
3427
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003428void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3429 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3430 GLbitfield mask)
3431{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003432 Framebuffer *readFramebuffer = getReadFramebuffer();
3433 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3434
3435 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3436 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3437 {
3438 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3439 }
3440
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003441 if (drawFramebuffer->getSamples() != 0)
3442 {
3443 return error(GL_INVALID_OPERATION);
3444 }
3445
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003446 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3447 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3448 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3449 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3450
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003451 RECT sourceRect;
3452 RECT destRect;
3453
3454 if (srcX0 < srcX1)
3455 {
3456 sourceRect.left = srcX0;
3457 sourceRect.right = srcX1;
3458 destRect.left = dstX0;
3459 destRect.right = dstX1;
3460 }
3461 else
3462 {
3463 sourceRect.left = srcX1;
3464 destRect.left = dstX1;
3465 sourceRect.right = srcX0;
3466 destRect.right = dstX0;
3467 }
3468
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003469 if (srcY0 < srcY1)
3470 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003471 sourceRect.top = readBufferHeight - srcY1;
3472 destRect.top = drawBufferHeight - dstY1;
3473 sourceRect.bottom = readBufferHeight - srcY0;
3474 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003475 }
3476 else
3477 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003478 sourceRect.top = readBufferHeight - srcY0;
3479 destRect.top = drawBufferHeight - dstY0;
3480 sourceRect.bottom = readBufferHeight - srcY1;
3481 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003482 }
3483
3484 RECT sourceScissoredRect = sourceRect;
3485 RECT destScissoredRect = destRect;
3486
3487 if (mState.scissorTest)
3488 {
3489 // Only write to parts of the destination framebuffer which pass the scissor test
3490 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3491 // rect will be checked against scissorY, rather than the bottom.
3492 if (destRect.left < mState.scissorX)
3493 {
3494 int xDiff = mState.scissorX - destRect.left;
3495 destScissoredRect.left = mState.scissorX;
3496 sourceScissoredRect.left += xDiff;
3497 }
3498
3499 if (destRect.right > mState.scissorX + mState.scissorWidth)
3500 {
3501 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3502 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3503 sourceScissoredRect.right -= xDiff;
3504 }
3505
3506 if (destRect.top < mState.scissorY)
3507 {
3508 int yDiff = mState.scissorY - destRect.top;
3509 destScissoredRect.top = mState.scissorY;
3510 sourceScissoredRect.top += yDiff;
3511 }
3512
3513 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3514 {
3515 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3516 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3517 sourceScissoredRect.bottom -= yDiff;
3518 }
3519 }
3520
3521 bool blitRenderTarget = false;
3522 bool blitDepthStencil = false;
3523
3524 RECT sourceTrimmedRect = sourceScissoredRect;
3525 RECT destTrimmedRect = destScissoredRect;
3526
3527 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3528 // the actual draw and read surfaces.
3529 if (sourceTrimmedRect.left < 0)
3530 {
3531 int xDiff = 0 - sourceTrimmedRect.left;
3532 sourceTrimmedRect.left = 0;
3533 destTrimmedRect.left += xDiff;
3534 }
3535
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003536 if (sourceTrimmedRect.right > readBufferWidth)
3537 {
3538 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3539 sourceTrimmedRect.right = readBufferWidth;
3540 destTrimmedRect.right -= xDiff;
3541 }
3542
3543 if (sourceTrimmedRect.top < 0)
3544 {
3545 int yDiff = 0 - sourceTrimmedRect.top;
3546 sourceTrimmedRect.top = 0;
3547 destTrimmedRect.top += yDiff;
3548 }
3549
3550 if (sourceTrimmedRect.bottom > readBufferHeight)
3551 {
3552 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3553 sourceTrimmedRect.bottom = readBufferHeight;
3554 destTrimmedRect.bottom -= yDiff;
3555 }
3556
3557 if (destTrimmedRect.left < 0)
3558 {
3559 int xDiff = 0 - destTrimmedRect.left;
3560 destTrimmedRect.left = 0;
3561 sourceTrimmedRect.left += xDiff;
3562 }
3563
3564 if (destTrimmedRect.right > drawBufferWidth)
3565 {
3566 int xDiff = destTrimmedRect.right - drawBufferWidth;
3567 destTrimmedRect.right = drawBufferWidth;
3568 sourceTrimmedRect.right -= xDiff;
3569 }
3570
3571 if (destTrimmedRect.top < 0)
3572 {
3573 int yDiff = 0 - destTrimmedRect.top;
3574 destTrimmedRect.top = 0;
3575 sourceTrimmedRect.top += yDiff;
3576 }
3577
3578 if (destTrimmedRect.bottom > drawBufferHeight)
3579 {
3580 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3581 destTrimmedRect.bottom = drawBufferHeight;
3582 sourceTrimmedRect.bottom -= yDiff;
3583 }
3584
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003585 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003586 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3587 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3588 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3589 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003590 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3591 {
3592 partialBufferCopy = true;
3593 }
3594
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003595 if (mask & GL_COLOR_BUFFER_BIT)
3596 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003597 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3598 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3599 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3600 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3601 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003602 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3603 {
3604 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3605 return error(GL_INVALID_OPERATION);
3606 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003607
3608 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3609 {
3610 return error(GL_INVALID_OPERATION);
3611 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003612
3613 blitRenderTarget = true;
3614
3615 }
3616
3617 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3618 {
3619 DepthStencilbuffer *readDSBuffer = NULL;
3620 DepthStencilbuffer *drawDSBuffer = NULL;
3621
3622 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3623 // both a depth and stencil buffer, it will be the same buffer.
3624
3625 if (mask & GL_DEPTH_BUFFER_BIT)
3626 {
3627 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3628 {
3629 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3630 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3631 {
3632 return error(GL_INVALID_OPERATION);
3633 }
3634
3635 blitDepthStencil = true;
3636 readDSBuffer = readFramebuffer->getDepthbuffer();
3637 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3638 }
3639 }
3640
3641 if (mask & GL_STENCIL_BUFFER_BIT)
3642 {
3643 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3644 {
3645 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3646 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3647 {
3648 return error(GL_INVALID_OPERATION);
3649 }
3650
3651 blitDepthStencil = true;
3652 readDSBuffer = readFramebuffer->getStencilbuffer();
3653 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3654 }
3655 }
3656
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003657 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003658 {
3659 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3660 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3661 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003662
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003663 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3664 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003665 {
3666 return error(GL_INVALID_OPERATION);
3667 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003668 }
3669
3670 if (blitRenderTarget || blitDepthStencil)
3671 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003672 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003673
3674 if (blitRenderTarget)
3675 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003676 HRESULT result = mDevice->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003677 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3678
3679 if (FAILED(result))
3680 {
3681 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3682 return;
3683 }
3684 }
3685
3686 if (blitDepthStencil)
3687 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003688 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003689
3690 if (FAILED(result))
3691 {
3692 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3693 return;
3694 }
3695 }
3696 }
3697}
3698
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003699VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3700{
3701 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3702 {
3703 mVertexDeclCache[i].vertexDeclaration = NULL;
3704 mVertexDeclCache[i].lruCount = 0;
3705 }
3706}
3707
3708VertexDeclarationCache::~VertexDeclarationCache()
3709{
3710 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3711 {
3712 if (mVertexDeclCache[i].vertexDeclaration)
3713 {
3714 mVertexDeclCache[i].vertexDeclaration->Release();
3715 }
3716 }
3717}
3718
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003719GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003720{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003721 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3722 D3DVERTEXELEMENT9 *element = &elements[0];
3723
3724 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3725 {
3726 if (attributes[i].active)
3727 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003728 if (mAppliedVBs[i].serial != attributes[i].serial ||
3729 mAppliedVBs[i].stride != attributes[i].stride ||
3730 mAppliedVBs[i].offset != attributes[i].offset)
3731 {
3732 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3733 mAppliedVBs[i].serial = attributes[i].serial;
3734 mAppliedVBs[i].stride = attributes[i].stride;
3735 mAppliedVBs[i].offset = attributes[i].offset;
3736 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003737
3738 element->Stream = i;
3739 element->Offset = 0;
3740 element->Type = attributes[i].type;
3741 element->Method = D3DDECLMETHOD_DEFAULT;
3742 element->Usage = D3DDECLUSAGE_TEXCOORD;
3743 element->UsageIndex = program->getSemanticIndex(i);
3744 element++;
3745 }
3746 }
3747
3748 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3749 *(element++) = end;
3750
3751 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3752 {
3753 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3754 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3755 {
3756 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003757 if(entry->vertexDeclaration != mLastSetVDecl)
3758 {
3759 device->SetVertexDeclaration(entry->vertexDeclaration);
3760 mLastSetVDecl = entry->vertexDeclaration;
3761 }
3762
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003763 return GL_NO_ERROR;
3764 }
3765 }
3766
3767 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3768
3769 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3770 {
3771 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3772 {
3773 lastCache = &mVertexDeclCache[i];
3774 }
3775 }
3776
3777 if (lastCache->vertexDeclaration != NULL)
3778 {
3779 lastCache->vertexDeclaration->Release();
3780 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003781 // mLastSetVDecl is set to the replacement, so we don't have to worry
3782 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003783 }
3784
3785 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3786 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3787 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003788 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003789 lastCache->lruCount = ++mMaxLru;
3790
3791 return GL_NO_ERROR;
3792}
3793
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003794void VertexDeclarationCache::markStateDirty()
3795{
3796 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3797 {
3798 mAppliedVBs[i].serial = 0;
3799 }
3800
3801 mLastSetVDecl = NULL;
3802}
3803
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003804}
3805
3806extern "C"
3807{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003808gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003809{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003810 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003811}
3812
3813void glDestroyContext(gl::Context *context)
3814{
3815 delete context;
3816
3817 if (context == gl::getContext())
3818 {
3819 gl::makeCurrent(NULL, NULL, NULL);
3820 }
3821}
3822
3823void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3824{
3825 gl::makeCurrent(context, display, surface);
3826}
3827
3828gl::Context *glGetCurrentContext()
3829{
3830 return gl::getContext();
3831}
3832}