blob: 77d0e91e8b32236cba319ac9633d8a53f0f535c7 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000028#include "libGLESv2/VertexDataManager.h"
29#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com86487c22010-03-11 19:41:43 +000031#undef near
32#undef far
33
jbauman@chromium.org399c35f2011-04-28 23:19:51 +000034namespace
35{
36 enum { CLOSING_INDEX_BUFFER_SIZE = 4096 };
37}
38
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039namespace gl
40{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +000041Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042{
benvanik@google.com1a233342011-04-28 19:44:39 +000043 mFenceHandleAllocator.setBaseHandle(0);
44
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000046
daniel@transgaming.com428d1582010-05-04 03:35:25 +000047 mState.depthClearValue = 1.0f;
48 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049
daniel@transgaming.com428d1582010-05-04 03:35:25 +000050 mState.cullFace = false;
51 mState.cullMode = GL_BACK;
52 mState.frontFace = GL_CCW;
53 mState.depthTest = false;
54 mState.depthFunc = GL_LESS;
55 mState.blend = false;
56 mState.sourceBlendRGB = GL_ONE;
57 mState.sourceBlendAlpha = GL_ONE;
58 mState.destBlendRGB = GL_ZERO;
59 mState.destBlendAlpha = GL_ZERO;
60 mState.blendEquationRGB = GL_FUNC_ADD;
61 mState.blendEquationAlpha = GL_FUNC_ADD;
62 mState.blendColor.red = 0;
63 mState.blendColor.green = 0;
64 mState.blendColor.blue = 0;
65 mState.blendColor.alpha = 0;
66 mState.stencilTest = false;
67 mState.stencilFunc = GL_ALWAYS;
68 mState.stencilRef = 0;
69 mState.stencilMask = -1;
70 mState.stencilWritemask = -1;
71 mState.stencilBackFunc = GL_ALWAYS;
72 mState.stencilBackRef = 0;
73 mState.stencilBackMask = - 1;
74 mState.stencilBackWritemask = -1;
75 mState.stencilFail = GL_KEEP;
76 mState.stencilPassDepthFail = GL_KEEP;
77 mState.stencilPassDepthPass = GL_KEEP;
78 mState.stencilBackFail = GL_KEEP;
79 mState.stencilBackPassDepthFail = GL_KEEP;
80 mState.stencilBackPassDepthPass = GL_KEEP;
81 mState.polygonOffsetFill = false;
82 mState.polygonOffsetFactor = 0.0f;
83 mState.polygonOffsetUnits = 0.0f;
84 mState.sampleAlphaToCoverage = false;
85 mState.sampleCoverage = false;
86 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000087 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.scissorTest = false;
89 mState.dither = true;
90 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000091 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092
daniel@transgaming.com428d1582010-05-04 03:35:25 +000093 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000094
daniel@transgaming.com428d1582010-05-04 03:35:25 +000095 mState.viewportX = 0;
96 mState.viewportY = 0;
97 mState.viewportWidth = config->mDisplayMode.Width;
98 mState.viewportHeight = config->mDisplayMode.Height;
99 mState.zNear = 0.0f;
100 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000102 mState.scissorX = 0;
103 mState.scissorY = 0;
104 mState.scissorWidth = config->mDisplayMode.Width;
105 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000107 mState.colorMaskRed = true;
108 mState.colorMaskGreen = true;
109 mState.colorMaskBlue = true;
110 mState.colorMaskAlpha = true;
111 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000113 if (shareContext != NULL)
114 {
115 mResourceManager = shareContext->mResourceManager;
116 mResourceManager->addRef();
117 }
118 else
119 {
120 mResourceManager = new ResourceManager();
121 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 // [OpenGL ES 2.0.24] section 3.7 page 83:
124 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
125 // and cube map texture state vectors respectively associated with them.
126 // In order that access to these initial textures not be lost, they are treated as texture
127 // objects all of whose names are 0.
128
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000129 mTexture2DZero.set(new Texture2D(0));
130 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000132 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000133 bindArrayBuffer(0);
134 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindTextureCubeMap(0);
136 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000137 bindReadFramebuffer(0);
138 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 bindRenderbuffer(0);
140
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000141 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.packAlignment = 4;
144 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000145
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000147 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000148 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000149 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000150
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 mInvalidEnum = false;
152 mInvalidValue = false;
153 mInvalidOperation = false;
154 mOutOfMemory = false;
155 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000156
157 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000158
gman@chromium.org50c526d2011-08-10 05:19:44 +0000159 mSupportsDXT1Textures = false;
160 mSupportsDXT3Textures = false;
161 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000162 mSupportsEventQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000163 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000164 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000165 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000166 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167}
168
169Context::~Context()
170{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000171 if (mState.currentProgram != 0)
172 {
173 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
174 if (programObject)
175 {
176 programObject->release();
177 }
178 mState.currentProgram = 0;
179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000181 while (!mFramebufferMap.empty())
182 {
183 deleteFramebuffer(mFramebufferMap.begin()->first);
184 }
185
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000186 while (!mFenceMap.empty())
187 {
188 deleteFence(mFenceMap.begin()->first);
189 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000190
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000191 while (!mMultiSampleSupport.empty())
192 {
193 delete [] mMultiSampleSupport.begin()->second;
194 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
195 }
196
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000197 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000198 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000199 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000200 {
201 mState.samplerTexture[type][sampler].set(NULL);
202 }
203 }
204
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000205 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000206 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000207 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000208 }
209
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
211 {
212 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
213 }
214
215 mState.arrayBuffer.set(NULL);
216 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000217 mState.renderbuffer.set(NULL);
218
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000219 mTexture2DZero.set(NULL);
220 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000222 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000223 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000224 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000225 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000226
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000227 if (mMaskedClearSavedState)
228 {
229 mMaskedClearSavedState->Release();
230 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000231
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000232 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233}
234
235void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
236{
237 IDirect3DDevice9 *device = display->getDevice();
238
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000239 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000240 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000241 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000242
daniel@transgaming.com83921382011-01-08 05:46:00 +0000243 mVertexDataManager = new VertexDataManager(this, device);
244 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000245 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000246
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000247 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +0000248 mSupportsVertexTexture = display->getVertexTextureSupport();
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000249 mSupportsNonPower2Texture = display->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000250
251 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
252 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
253 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
254 mMaxRenderbufferDimension = mMaxTextureDimension;
255 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
256 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
257 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
258
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000259 const D3DFORMAT renderBufferFormats[] =
260 {
261 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000262 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000263 D3DFMT_R5G6B5,
264 D3DFMT_D24S8
265 };
266
267 int max = 0;
268 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
269 {
270 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
271 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
272 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
273
274 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
275 {
276 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
277 {
278 max = j;
279 }
280 }
281 }
282
283 mMaxSupportedSamples = max;
284
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000285 mSupportsEventQueries = display->getEventQuerySupport();
gman@chromium.org50c526d2011-08-10 05:19:44 +0000286 mSupportsDXT1Textures = display->getDXT1TextureSupport();
287 mSupportsDXT3Textures = display->getDXT3TextureSupport();
288 mSupportsDXT5Textures = display->getDXT5TextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000289 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
290 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000291 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
292 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000293
daniel@transgaming.com83921382011-01-08 05:46:00 +0000294 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
295
gman@chromium.org50c526d2011-08-10 05:19:44 +0000296 mNumCompressedTextureFormats = 0;
297 if (supportsDXT1Textures())
298 {
299 mNumCompressedTextureFormats += 2;
300 }
301 if (supportsDXT3Textures())
302 {
303 mNumCompressedTextureFormats += 1;
304 }
305 if (supportsDXT5Textures())
306 {
307 mNumCompressedTextureFormats += 1;
308 }
309
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000310 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000311 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000312
313 mState.viewportX = 0;
314 mState.viewportY = 0;
315 mState.viewportWidth = surface->getWidth();
316 mState.viewportHeight = surface->getHeight();
317
318 mState.scissorX = 0;
319 mState.scissorY = 0;
320 mState.scissorWidth = surface->getWidth();
321 mState.scissorHeight = surface->getHeight();
322
323 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000324 }
325
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
327 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000328 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000331 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000332 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
334 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000336 if (defaultRenderTarget)
337 {
338 defaultRenderTarget->Release();
339 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000341 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000343 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000345
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000346 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347}
348
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000349// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000350void Context::markAllStateDirty()
351{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000352 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
353 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000354 mAppliedTextureSerialPS[t] = 0;
355 }
356
357 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
358 {
359 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000360 }
361
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000362 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000363 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000364 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000365 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000366 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000367 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000368 mViewportInitialized = false;
369 mRenderTargetDescInitialized = false;
370
371 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000372
373 mClearStateDirty = true;
374 mCullStateDirty = true;
375 mDepthStateDirty = true;
376 mMaskStateDirty = true;
377 mBlendStateDirty = true;
378 mStencilStateDirty = true;
379 mPolygonOffsetStateDirty = true;
380 mScissorStateDirty = true;
381 mSampleStateDirty = true;
382 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000383 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000384 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000385 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000386}
387
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388void Context::setClearColor(float red, float green, float blue, float alpha)
389{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000390 mState.colorClearValue.red = red;
391 mState.colorClearValue.green = green;
392 mState.colorClearValue.blue = blue;
393 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394}
395
396void Context::setClearDepth(float depth)
397{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000398 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399}
400
401void Context::setClearStencil(int stencil)
402{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000403 mState.stencilClearValue = stencil;
404}
405
406void Context::setCullFace(bool enabled)
407{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000408 if (mState.cullFace != enabled)
409 {
410 mState.cullFace = enabled;
411 mCullStateDirty = true;
412 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000413}
414
415bool Context::isCullFaceEnabled() const
416{
417 return mState.cullFace;
418}
419
420void Context::setCullMode(GLenum mode)
421{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000422 if (mState.cullMode != mode)
423 {
424 mState.cullMode = mode;
425 mCullStateDirty = true;
426 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000427}
428
429void Context::setFrontFace(GLenum front)
430{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000431 if (mState.frontFace != front)
432 {
433 mState.frontFace = front;
434 mFrontFaceDirty = true;
435 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000436}
437
438void Context::setDepthTest(bool enabled)
439{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000440 if (mState.depthTest != enabled)
441 {
442 mState.depthTest = enabled;
443 mDepthStateDirty = true;
444 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000445}
446
447bool Context::isDepthTestEnabled() const
448{
449 return mState.depthTest;
450}
451
452void Context::setDepthFunc(GLenum depthFunc)
453{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000454 if (mState.depthFunc != depthFunc)
455 {
456 mState.depthFunc = depthFunc;
457 mDepthStateDirty = true;
458 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000459}
460
461void Context::setDepthRange(float zNear, float zFar)
462{
463 mState.zNear = zNear;
464 mState.zFar = zFar;
465}
466
467void Context::setBlend(bool enabled)
468{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000469 if (mState.blend != enabled)
470 {
471 mState.blend = enabled;
472 mBlendStateDirty = true;
473 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000474}
475
476bool Context::isBlendEnabled() const
477{
478 return mState.blend;
479}
480
481void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
482{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000483 if (mState.sourceBlendRGB != sourceRGB ||
484 mState.sourceBlendAlpha != sourceAlpha ||
485 mState.destBlendRGB != destRGB ||
486 mState.destBlendAlpha != destAlpha)
487 {
488 mState.sourceBlendRGB = sourceRGB;
489 mState.destBlendRGB = destRGB;
490 mState.sourceBlendAlpha = sourceAlpha;
491 mState.destBlendAlpha = destAlpha;
492 mBlendStateDirty = true;
493 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000494}
495
496void Context::setBlendColor(float red, float green, float blue, float alpha)
497{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000498 if (mState.blendColor.red != red ||
499 mState.blendColor.green != green ||
500 mState.blendColor.blue != blue ||
501 mState.blendColor.alpha != alpha)
502 {
503 mState.blendColor.red = red;
504 mState.blendColor.green = green;
505 mState.blendColor.blue = blue;
506 mState.blendColor.alpha = alpha;
507 mBlendStateDirty = true;
508 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000509}
510
511void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
512{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000513 if (mState.blendEquationRGB != rgbEquation ||
514 mState.blendEquationAlpha != alphaEquation)
515 {
516 mState.blendEquationRGB = rgbEquation;
517 mState.blendEquationAlpha = alphaEquation;
518 mBlendStateDirty = true;
519 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000520}
521
522void Context::setStencilTest(bool enabled)
523{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000524 if (mState.stencilTest != enabled)
525 {
526 mState.stencilTest = enabled;
527 mStencilStateDirty = true;
528 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000529}
530
531bool Context::isStencilTestEnabled() const
532{
533 return mState.stencilTest;
534}
535
536void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
537{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000538 if (mState.stencilFunc != stencilFunc ||
539 mState.stencilRef != stencilRef ||
540 mState.stencilMask != stencilMask)
541 {
542 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000543 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000544 mState.stencilMask = stencilMask;
545 mStencilStateDirty = true;
546 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000547}
548
549void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
550{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000551 if (mState.stencilBackFunc != stencilBackFunc ||
552 mState.stencilBackRef != stencilBackRef ||
553 mState.stencilBackMask != stencilBackMask)
554 {
555 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000556 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000557 mState.stencilBackMask = stencilBackMask;
558 mStencilStateDirty = true;
559 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000560}
561
562void Context::setStencilWritemask(GLuint stencilWritemask)
563{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000564 if (mState.stencilWritemask != stencilWritemask)
565 {
566 mState.stencilWritemask = stencilWritemask;
567 mStencilStateDirty = true;
568 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000569}
570
571void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
572{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000573 if (mState.stencilBackWritemask != stencilBackWritemask)
574 {
575 mState.stencilBackWritemask = stencilBackWritemask;
576 mStencilStateDirty = true;
577 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000578}
579
580void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
581{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000582 if (mState.stencilFail != stencilFail ||
583 mState.stencilPassDepthFail != stencilPassDepthFail ||
584 mState.stencilPassDepthPass != stencilPassDepthPass)
585 {
586 mState.stencilFail = stencilFail;
587 mState.stencilPassDepthFail = stencilPassDepthFail;
588 mState.stencilPassDepthPass = stencilPassDepthPass;
589 mStencilStateDirty = true;
590 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.stencilBackFail != stencilBackFail ||
596 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
597 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
598 {
599 mState.stencilBackFail = stencilBackFail;
600 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
601 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
602 mStencilStateDirty = true;
603 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000604}
605
606void Context::setPolygonOffsetFill(bool enabled)
607{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000608 if (mState.polygonOffsetFill != enabled)
609 {
610 mState.polygonOffsetFill = enabled;
611 mPolygonOffsetStateDirty = true;
612 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000613}
614
615bool Context::isPolygonOffsetFillEnabled() const
616{
617 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000618
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000619}
620
621void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
622{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000623 if (mState.polygonOffsetFactor != factor ||
624 mState.polygonOffsetUnits != units)
625 {
626 mState.polygonOffsetFactor = factor;
627 mState.polygonOffsetUnits = units;
628 mPolygonOffsetStateDirty = true;
629 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000630}
631
632void Context::setSampleAlphaToCoverage(bool enabled)
633{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000634 if (mState.sampleAlphaToCoverage != enabled)
635 {
636 mState.sampleAlphaToCoverage = enabled;
637 mSampleStateDirty = true;
638 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000639}
640
641bool Context::isSampleAlphaToCoverageEnabled() const
642{
643 return mState.sampleAlphaToCoverage;
644}
645
646void Context::setSampleCoverage(bool enabled)
647{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000648 if (mState.sampleCoverage != enabled)
649 {
650 mState.sampleCoverage = enabled;
651 mSampleStateDirty = true;
652 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000653}
654
655bool Context::isSampleCoverageEnabled() const
656{
657 return mState.sampleCoverage;
658}
659
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000660void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000661{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000662 if (mState.sampleCoverageValue != value ||
663 mState.sampleCoverageInvert != invert)
664 {
665 mState.sampleCoverageValue = value;
666 mState.sampleCoverageInvert = invert;
667 mSampleStateDirty = true;
668 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000669}
670
671void Context::setScissorTest(bool enabled)
672{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000673 if (mState.scissorTest != enabled)
674 {
675 mState.scissorTest = enabled;
676 mScissorStateDirty = true;
677 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000678}
679
680bool Context::isScissorTestEnabled() const
681{
682 return mState.scissorTest;
683}
684
685void Context::setDither(bool enabled)
686{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000687 if (mState.dither != enabled)
688 {
689 mState.dither = enabled;
690 mDitherStateDirty = true;
691 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000692}
693
694bool Context::isDitherEnabled() const
695{
696 return mState.dither;
697}
698
699void Context::setLineWidth(GLfloat width)
700{
701 mState.lineWidth = width;
702}
703
704void Context::setGenerateMipmapHint(GLenum hint)
705{
706 mState.generateMipmapHint = hint;
707}
708
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000709void Context::setFragmentShaderDerivativeHint(GLenum hint)
710{
711 mState.fragmentShaderDerivativeHint = hint;
712 // TODO: Propagate the hint to shader translator so we can write
713 // ddx, ddx_coarse, or ddx_fine depending on the hint.
714 // Ignore for now. It is valid for implementations to ignore hint.
715}
716
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000717void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
718{
719 mState.viewportX = x;
720 mState.viewportY = y;
721 mState.viewportWidth = width;
722 mState.viewportHeight = height;
723}
724
725void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
726{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000727 if (mState.scissorX != x || mState.scissorY != y ||
728 mState.scissorWidth != width || mState.scissorHeight != height)
729 {
730 mState.scissorX = x;
731 mState.scissorY = y;
732 mState.scissorWidth = width;
733 mState.scissorHeight = height;
734 mScissorStateDirty = true;
735 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000736}
737
738void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
739{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000740 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
741 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
742 {
743 mState.colorMaskRed = red;
744 mState.colorMaskGreen = green;
745 mState.colorMaskBlue = blue;
746 mState.colorMaskAlpha = alpha;
747 mMaskStateDirty = true;
748 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749}
750
751void Context::setDepthMask(bool mask)
752{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000753 if (mState.depthMask != mask)
754 {
755 mState.depthMask = mask;
756 mMaskStateDirty = true;
757 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000758}
759
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000760void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000761{
762 mState.activeSampler = active;
763}
764
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000765GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000766{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000767 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000768}
769
770GLuint Context::getDrawFramebufferHandle() const
771{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000772 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000773}
774
775GLuint Context::getRenderbufferHandle() const
776{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000777 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000778}
779
780GLuint Context::getArrayBufferHandle() const
781{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000782 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000783}
784
daniel@transgaming.com83921382011-01-08 05:46:00 +0000785void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000786{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000787 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000788}
789
daniel@transgaming.com83921382011-01-08 05:46:00 +0000790const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000791{
792 return mState.vertexAttribute[attribNum];
793}
794
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000795void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000796 GLsizei stride, const void *pointer)
797{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000798 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000799 mState.vertexAttribute[attribNum].mSize = size;
800 mState.vertexAttribute[attribNum].mType = type;
801 mState.vertexAttribute[attribNum].mNormalized = normalized;
802 mState.vertexAttribute[attribNum].mStride = stride;
803 mState.vertexAttribute[attribNum].mPointer = pointer;
804}
805
806const void *Context::getVertexAttribPointer(unsigned int attribNum) const
807{
808 return mState.vertexAttribute[attribNum].mPointer;
809}
810
daniel@transgaming.com83921382011-01-08 05:46:00 +0000811const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000812{
813 return mState.vertexAttribute;
814}
815
816void Context::setPackAlignment(GLint alignment)
817{
818 mState.packAlignment = alignment;
819}
820
821GLint Context::getPackAlignment() const
822{
823 return mState.packAlignment;
824}
825
826void Context::setUnpackAlignment(GLint alignment)
827{
828 mState.unpackAlignment = alignment;
829}
830
831GLint Context::getUnpackAlignment() const
832{
833 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834}
835
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836GLuint Context::createBuffer()
837{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000838 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839}
840
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841GLuint Context::createProgram()
842{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000843 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844}
845
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000846GLuint Context::createShader(GLenum type)
847{
848 return mResourceManager->createShader(type);
849}
850
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851GLuint Context::createTexture()
852{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000853 return mResourceManager->createTexture();
854}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000856GLuint Context::createRenderbuffer()
857{
858 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000861// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862GLuint Context::createFramebuffer()
863{
benvanik@google.com1a233342011-04-28 19:44:39 +0000864 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000865
866 mFramebufferMap[handle] = NULL;
867
868 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869}
870
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000871GLuint Context::createFence()
872{
benvanik@google.com1a233342011-04-28 19:44:39 +0000873 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000874
875 mFenceMap[handle] = new Fence;
876
877 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000878}
879
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880void Context::deleteBuffer(GLuint buffer)
881{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000882 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883 {
884 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000886
887 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888}
889
890void Context::deleteShader(GLuint shader)
891{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000892 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893}
894
895void Context::deleteProgram(GLuint program)
896{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000897 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000898 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
900
901void Context::deleteTexture(GLuint texture)
902{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000903 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904 {
905 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907
908 mResourceManager->deleteTexture(texture);
909}
910
911void Context::deleteRenderbuffer(GLuint renderbuffer)
912{
913 if (mResourceManager->getRenderbuffer(renderbuffer))
914 {
915 detachRenderbuffer(renderbuffer);
916 }
917
918 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
921void Context::deleteFramebuffer(GLuint framebuffer)
922{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000923 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
924
925 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926 {
927 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000928
benvanik@google.com1a233342011-04-28 19:44:39 +0000929 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000930 delete framebufferObject->second;
931 mFramebufferMap.erase(framebufferObject);
932 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000934
935void Context::deleteFence(GLuint fence)
936{
937 FenceMap::iterator fenceObject = mFenceMap.find(fence);
938
939 if (fenceObject != mFenceMap.end())
940 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000941 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000942 delete fenceObject->second;
943 mFenceMap.erase(fenceObject);
944 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000945}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000947Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000949 return mResourceManager->getBuffer(handle);
950}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000952Shader *Context::getShader(GLuint handle)
953{
954 return mResourceManager->getShader(handle);
955}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000957Program *Context::getProgram(GLuint handle)
958{
959 return mResourceManager->getProgram(handle);
960}
961
962Texture *Context::getTexture(GLuint handle)
963{
964 return mResourceManager->getTexture(handle);
965}
966
967Renderbuffer *Context::getRenderbuffer(GLuint handle)
968{
969 return mResourceManager->getRenderbuffer(handle);
970}
971
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000972Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000973{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000974 return getFramebuffer(mState.readFramebuffer);
975}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000976
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000977Framebuffer *Context::getDrawFramebuffer()
978{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +0000979 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980}
981
982void Context::bindArrayBuffer(unsigned int buffer)
983{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000984 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000986 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987}
988
989void Context::bindElementArrayBuffer(unsigned int buffer)
990{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000991 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000993 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994}
995
996void Context::bindTexture2D(GLuint texture)
997{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000998 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001000 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001}
1002
1003void Context::bindTextureCubeMap(GLuint texture)
1004{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001005 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001007 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008}
1009
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001010void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001012 if (!getFramebuffer(framebuffer))
1013 {
1014 mFramebufferMap[framebuffer] = new Framebuffer();
1015 }
1016
1017 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001018}
1019
1020void Context::bindDrawFramebuffer(GLuint framebuffer)
1021{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001022 if (!getFramebuffer(framebuffer))
1023 {
1024 mFramebufferMap[framebuffer] = new Framebuffer();
1025 }
1026
1027 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001028
1029 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030}
1031
1032void Context::bindRenderbuffer(GLuint renderbuffer)
1033{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001034 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1035
1036 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037}
1038
1039void Context::useProgram(GLuint program)
1040{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001041 GLuint priorProgram = mState.currentProgram;
1042 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001043
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001044 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001046 Program *newProgram = mResourceManager->getProgram(program);
1047 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001048 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001049 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001050
1051 if (newProgram)
1052 {
1053 newProgram->addRef();
1054 }
1055
1056 if (oldProgram)
1057 {
1058 oldProgram->release();
1059 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061}
1062
1063void Context::setFramebufferZero(Framebuffer *buffer)
1064{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001065 delete mFramebufferMap[0];
1066 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001067 if (mState.drawFramebuffer == 0)
1068 {
1069 mBoundDrawFramebuffer = buffer;
1070 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071}
1072
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001073void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001075 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1076 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001079Framebuffer *Context::getFramebuffer(unsigned int handle)
1080{
1081 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1082
1083 if (framebuffer == mFramebufferMap.end())
1084 {
1085 return NULL;
1086 }
1087 else
1088 {
1089 return framebuffer->second;
1090 }
1091}
1092
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001093Fence *Context::getFence(unsigned int handle)
1094{
1095 FenceMap::iterator fence = mFenceMap.find(handle);
1096
1097 if (fence == mFenceMap.end())
1098 {
1099 return NULL;
1100 }
1101 else
1102 {
1103 return fence->second;
1104 }
1105}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001106
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107Buffer *Context::getArrayBuffer()
1108{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001109 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110}
1111
1112Buffer *Context::getElementArrayBuffer()
1113{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001114 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115}
1116
1117Program *Context::getCurrentProgram()
1118{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001119 if (!mCachedCurrentProgram)
1120 {
1121 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1122 }
1123 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124}
1125
1126Texture2D *Context::getTexture2D()
1127{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001128 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129}
1130
1131TextureCubeMap *Context::getTextureCubeMap()
1132{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001133 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134}
1135
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001136Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001137{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001138 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001139
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001140 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001141 {
1142 switch (type)
1143 {
1144 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001145 case TEXTURE_2D: return mTexture2DZero.get();
1146 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001147 }
1148 }
1149
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001150 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151}
1152
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001153bool Context::getBooleanv(GLenum pname, GLboolean *params)
1154{
1155 switch (pname)
1156 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001157 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1158 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1159 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001160 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 params[0] = mState.colorMaskRed;
1162 params[1] = mState.colorMaskGreen;
1163 params[2] = mState.colorMaskBlue;
1164 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001165 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001166 case GL_CULL_FACE: *params = mState.cullFace; break;
1167 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1168 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1169 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1170 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1171 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1172 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1173 case GL_BLEND: *params = mState.blend; break;
1174 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001175 default:
1176 return false;
1177 }
1178
1179 return true;
1180}
1181
1182bool Context::getFloatv(GLenum pname, GLfloat *params)
1183{
1184 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1185 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1186 // GetIntegerv as its native query function. As it would require conversion in any
1187 // case, this should make no difference to the calling application.
1188 switch (pname)
1189 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001190 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1191 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1192 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1193 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1194 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001195 case GL_ALIASED_LINE_WIDTH_RANGE:
1196 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1197 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1198 break;
1199 case GL_ALIASED_POINT_SIZE_RANGE:
1200 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001201 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 +00001202 break;
1203 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001204 params[0] = mState.zNear;
1205 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001206 break;
1207 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001208 params[0] = mState.colorClearValue.red;
1209 params[1] = mState.colorClearValue.green;
1210 params[2] = mState.colorClearValue.blue;
1211 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001212 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001213 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001214 params[0] = mState.blendColor.red;
1215 params[1] = mState.blendColor.green;
1216 params[2] = mState.blendColor.blue;
1217 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001218 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001219 default:
1220 return false;
1221 }
1222
1223 return true;
1224}
1225
1226bool Context::getIntegerv(GLenum pname, GLint *params)
1227{
1228 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1229 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1230 // GetIntegerv as its native query function. As it would require conversion in any
1231 // case, this should make no difference to the calling application. You may find it in
1232 // Context::getFloatv.
1233 switch (pname)
1234 {
1235 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1236 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001237 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001238 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1239 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001240 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001241 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001242 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001243 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001244 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001245 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1246 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001247 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1248 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1249 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001250 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001251 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1252 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1253 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1254 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001255 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001256 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1257 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1258 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1259 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1260 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1261 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1262 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1263 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1264 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1265 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1266 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1267 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1268 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1269 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1270 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1271 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1272 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1273 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1274 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1275 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1276 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1277 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1278 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1279 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001280 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1281 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001282 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001283 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001284 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001285 case GL_MAX_SAMPLES_ANGLE:
1286 {
1287 GLsizei maxSamples = getMaxSupportedSamples();
1288 if (maxSamples != 0)
1289 {
1290 *params = maxSamples;
1291 }
1292 else
1293 {
1294 return false;
1295 }
1296
1297 break;
1298 }
1299 case GL_SAMPLE_BUFFERS:
1300 case GL_SAMPLES:
1301 {
1302 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1303 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1304 {
1305 switch (pname)
1306 {
1307 case GL_SAMPLE_BUFFERS:
1308 if (framebuffer->getSamples() != 0)
1309 {
1310 *params = 1;
1311 }
1312 else
1313 {
1314 *params = 0;
1315 }
1316 break;
1317 case GL_SAMPLES:
1318 *params = framebuffer->getSamples();
1319 break;
1320 }
1321 }
1322 else
1323 {
1324 *params = 0;
1325 }
1326 }
1327 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001328 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1329 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1330 case GL_MAX_VIEWPORT_DIMS:
1331 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001332 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001333 params[0] = maxDimension;
1334 params[1] = maxDimension;
1335 }
1336 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001337 case GL_COMPRESSED_TEXTURE_FORMATS:
1338 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001339 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001340 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001341 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1342 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1343 }
1344 if (supportsDXT3Textures())
1345 {
1346 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1347 }
1348 if (supportsDXT5Textures())
1349 {
1350 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001351 }
1352 }
1353 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001354 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001355 params[0] = mState.viewportX;
1356 params[1] = mState.viewportY;
1357 params[2] = mState.viewportWidth;
1358 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359 break;
1360 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001361 params[0] = mState.scissorX;
1362 params[1] = mState.scissorY;
1363 params[2] = mState.scissorWidth;
1364 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001365 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001366 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1367 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001368 case GL_RED_BITS:
1369 case GL_GREEN_BITS:
1370 case GL_BLUE_BITS:
1371 case GL_ALPHA_BITS:
1372 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001373 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001374 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1375
1376 if (colorbuffer)
1377 {
1378 switch (pname)
1379 {
1380 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1381 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1382 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1383 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1384 }
1385 }
1386 else
1387 {
1388 *params = 0;
1389 }
1390 }
1391 break;
1392 case GL_DEPTH_BITS:
1393 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001394 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001395 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001396
1397 if (depthbuffer)
1398 {
1399 *params = depthbuffer->getDepthSize();
1400 }
1401 else
1402 {
1403 *params = 0;
1404 }
1405 }
1406 break;
1407 case GL_STENCIL_BITS:
1408 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001409 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001410 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001411
1412 if (stencilbuffer)
1413 {
1414 *params = stencilbuffer->getStencilSize();
1415 }
1416 else
1417 {
1418 *params = 0;
1419 }
1420 }
1421 break;
1422 case GL_TEXTURE_BINDING_2D:
1423 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001424 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001425 {
1426 error(GL_INVALID_OPERATION);
1427 return false;
1428 }
1429
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001430 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001431 }
1432 break;
1433 case GL_TEXTURE_BINDING_CUBE_MAP:
1434 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001435 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001436 {
1437 error(GL_INVALID_OPERATION);
1438 return false;
1439 }
1440
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001441 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001442 }
1443 break;
1444 default:
1445 return false;
1446 }
1447
1448 return true;
1449}
1450
1451bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1452{
1453 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1454 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1455 // to the fact that it is stored internally as a float, and so would require conversion
1456 // if returned from Context::getIntegerv. Since this conversion is already implemented
1457 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1458 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1459 // application.
1460 switch (pname)
1461 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001462 case GL_COMPRESSED_TEXTURE_FORMATS:
1463 {
1464 *type = GL_INT;
1465 *numParams = mNumCompressedTextureFormats;
1466 }
1467 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001468 case GL_SHADER_BINARY_FORMATS:
1469 {
1470 *type = GL_INT;
1471 *numParams = 0;
1472 }
1473 break;
1474 case GL_MAX_VERTEX_ATTRIBS:
1475 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1476 case GL_MAX_VARYING_VECTORS:
1477 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1478 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1479 case GL_MAX_TEXTURE_IMAGE_UNITS:
1480 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1481 case GL_MAX_RENDERBUFFER_SIZE:
1482 case GL_NUM_SHADER_BINARY_FORMATS:
1483 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1484 case GL_ARRAY_BUFFER_BINDING:
1485 case GL_FRAMEBUFFER_BINDING:
1486 case GL_RENDERBUFFER_BINDING:
1487 case GL_CURRENT_PROGRAM:
1488 case GL_PACK_ALIGNMENT:
1489 case GL_UNPACK_ALIGNMENT:
1490 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001491 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001492 case GL_RED_BITS:
1493 case GL_GREEN_BITS:
1494 case GL_BLUE_BITS:
1495 case GL_ALPHA_BITS:
1496 case GL_DEPTH_BITS:
1497 case GL_STENCIL_BITS:
1498 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1499 case GL_CULL_FACE_MODE:
1500 case GL_FRONT_FACE:
1501 case GL_ACTIVE_TEXTURE:
1502 case GL_STENCIL_FUNC:
1503 case GL_STENCIL_VALUE_MASK:
1504 case GL_STENCIL_REF:
1505 case GL_STENCIL_FAIL:
1506 case GL_STENCIL_PASS_DEPTH_FAIL:
1507 case GL_STENCIL_PASS_DEPTH_PASS:
1508 case GL_STENCIL_BACK_FUNC:
1509 case GL_STENCIL_BACK_VALUE_MASK:
1510 case GL_STENCIL_BACK_REF:
1511 case GL_STENCIL_BACK_FAIL:
1512 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1513 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1514 case GL_DEPTH_FUNC:
1515 case GL_BLEND_SRC_RGB:
1516 case GL_BLEND_SRC_ALPHA:
1517 case GL_BLEND_DST_RGB:
1518 case GL_BLEND_DST_ALPHA:
1519 case GL_BLEND_EQUATION_RGB:
1520 case GL_BLEND_EQUATION_ALPHA:
1521 case GL_STENCIL_WRITEMASK:
1522 case GL_STENCIL_BACK_WRITEMASK:
1523 case GL_STENCIL_CLEAR_VALUE:
1524 case GL_SUBPIXEL_BITS:
1525 case GL_MAX_TEXTURE_SIZE:
1526 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1527 case GL_SAMPLE_BUFFERS:
1528 case GL_SAMPLES:
1529 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1530 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1531 case GL_TEXTURE_BINDING_2D:
1532 case GL_TEXTURE_BINDING_CUBE_MAP:
1533 {
1534 *type = GL_INT;
1535 *numParams = 1;
1536 }
1537 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001538 case GL_MAX_SAMPLES_ANGLE:
1539 {
1540 if (getMaxSupportedSamples() != 0)
1541 {
1542 *type = GL_INT;
1543 *numParams = 1;
1544 }
1545 else
1546 {
1547 return false;
1548 }
1549 }
1550 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001551 case GL_MAX_VIEWPORT_DIMS:
1552 {
1553 *type = GL_INT;
1554 *numParams = 2;
1555 }
1556 break;
1557 case GL_VIEWPORT:
1558 case GL_SCISSOR_BOX:
1559 {
1560 *type = GL_INT;
1561 *numParams = 4;
1562 }
1563 break;
1564 case GL_SHADER_COMPILER:
1565 case GL_SAMPLE_COVERAGE_INVERT:
1566 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001567 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1568 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1569 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1570 case GL_SAMPLE_COVERAGE:
1571 case GL_SCISSOR_TEST:
1572 case GL_STENCIL_TEST:
1573 case GL_DEPTH_TEST:
1574 case GL_BLEND:
1575 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001576 {
1577 *type = GL_BOOL;
1578 *numParams = 1;
1579 }
1580 break;
1581 case GL_COLOR_WRITEMASK:
1582 {
1583 *type = GL_BOOL;
1584 *numParams = 4;
1585 }
1586 break;
1587 case GL_POLYGON_OFFSET_FACTOR:
1588 case GL_POLYGON_OFFSET_UNITS:
1589 case GL_SAMPLE_COVERAGE_VALUE:
1590 case GL_DEPTH_CLEAR_VALUE:
1591 case GL_LINE_WIDTH:
1592 {
1593 *type = GL_FLOAT;
1594 *numParams = 1;
1595 }
1596 break;
1597 case GL_ALIASED_LINE_WIDTH_RANGE:
1598 case GL_ALIASED_POINT_SIZE_RANGE:
1599 case GL_DEPTH_RANGE:
1600 {
1601 *type = GL_FLOAT;
1602 *numParams = 2;
1603 }
1604 break;
1605 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001606 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001607 {
1608 *type = GL_FLOAT;
1609 *numParams = 4;
1610 }
1611 break;
1612 default:
1613 return false;
1614 }
1615
1616 return true;
1617}
1618
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619// Applies the render target surface, depth stencil surface, viewport rectangle and
1620// scissor rectangle to the Direct3D 9 device
1621bool Context::applyRenderTarget(bool ignoreViewport)
1622{
1623 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001624
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001625 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626
1627 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1628 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001629 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630 }
1631
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001632 IDirect3DSurface9 *renderTarget = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001633 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001635 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001636 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1637 if (renderTargetSerial != mAppliedRenderTargetSerial)
1638 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001639 renderTarget = framebufferObject->getRenderTarget();
1640
1641 if (!renderTarget)
1642 {
1643 return false; // Context must be lost
1644 }
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001645 device->SetRenderTarget(0, renderTarget);
1646 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001647 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 +00001648 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001649 }
1650
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001651 unsigned int depthbufferSerial = 0;
1652 unsigned int stencilbufferSerial = 0;
1653 if (framebufferObject->getDepthbufferType() != GL_NONE)
1654 {
1655 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001656 if (!depthStencil)
1657 {
1658 ERR("Depth stencil pointer unexpectedly null.");
1659 return false;
1660 }
1661
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001662 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1663 }
1664 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1665 {
1666 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001667 if (!depthStencil)
1668 {
1669 ERR("Depth stencil pointer unexpectedly null.");
1670 return false;
1671 }
1672
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001673 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1674 }
1675
1676 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001677 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001678 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001679 {
1680 device->SetDepthStencilSurface(depthStencil);
1681 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001682 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001683 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001684 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001686 if (!mRenderTargetDescInitialized || renderTargetChanged)
1687 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001688 if (!renderTarget)
1689 {
1690 renderTarget = framebufferObject->getRenderTarget();
1691
1692 if (!renderTarget)
1693 {
1694 return false; // Context must be lost
1695 }
1696 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001697 renderTarget->GetDesc(&mRenderTargetDesc);
1698 mRenderTargetDescInitialized = true;
1699 }
1700
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001701 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001702
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001703 float zNear = clamp01(mState.zNear);
1704 float zFar = clamp01(mState.zFar);
1705
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706 if (ignoreViewport)
1707 {
1708 viewport.X = 0;
1709 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001710 viewport.Width = mRenderTargetDesc.Width;
1711 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712 viewport.MinZ = 0.0f;
1713 viewport.MaxZ = 1.0f;
1714 }
1715 else
1716 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001717 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1718 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1719 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1720 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1721 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 +00001722 viewport.MinZ = zNear;
1723 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724 }
1725
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001726 if (viewport.Width <= 0 || viewport.Height <= 0)
1727 {
1728 return false; // Nothing to render
1729 }
1730
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001731 if (!mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
1732 {
1733 device->SetViewport(&viewport);
1734 mSetViewport = viewport;
1735 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001736 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001737 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001739 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 if (mState.scissorTest)
1742 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001743 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1744 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1745 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1746 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1747 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 device->SetScissorRect(&rect);
1749 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1750 }
1751 else
1752 {
1753 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1754 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001755
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 }
1758
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001759 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761 Program *programObject = getCurrentProgram();
1762
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001763 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001764 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001765 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001766
daniel@transgaming.com31754962010-11-28 02:02:52 +00001767 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001768 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1769 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1770 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001771 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001772
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001773 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001774 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001775 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001776
daniel@transgaming.com31754962010-11-28 02:02:52 +00001777 GLint depthRange = programObject->getDxDepthRangeLocation();
1778 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1779 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001780 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781 }
1782
1783 return true;
1784}
1785
1786// 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 +00001787void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001788{
1789 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001790 Program *programObject = getCurrentProgram();
1791
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001792 Framebuffer *framebufferObject = getDrawFramebuffer();
1793
1794 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1795
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001796 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001797 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001798 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001799
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001800 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001801 GLint alwaysFront = !isTriangleMode(drawMode);
1802 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1803
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001804 egl::Display *display = getDisplay();
1805 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
1806 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 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001817 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818 }
1819 else
1820 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001821 device->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.coma79f9d12010-05-12 03:40:01 +00001831 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1832 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833 }
1834 else
1835 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001836 device->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.coma79f9d12010-05-12 03:40:01 +00001852 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1853
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 {
1857 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1858 }
1859 else
1860 {
1861 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1862 unorm<8>(mState.blendColor.alpha),
1863 unorm<8>(mState.blendColor.alpha),
1864 unorm<8>(mState.blendColor.alpha)));
1865 }
1866
1867 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1868 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1869 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1870
1871 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1872 mState.destBlendRGB != mState.destBlendAlpha ||
1873 mState.blendEquationRGB != mState.blendEquationAlpha)
1874 {
1875 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1876
1877 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1878 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1879 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001880 }
1881 else
1882 {
1883 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1884 }
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.coma79f9d12010-05-12 03:40:01 +00001888 device->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 {
1898 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1899 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1900
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
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001917 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1918 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001919 es2dx::ConvertComparison(mState.stencilFunc));
1920
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001921 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1922 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001923
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001924 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001925 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001926 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001927 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001928 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001929 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1930
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001931 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1932 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001933 es2dx::ConvertComparison(mState.stencilBackFunc));
1934
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001935 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1936 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001937
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001938 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001939 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001940 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001941 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001942 device->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 {
1947 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1948 }
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.
1961 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1962 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1963
1964 device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1965 device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1966 device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
1967 }
1968 else
1969 {
1970 device->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
1971 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001972 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1973
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 {
1984 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1985 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1986 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1987 }
1988 }
1989 else
1990 {
1991 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1992 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1993 }
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.com3203c102011-06-08 12:41:32 +00002005 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
2006 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
2030 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002031 }
2032 else
2033 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002034 device->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 {
2042 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
2043
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.com09c2c1a2011-04-13 14:57:16 +00002058 return mVertexDeclarationCache.applyDeclaration(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 IDirect3DDevice9 *device = getDevice();
2065 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002066
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002067 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002068 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002069 if (indexInfo->serial != mAppliedIBSerial)
2070 {
2071 device->SetIndices(indexInfo->indexBuffer);
2072 mAppliedIBSerial = indexInfo->serial;
2073 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002074 }
2075
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002076 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077}
2078
2079// Applies the shaders and shader constants to the Direct3D 9 device
2080void Context::applyShaders()
2081{
2082 IDirect3DDevice9 *device = getDevice();
2083 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002084 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002085 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002086 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2087 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2088
2089 device->SetPixelShader(pixelShader);
2090 device->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002091 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002092 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002093 }
2094
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095 programObject->applyUniforms();
2096}
2097
2098// Applies the textures and sampler states to the Direct3D 9 device
2099void Context::applyTextures()
2100{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002101 applyTextures(SAMPLER_PIXEL);
2102
2103 if (mSupportsVertexTexture)
2104 {
2105 applyTextures(SAMPLER_VERTEX);
2106 }
2107}
2108
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002109// For each Direct3D 9 sampler of either the pixel or vertex stage,
2110// looks up the corresponding OpenGL texture image unit and texture type,
2111// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002112void Context::applyTextures(SamplerType type)
2113{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114 IDirect3DDevice9 *device = getDevice();
2115 Program *programObject = getCurrentProgram();
2116
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002117 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 +00002118 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2119 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002120 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002121
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002122 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002124 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002125 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002126
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002127 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002129 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002130
2131 Texture *texture = getSamplerTexture(textureUnit, textureType);
2132
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002133 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002134 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002135 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2136
2137 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002138 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002139 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002140 {
2141 GLenum wrapS = texture->getWrapS();
2142 GLenum wrapT = texture->getWrapT();
2143 GLenum minFilter = texture->getMinFilter();
2144 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002146 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2147 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002149 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002150 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2151 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002152 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2153 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002154 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002156 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002157 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002158 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002159 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002160 }
2161 else
2162 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002163 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002164 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002165
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002166 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002167 texture->resetDirty();
2168 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002170 else
2171 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002172 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002173 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002174 device->SetTexture(d3dSampler, NULL);
2175 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002176 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002177 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002179
2180 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2181 {
2182 if (appliedTextureSerial[samplerIndex] != 0)
2183 {
2184 device->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
2185 appliedTextureSerial[samplerIndex] = 0;
2186 }
2187 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188}
2189
2190void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2191{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002192 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002193
2194 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2195 {
2196 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2197 }
2198
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002199 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2200 {
2201 return error(GL_INVALID_OPERATION);
2202 }
2203
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002204 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002205
2206 if (!renderTarget)
2207 {
2208 return; // Context must be lost, return silently
2209 }
2210
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211 IDirect3DDevice9 *device = getDevice();
2212
2213 D3DSURFACE_DESC desc;
2214 renderTarget->GetDesc(&desc);
2215
2216 IDirect3DSurface9 *systemSurface;
2217 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2218
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002219 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002221 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002222 return error(GL_OUT_OF_MEMORY);
2223 }
2224
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002225 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2226 {
2227 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002228 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002229 }
2230
2231 result = device->GetRenderTargetData(renderTarget, systemSurface);
2232
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233 if (FAILED(result))
2234 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002235 systemSurface->Release();
2236
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002237 switch (result)
2238 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002239 // It turns out that D3D will sometimes produce more error
2240 // codes than those documented.
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002241 case D3DERR_DRIVERINTERNALERROR:
2242 case D3DERR_DEVICELOST:
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002243 case D3DERR_DEVICEHUNG:
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002244 return error(GL_OUT_OF_MEMORY);
2245 default:
2246 UNREACHABLE();
2247 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002248 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002249 }
2250
2251 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002252 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2253 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2254 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2255 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2256 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257
2258 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2259
2260 if (FAILED(result))
2261 {
2262 UNREACHABLE();
2263 systemSurface->Release();
2264
2265 return; // No sensible error to generate
2266 }
2267
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002268 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002270 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002271 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002272 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002273
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 for (int j = 0; j < rect.bottom - rect.top; j++)
2275 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002276 if (desc.Format == D3DFMT_A8R8G8B8 &&
2277 format == GL_BGRA_EXT &&
2278 type == GL_UNSIGNED_BYTE)
2279 {
2280 // Fast path for EXT_read_format_bgra, given
2281 // an RGBA source buffer. Note that buffers with no
2282 // alpha go through the slow path below.
2283 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002284 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002285 (rect.right - rect.left) * 4);
2286 continue;
2287 }
2288
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 for (int i = 0; i < rect.right - rect.left; i++)
2290 {
2291 float r;
2292 float g;
2293 float b;
2294 float a;
2295
2296 switch (desc.Format)
2297 {
2298 case D3DFMT_R5G6B5:
2299 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002300 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
2302 a = 1.0f;
2303 b = (rgb & 0x001F) * (1.0f / 0x001F);
2304 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2305 r = (rgb & 0xF800) * (1.0f / 0xF800);
2306 }
2307 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308 case D3DFMT_A1R5G5B5:
2309 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002310 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311
2312 a = (argb & 0x8000) ? 1.0f : 0.0f;
2313 b = (argb & 0x001F) * (1.0f / 0x001F);
2314 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2315 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2316 }
2317 break;
2318 case D3DFMT_A8R8G8B8:
2319 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002320 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
2322 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2323 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2324 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2325 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2326 }
2327 break;
2328 case D3DFMT_X8R8G8B8:
2329 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002330 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331
2332 a = 1.0f;
2333 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2334 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2335 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2336 }
2337 break;
2338 case D3DFMT_A2R10G10B10:
2339 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002340 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341
2342 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2343 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2344 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2345 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2346 }
2347 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002348 case D3DFMT_A32B32G32R32F:
2349 {
2350 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002351 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2352 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2353 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2354 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002355 }
2356 break;
2357 case D3DFMT_A16B16G16R16F:
2358 {
2359 // float formats in D3D are stored rgba, rather than the other way round
2360 float abgr[4];
2361
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002362 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002363
2364 a = abgr[3];
2365 b = abgr[2];
2366 g = abgr[1];
2367 r = abgr[0];
2368 }
2369 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 default:
2371 UNIMPLEMENTED(); // FIXME
2372 UNREACHABLE();
2373 }
2374
2375 switch (format)
2376 {
2377 case GL_RGBA:
2378 switch (type)
2379 {
2380 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002381 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2382 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2383 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2384 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385 break;
2386 default: UNREACHABLE();
2387 }
2388 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002389 case GL_BGRA_EXT:
2390 switch (type)
2391 {
2392 case GL_UNSIGNED_BYTE:
2393 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2394 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2395 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2396 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2397 break;
2398 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2399 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2400 // this type is packed as follows:
2401 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2402 // --------------------------------------------------------------------------------
2403 // | 4th | 3rd | 2nd | 1st component |
2404 // --------------------------------------------------------------------------------
2405 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2406 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2407 ((unsigned short)(15 * a + 0.5f) << 12)|
2408 ((unsigned short)(15 * r + 0.5f) << 8) |
2409 ((unsigned short)(15 * g + 0.5f) << 4) |
2410 ((unsigned short)(15 * b + 0.5f) << 0);
2411 break;
2412 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2413 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2414 // this type is packed as follows:
2415 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2416 // --------------------------------------------------------------------------------
2417 // | 4th | 3rd | 2nd | 1st component |
2418 // --------------------------------------------------------------------------------
2419 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2420 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2421 ((unsigned short)( a + 0.5f) << 15) |
2422 ((unsigned short)(31 * r + 0.5f) << 10) |
2423 ((unsigned short)(31 * g + 0.5f) << 5) |
2424 ((unsigned short)(31 * b + 0.5f) << 0);
2425 break;
2426 default: UNREACHABLE();
2427 }
2428 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002429 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 switch (type)
2431 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002432 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002433 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2434 ((unsigned short)(31 * b + 0.5f) << 0) |
2435 ((unsigned short)(63 * g + 0.5f) << 5) |
2436 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437 break;
2438 default: UNREACHABLE();
2439 }
2440 break;
2441 default: UNREACHABLE();
2442 }
2443 }
2444 }
2445
2446 systemSurface->UnlockRect();
2447
2448 systemSurface->Release();
2449}
2450
2451void Context::clear(GLbitfield mask)
2452{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002453 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002454
2455 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2456 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002457 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002458 }
2459
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002460 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461 IDirect3DDevice9 *device = getDevice();
2462 DWORD flags = 0;
2463
2464 if (mask & GL_COLOR_BUFFER_BIT)
2465 {
2466 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002467
2468 if (framebufferObject->getColorbufferType() != GL_NONE)
2469 {
2470 flags |= D3DCLEAR_TARGET;
2471 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472 }
2473
2474 if (mask & GL_DEPTH_BUFFER_BIT)
2475 {
2476 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002477 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478 {
2479 flags |= D3DCLEAR_ZBUFFER;
2480 }
2481 }
2482
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483 GLuint stencilUnmasked = 0x0;
2484
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002485 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002486 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002488 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002490 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002491 if (!depthStencil)
2492 {
2493 ERR("Depth stencil pointer unexpectedly null.");
2494 return;
2495 }
2496
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002497 D3DSURFACE_DESC desc;
2498 depthStencil->GetDesc(&desc);
2499
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002500 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002501 stencilUnmasked = (0x1 << stencilSize) - 1;
2502
2503 if (stencilUnmasked != 0x0)
2504 {
2505 flags |= D3DCLEAR_STENCIL;
2506 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507 }
2508 }
2509
2510 if (mask != 0)
2511 {
2512 return error(GL_INVALID_VALUE);
2513 }
2514
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002515 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2516 {
2517 return;
2518 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002519
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002520 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002521 unorm<8>(mState.colorClearValue.red),
2522 unorm<8>(mState.colorClearValue.green),
2523 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002524 float depth = clamp01(mState.depthClearValue);
2525 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002526
2527 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2528
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002529 if (!renderTarget)
2530 {
2531 return; // Context must be lost, return silently
2532 }
2533
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534 D3DSURFACE_DESC desc;
2535 renderTarget->GetDesc(&desc);
2536
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002537 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538
2539 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002540 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002542 !(mState.colorMaskRed && mState.colorMaskGreen &&
2543 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544
2545 if (needMaskedColorClear || needMaskedStencilClear)
2546 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002547 // State which is altered in all paths from this point to the clear call is saved.
2548 // State which is altered in only some paths will be flagged dirty in the case that
2549 // that path is taken.
2550 HRESULT hr;
2551 if (mMaskedClearSavedState == NULL)
2552 {
2553 hr = device->BeginStateBlock();
2554 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2555
2556 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2557 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2558 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2559 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2560 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2561 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2562 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2563 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2564 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2565 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2566 device->SetPixelShader(NULL);
2567 device->SetVertexShader(NULL);
2568 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
jbauman@chromium.org23c9e312011-09-21 22:16:44 +00002569 device->SetStreamSource(0, NULL, 0, 0);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002570 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2571 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2572 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2573 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2574 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2575 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2576 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002577
2578 hr = device->EndStateBlock(&mMaskedClearSavedState);
2579 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2580 }
2581
2582 ASSERT(mMaskedClearSavedState != NULL);
2583
2584 if (mMaskedClearSavedState != NULL)
2585 {
2586 hr = mMaskedClearSavedState->Capture();
2587 ASSERT(SUCCEEDED(hr));
2588 }
2589
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002590 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2591 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2592 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2593 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2594 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2595 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2596 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2597 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2598
2599 if (flags & D3DCLEAR_TARGET)
2600 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002601 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602 }
2603 else
2604 {
2605 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2606 }
2607
2608 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2609 {
2610 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2611 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2612 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2613 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002614 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002615 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2617 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002618 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619 }
2620 else
2621 {
2622 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2623 }
2624
2625 device->SetPixelShader(NULL);
2626 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002627 device->SetFVF(D3DFVF_XYZRHW);
2628 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2629 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2630 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2631 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2632 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2633 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2634 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002636 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2637 quad[0][0] = -0.5f;
2638 quad[0][1] = desc.Height - 0.5f;
2639 quad[0][2] = 0.0f;
2640 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002642 quad[1][0] = desc.Width - 0.5f;
2643 quad[1][1] = desc.Height - 0.5f;
2644 quad[1][2] = 0.0f;
2645 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002647 quad[2][0] = -0.5f;
2648 quad[2][1] = -0.5f;
2649 quad[2][2] = 0.0f;
2650 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002652 quad[3][0] = desc.Width - 0.5f;
2653 quad[3][1] = -0.5f;
2654 quad[3][2] = 0.0f;
2655 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002657 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002658 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002659
2660 if (flags & D3DCLEAR_ZBUFFER)
2661 {
2662 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2663 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2664 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2665 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002666
2667 if (mMaskedClearSavedState != NULL)
2668 {
2669 mMaskedClearSavedState->Apply();
2670 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002672 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673 {
2674 device->Clear(0, NULL, flags, color, depth, stencil);
2675 }
2676}
2677
2678void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2679{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002680 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 {
2682 return error(GL_INVALID_OPERATION);
2683 }
2684
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002685 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686 IDirect3DDevice9 *device = getDevice();
2687 D3DPRIMITIVETYPE primitiveType;
2688 int primitiveCount;
2689
2690 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2691 return error(GL_INVALID_ENUM);
2692
2693 if (primitiveCount <= 0)
2694 {
2695 return;
2696 }
2697
2698 if (!applyRenderTarget(false))
2699 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002700 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002701 }
2702
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002703 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002704
daniel@transgaming.com83921382011-01-08 05:46:00 +00002705 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002706 if (err != GL_NO_ERROR)
2707 {
2708 return error(err);
2709 }
2710
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711 applyShaders();
2712 applyTextures();
2713
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002714 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002715 {
2716 return error(GL_INVALID_OPERATION);
2717 }
2718
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002719 if (!cullSkipsDraw(mode))
2720 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002721 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002722
2723 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002724
2725 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2726 {
2727 drawClosingLine(first, first + count - 1);
2728 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002729 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730}
2731
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002732void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002733{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002734 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735 {
2736 return error(GL_INVALID_OPERATION);
2737 }
2738
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002739 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740 {
2741 return error(GL_INVALID_OPERATION);
2742 }
2743
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002744 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002745 IDirect3DDevice9 *device = getDevice();
2746 D3DPRIMITIVETYPE primitiveType;
2747 int primitiveCount;
2748
2749 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2750 return error(GL_INVALID_ENUM);
2751
2752 if (primitiveCount <= 0)
2753 {
2754 return;
2755 }
2756
2757 if (!applyRenderTarget(false))
2758 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002759 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760 }
2761
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002762 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002763
2764 TranslatedIndexData indexInfo;
2765 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2766 if (err != GL_NO_ERROR)
2767 {
2768 return error(err);
2769 }
2770
daniel@transgaming.com83921382011-01-08 05:46:00 +00002771 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2772 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002773 if (err != GL_NO_ERROR)
2774 {
2775 return error(err);
2776 }
2777
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778 applyShaders();
2779 applyTextures();
2780
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002781 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002782 {
2783 return error(GL_INVALID_OPERATION);
2784 }
2785
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002786 if (!cullSkipsDraw(mode))
2787 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002788 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002789
daniel@transgaming.com83921382011-01-08 05:46:00 +00002790 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002791
2792 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2793 {
2794 drawClosingLine(count, type, indices);
2795 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002796 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002797}
2798
2799void Context::finish()
2800{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002801 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002802 IDirect3DDevice9 *device = getDevice();
2803 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002804 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002806 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2807 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002809 ERR("CreateQuery failed hr=%x\n", result);
2810 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2811 {
2812 return error(GL_OUT_OF_MEMORY);
2813 }
2814 ASSERT(false);
2815 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816 }
2817
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002818 IDirect3DStateBlock9 *savedState = NULL;
2819 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2820 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002821 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002822 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002823 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002824
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002825 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002826 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002827 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002828 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002829 ASSERT(false);
2830 return;
2831 }
2832
2833 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2834 if (FAILED(result))
2835 {
2836 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2837 occlusionQuery->Release();
2838 savedState->Release();
2839 ASSERT(false);
2840 return;
2841 }
2842
2843 // Render something outside the render target
2844 device->SetPixelShader(NULL);
2845 device->SetVertexShader(NULL);
2846 device->SetFVF(D3DFVF_XYZRHW);
2847 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2848 display->startScene();
2849 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2850
2851 result = occlusionQuery->Issue(D3DISSUE_END);
2852 if (FAILED(result))
2853 {
2854 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2855 occlusionQuery->Release();
2856 savedState->Apply();
2857 savedState->Release();
2858 ASSERT(false);
2859 return;
2860 }
2861
2862 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2863 {
2864 // Keep polling, but allow other threads to do something useful first
2865 Sleep(0);
2866 }
2867
2868 occlusionQuery->Release();
2869 savedState->Apply();
2870 savedState->Release();
2871
2872 if (result == D3DERR_DEVICELOST)
2873 {
2874 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002875 }
2876}
2877
2878void Context::flush()
2879{
2880 IDirect3DDevice9 *device = getDevice();
2881 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002882 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002884 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2885 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002886 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002887 ERR("CreateQuery failed hr=%x\n", result);
2888 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2889 {
2890 return error(GL_OUT_OF_MEMORY);
2891 }
2892 ASSERT(false);
2893 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002894 }
2895
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002896 result = eventQuery->Issue(D3DISSUE_END);
2897 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002898 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002899 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2900 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002901 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002902 return;
2903 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002904
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002905 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2906 eventQuery->Release();
2907
2908 if (result == D3DERR_DEVICELOST)
2909 {
2910 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002911 }
2912}
2913
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002914void Context::drawClosingLine(unsigned int first, unsigned int last)
2915{
2916 IDirect3DDevice9 *device = getDevice();
2917 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002918 bool succeeded = false;
2919 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002920
2921 if (supports32bitIndices())
2922 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002923 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002924
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002925 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002926 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002927 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2928 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002929
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002930 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2931
2932 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2933 if (data)
2934 {
2935 data[0] = last;
2936 data[1] = first;
2937 mClosingIB->unmap();
2938 offset /= 4;
2939 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002940 }
2941 }
2942 else
2943 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002944 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002945
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002946 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002947 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002948 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2949 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002950
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002951 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2952
2953 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2954 if (data)
2955 {
2956 data[0] = last;
2957 data[1] = first;
2958 mClosingIB->unmap();
2959 offset /= 2;
2960 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002961 }
2962 }
2963
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002964 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002965 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002966 device->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002967 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002968
jbauman@chromium.org2c199b12011-06-13 22:20:06 +00002969 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002970 }
2971 else
2972 {
2973 ERR("Could not create an index buffer for closing a line loop.");
2974 error(GL_OUT_OF_MEMORY);
2975 }
2976}
2977
2978void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2979{
2980 unsigned int first = 0;
2981 unsigned int last = 0;
2982
2983 if (mState.elementArrayBuffer.get())
2984 {
2985 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2986 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2987 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2988 }
2989
2990 switch (type)
2991 {
2992 case GL_UNSIGNED_BYTE:
2993 first = static_cast<const GLubyte*>(indices)[0];
2994 last = static_cast<const GLubyte*>(indices)[count - 1];
2995 break;
2996 case GL_UNSIGNED_SHORT:
2997 first = static_cast<const GLushort*>(indices)[0];
2998 last = static_cast<const GLushort*>(indices)[count - 1];
2999 break;
3000 case GL_UNSIGNED_INT:
3001 first = static_cast<const GLuint*>(indices)[0];
3002 last = static_cast<const GLuint*>(indices)[count - 1];
3003 break;
3004 default: UNREACHABLE();
3005 }
3006
3007 drawClosingLine(first, last);
3008}
3009
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003010void Context::recordInvalidEnum()
3011{
3012 mInvalidEnum = true;
3013}
3014
3015void Context::recordInvalidValue()
3016{
3017 mInvalidValue = true;
3018}
3019
3020void Context::recordInvalidOperation()
3021{
3022 mInvalidOperation = true;
3023}
3024
3025void Context::recordOutOfMemory()
3026{
3027 mOutOfMemory = true;
3028}
3029
3030void Context::recordInvalidFramebufferOperation()
3031{
3032 mInvalidFramebufferOperation = true;
3033}
3034
3035// Get one of the recorded errors and clear its flag, if any.
3036// [OpenGL ES 2.0.24] section 2.5 page 13.
3037GLenum Context::getError()
3038{
3039 if (mInvalidEnum)
3040 {
3041 mInvalidEnum = false;
3042
3043 return GL_INVALID_ENUM;
3044 }
3045
3046 if (mInvalidValue)
3047 {
3048 mInvalidValue = false;
3049
3050 return GL_INVALID_VALUE;
3051 }
3052
3053 if (mInvalidOperation)
3054 {
3055 mInvalidOperation = false;
3056
3057 return GL_INVALID_OPERATION;
3058 }
3059
3060 if (mOutOfMemory)
3061 {
3062 mOutOfMemory = false;
3063
3064 return GL_OUT_OF_MEMORY;
3065 }
3066
3067 if (mInvalidFramebufferOperation)
3068 {
3069 mInvalidFramebufferOperation = false;
3070
3071 return GL_INVALID_FRAMEBUFFER_OPERATION;
3072 }
3073
3074 return GL_NO_ERROR;
3075}
3076
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003077bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003078{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003079 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003080}
3081
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003082int Context::getMaximumVaryingVectors() const
3083{
3084 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3085}
3086
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003087unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003088{
3089 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3090}
3091
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003092unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003093{
3094 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3095}
3096
daniel@transgaming.com458da142010-11-28 02:03:02 +00003097int Context::getMaximumFragmentUniformVectors() const
3098{
3099 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3100}
3101
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003102int Context::getMaxSupportedSamples() const
3103{
3104 return mMaxSupportedSamples;
3105}
3106
3107int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3108{
3109 if (requested == 0)
3110 {
3111 return requested;
3112 }
3113
3114 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3115 if (itr == mMultiSampleSupport.end())
3116 {
3117 return -1;
3118 }
3119
3120 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3121 {
3122 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3123 {
3124 return i;
3125 }
3126 }
3127
3128 return -1;
3129}
3130
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003131bool Context::supportsEventQueries() const
3132{
3133 return mSupportsEventQueries;
3134}
3135
gman@chromium.org50c526d2011-08-10 05:19:44 +00003136bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003137{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003138 return mSupportsDXT1Textures;
3139}
3140
3141bool Context::supportsDXT3Textures() const
3142{
3143 return mSupportsDXT3Textures;
3144}
3145
3146bool Context::supportsDXT5Textures() const
3147{
3148 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003149}
3150
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003151bool Context::supportsFloatTextures() const
3152{
3153 return mSupportsFloatTextures;
3154}
3155
3156bool Context::supportsFloatLinearFilter() const
3157{
3158 return mSupportsFloatLinearFilter;
3159}
3160
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003161bool Context::supportsFloatRenderableTextures() const
3162{
3163 return mSupportsFloatRenderableTextures;
3164}
3165
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003166bool Context::supportsHalfFloatTextures() const
3167{
3168 return mSupportsHalfFloatTextures;
3169}
3170
3171bool Context::supportsHalfFloatLinearFilter() const
3172{
3173 return mSupportsHalfFloatLinearFilter;
3174}
3175
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003176bool Context::supportsHalfFloatRenderableTextures() const
3177{
3178 return mSupportsHalfFloatRenderableTextures;
3179}
3180
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003181int Context::getMaximumRenderbufferDimension() const
3182{
3183 return mMaxRenderbufferDimension;
3184}
3185
3186int Context::getMaximumTextureDimension() const
3187{
3188 return mMaxTextureDimension;
3189}
3190
3191int Context::getMaximumCubeTextureDimension() const
3192{
3193 return mMaxCubeTextureDimension;
3194}
3195
3196int Context::getMaximumTextureLevel() const
3197{
3198 return mMaxTextureLevel;
3199}
3200
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003201bool Context::supportsLuminanceTextures() const
3202{
3203 return mSupportsLuminanceTextures;
3204}
3205
3206bool Context::supportsLuminanceAlphaTextures() const
3207{
3208 return mSupportsLuminanceAlphaTextures;
3209}
3210
daniel@transgaming.com83921382011-01-08 05:46:00 +00003211bool Context::supports32bitIndices() const
3212{
3213 return mSupports32bitIndices;
3214}
3215
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003216bool Context::supportsNonPower2Texture() const
3217{
3218 return mSupportsNonPower2Texture;
3219}
3220
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003221void Context::detachBuffer(GLuint buffer)
3222{
3223 // [OpenGL ES 2.0.24] section 2.9 page 22:
3224 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3225 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3226
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003227 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003228 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003229 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003230 }
3231
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003232 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003233 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003234 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003235 }
3236
3237 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3238 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003239 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003240 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003241 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003242 }
3243 }
3244}
3245
3246void Context::detachTexture(GLuint texture)
3247{
3248 // [OpenGL ES 2.0.24] section 3.8 page 84:
3249 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3250 // rebound to texture object zero
3251
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003252 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003253 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003254 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003255 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003256 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003257 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003258 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003259 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003260 }
3261 }
3262
3263 // [OpenGL ES 2.0.24] section 4.4 page 112:
3264 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3265 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3266 // image was attached in the currently bound framebuffer.
3267
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003268 Framebuffer *readFramebuffer = getReadFramebuffer();
3269 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003270
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003271 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003272 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003273 readFramebuffer->detachTexture(texture);
3274 }
3275
3276 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3277 {
3278 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003279 }
3280}
3281
3282void Context::detachFramebuffer(GLuint framebuffer)
3283{
3284 // [OpenGL ES 2.0.24] section 4.4 page 107:
3285 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3286 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3287
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003288 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003289 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003290 bindReadFramebuffer(0);
3291 }
3292
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003293 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003294 {
3295 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003296 }
3297}
3298
3299void Context::detachRenderbuffer(GLuint renderbuffer)
3300{
3301 // [OpenGL ES 2.0.24] section 4.4 page 109:
3302 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3303 // had been executed with the target RENDERBUFFER and name of zero.
3304
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003305 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003306 {
3307 bindRenderbuffer(0);
3308 }
3309
3310 // [OpenGL ES 2.0.24] section 4.4 page 111:
3311 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3312 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3313 // point to which this image was attached in the currently bound framebuffer.
3314
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003315 Framebuffer *readFramebuffer = getReadFramebuffer();
3316 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003317
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003318 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003319 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003320 readFramebuffer->detachRenderbuffer(renderbuffer);
3321 }
3322
3323 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3324 {
3325 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003326 }
3327}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003328
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003329Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003330{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003331 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003332
3333 if (t == NULL)
3334 {
3335 static const GLubyte color[] = { 0, 0, 0, 255 };
3336
3337 switch (type)
3338 {
3339 default:
3340 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003341 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003342
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003343 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003344 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003345 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003346 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003347 t = incomplete2d;
3348 }
3349 break;
3350
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003351 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003352 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003353 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003354
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003355 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3356 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3357 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3358 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3359 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3360 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003361
3362 t = incompleteCube;
3363 }
3364 break;
3365 }
3366
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003367 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003368 }
3369
3370 return t;
3371}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003372
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003373bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003374{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003375 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003376}
3377
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003378bool Context::isTriangleMode(GLenum drawMode)
3379{
3380 switch (drawMode)
3381 {
3382 case GL_TRIANGLES:
3383 case GL_TRIANGLE_FAN:
3384 case GL_TRIANGLE_STRIP:
3385 return true;
3386 case GL_POINTS:
3387 case GL_LINES:
3388 case GL_LINE_LOOP:
3389 case GL_LINE_STRIP:
3390 return false;
3391 default: UNREACHABLE();
3392 }
3393
3394 return false;
3395}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003396
3397void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3398{
3399 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3400
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003401 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3402 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3403 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3404 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003405
daniel@transgaming.com83921382011-01-08 05:46:00 +00003406 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003407}
3408
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003409// keep list sorted in following order
3410// OES extensions
3411// EXT extensions
3412// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003413void Context::initExtensionString()
3414{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003415 mExtensionString = "";
3416
3417 // OES extensions
3418 if (supports32bitIndices())
3419 {
3420 mExtensionString += "GL_OES_element_index_uint ";
3421 }
3422
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003423 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003424 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003425 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003426
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003427 if (supportsHalfFloatTextures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003428 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003429 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003430 }
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003431 if (supportsHalfFloatLinearFilter())
3432 {
3433 mExtensionString += "GL_OES_texture_half_float_linear ";
3434 }
3435 if (supportsFloatTextures())
3436 {
3437 mExtensionString += "GL_OES_texture_float ";
3438 }
3439 if (supportsFloatLinearFilter())
3440 {
3441 mExtensionString += "GL_OES_texture_float_linear ";
3442 }
3443
3444 if (supportsNonPower2Texture())
3445 {
3446 mExtensionString += "GL_OES_texture_npot ";
3447 }
3448
3449 // Multi-vendor (EXT) extensions
3450 mExtensionString += "GL_EXT_read_format_bgra ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003451
gman@chromium.org50c526d2011-08-10 05:19:44 +00003452 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003453 {
3454 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3455 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003456
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003457 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003458
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003459 // ANGLE-specific extensions
3460 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003461 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003462 {
3463 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3464 }
3465
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003466 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003467 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003468 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3469 }
3470 if (supportsDXT5Textures())
3471 {
3472 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003473 }
zmo@google.coma574f782011-10-03 21:45:23 +00003474 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003475
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003476 // Other vendor-specific extensions
3477 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003478 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003479 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003480 }
3481
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003482 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3483 if (end != std::string::npos)
3484 {
3485 mExtensionString.resize(end+1);
3486 }
3487}
3488
3489const char *Context::getExtensionString() const
3490{
3491 return mExtensionString.c_str();
3492}
3493
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003494void Context::initRendererString()
3495{
3496 egl::Display *display = getDisplay();
3497 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
3498
3499 mRendererString = "ANGLE (";
3500 mRendererString += identifier->Description;
3501 mRendererString += ")";
3502}
3503
3504const char *Context::getRendererString() const
3505{
3506 return mRendererString.c_str();
3507}
3508
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003509void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3510 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3511 GLbitfield mask)
3512{
3513 IDirect3DDevice9 *device = getDevice();
3514
3515 Framebuffer *readFramebuffer = getReadFramebuffer();
3516 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3517
3518 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3519 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3520 {
3521 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3522 }
3523
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003524 if (drawFramebuffer->getSamples() != 0)
3525 {
3526 return error(GL_INVALID_OPERATION);
3527 }
3528
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003529 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3530 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3531 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3532 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3533
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003534 RECT sourceRect;
3535 RECT destRect;
3536
3537 if (srcX0 < srcX1)
3538 {
3539 sourceRect.left = srcX0;
3540 sourceRect.right = srcX1;
3541 destRect.left = dstX0;
3542 destRect.right = dstX1;
3543 }
3544 else
3545 {
3546 sourceRect.left = srcX1;
3547 destRect.left = dstX1;
3548 sourceRect.right = srcX0;
3549 destRect.right = dstX0;
3550 }
3551
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003552 if (srcY0 < srcY1)
3553 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003554 sourceRect.top = readBufferHeight - srcY1;
3555 destRect.top = drawBufferHeight - dstY1;
3556 sourceRect.bottom = readBufferHeight - srcY0;
3557 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003558 }
3559 else
3560 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003561 sourceRect.top = readBufferHeight - srcY0;
3562 destRect.top = drawBufferHeight - dstY0;
3563 sourceRect.bottom = readBufferHeight - srcY1;
3564 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003565 }
3566
3567 RECT sourceScissoredRect = sourceRect;
3568 RECT destScissoredRect = destRect;
3569
3570 if (mState.scissorTest)
3571 {
3572 // Only write to parts of the destination framebuffer which pass the scissor test
3573 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3574 // rect will be checked against scissorY, rather than the bottom.
3575 if (destRect.left < mState.scissorX)
3576 {
3577 int xDiff = mState.scissorX - destRect.left;
3578 destScissoredRect.left = mState.scissorX;
3579 sourceScissoredRect.left += xDiff;
3580 }
3581
3582 if (destRect.right > mState.scissorX + mState.scissorWidth)
3583 {
3584 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3585 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3586 sourceScissoredRect.right -= xDiff;
3587 }
3588
3589 if (destRect.top < mState.scissorY)
3590 {
3591 int yDiff = mState.scissorY - destRect.top;
3592 destScissoredRect.top = mState.scissorY;
3593 sourceScissoredRect.top += yDiff;
3594 }
3595
3596 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3597 {
3598 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3599 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3600 sourceScissoredRect.bottom -= yDiff;
3601 }
3602 }
3603
3604 bool blitRenderTarget = false;
3605 bool blitDepthStencil = false;
3606
3607 RECT sourceTrimmedRect = sourceScissoredRect;
3608 RECT destTrimmedRect = destScissoredRect;
3609
3610 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3611 // the actual draw and read surfaces.
3612 if (sourceTrimmedRect.left < 0)
3613 {
3614 int xDiff = 0 - sourceTrimmedRect.left;
3615 sourceTrimmedRect.left = 0;
3616 destTrimmedRect.left += xDiff;
3617 }
3618
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003619 if (sourceTrimmedRect.right > readBufferWidth)
3620 {
3621 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3622 sourceTrimmedRect.right = readBufferWidth;
3623 destTrimmedRect.right -= xDiff;
3624 }
3625
3626 if (sourceTrimmedRect.top < 0)
3627 {
3628 int yDiff = 0 - sourceTrimmedRect.top;
3629 sourceTrimmedRect.top = 0;
3630 destTrimmedRect.top += yDiff;
3631 }
3632
3633 if (sourceTrimmedRect.bottom > readBufferHeight)
3634 {
3635 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3636 sourceTrimmedRect.bottom = readBufferHeight;
3637 destTrimmedRect.bottom -= yDiff;
3638 }
3639
3640 if (destTrimmedRect.left < 0)
3641 {
3642 int xDiff = 0 - destTrimmedRect.left;
3643 destTrimmedRect.left = 0;
3644 sourceTrimmedRect.left += xDiff;
3645 }
3646
3647 if (destTrimmedRect.right > drawBufferWidth)
3648 {
3649 int xDiff = destTrimmedRect.right - drawBufferWidth;
3650 destTrimmedRect.right = drawBufferWidth;
3651 sourceTrimmedRect.right -= xDiff;
3652 }
3653
3654 if (destTrimmedRect.top < 0)
3655 {
3656 int yDiff = 0 - destTrimmedRect.top;
3657 destTrimmedRect.top = 0;
3658 sourceTrimmedRect.top += yDiff;
3659 }
3660
3661 if (destTrimmedRect.bottom > drawBufferHeight)
3662 {
3663 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3664 destTrimmedRect.bottom = drawBufferHeight;
3665 sourceTrimmedRect.bottom -= yDiff;
3666 }
3667
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003668 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003669 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3670 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3671 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3672 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003673 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3674 {
3675 partialBufferCopy = true;
3676 }
3677
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003678 if (mask & GL_COLOR_BUFFER_BIT)
3679 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003680 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3681 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3682 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3683 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3684 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003685 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3686 {
3687 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3688 return error(GL_INVALID_OPERATION);
3689 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003690
3691 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3692 {
3693 return error(GL_INVALID_OPERATION);
3694 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003695
3696 blitRenderTarget = true;
3697
3698 }
3699
3700 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3701 {
3702 DepthStencilbuffer *readDSBuffer = NULL;
3703 DepthStencilbuffer *drawDSBuffer = NULL;
3704
3705 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3706 // both a depth and stencil buffer, it will be the same buffer.
3707
3708 if (mask & GL_DEPTH_BUFFER_BIT)
3709 {
3710 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3711 {
3712 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3713 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3714 {
3715 return error(GL_INVALID_OPERATION);
3716 }
3717
3718 blitDepthStencil = true;
3719 readDSBuffer = readFramebuffer->getDepthbuffer();
3720 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3721 }
3722 }
3723
3724 if (mask & GL_STENCIL_BUFFER_BIT)
3725 {
3726 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3727 {
3728 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3729 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3730 {
3731 return error(GL_INVALID_OPERATION);
3732 }
3733
3734 blitDepthStencil = true;
3735 readDSBuffer = readFramebuffer->getStencilbuffer();
3736 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3737 }
3738 }
3739
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003740 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003741 {
3742 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3743 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3744 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003745
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003746 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3747 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003748 {
3749 return error(GL_INVALID_OPERATION);
3750 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003751 }
3752
3753 if (blitRenderTarget || blitDepthStencil)
3754 {
3755 egl::Display *display = getDisplay();
3756 display->endScene();
3757
3758 if (blitRenderTarget)
3759 {
3760 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3761 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3762
3763 if (FAILED(result))
3764 {
3765 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3766 return;
3767 }
3768 }
3769
3770 if (blitDepthStencil)
3771 {
3772 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3773
3774 if (FAILED(result))
3775 {
3776 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3777 return;
3778 }
3779 }
3780 }
3781}
3782
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003783VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3784{
3785 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3786 {
3787 mVertexDeclCache[i].vertexDeclaration = NULL;
3788 mVertexDeclCache[i].lruCount = 0;
3789 }
3790}
3791
3792VertexDeclarationCache::~VertexDeclarationCache()
3793{
3794 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3795 {
3796 if (mVertexDeclCache[i].vertexDeclaration)
3797 {
3798 mVertexDeclCache[i].vertexDeclaration->Release();
3799 }
3800 }
3801}
3802
3803GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3804{
3805 IDirect3DDevice9 *device = getDevice();
3806
3807 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3808 D3DVERTEXELEMENT9 *element = &elements[0];
3809
3810 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3811 {
3812 if (attributes[i].active)
3813 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003814 if (mAppliedVBs[i].serial != attributes[i].serial ||
3815 mAppliedVBs[i].stride != attributes[i].stride ||
3816 mAppliedVBs[i].offset != attributes[i].offset)
3817 {
3818 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3819 mAppliedVBs[i].serial = attributes[i].serial;
3820 mAppliedVBs[i].stride = attributes[i].stride;
3821 mAppliedVBs[i].offset = attributes[i].offset;
3822 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003823
3824 element->Stream = i;
3825 element->Offset = 0;
3826 element->Type = attributes[i].type;
3827 element->Method = D3DDECLMETHOD_DEFAULT;
3828 element->Usage = D3DDECLUSAGE_TEXCOORD;
3829 element->UsageIndex = program->getSemanticIndex(i);
3830 element++;
3831 }
3832 }
3833
3834 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3835 *(element++) = end;
3836
3837 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3838 {
3839 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3840 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3841 {
3842 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003843 if(entry->vertexDeclaration != mLastSetVDecl)
3844 {
3845 device->SetVertexDeclaration(entry->vertexDeclaration);
3846 mLastSetVDecl = entry->vertexDeclaration;
3847 }
3848
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003849 return GL_NO_ERROR;
3850 }
3851 }
3852
3853 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3854
3855 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3856 {
3857 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3858 {
3859 lastCache = &mVertexDeclCache[i];
3860 }
3861 }
3862
3863 if (lastCache->vertexDeclaration != NULL)
3864 {
3865 lastCache->vertexDeclaration->Release();
3866 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003867 // mLastSetVDecl is set to the replacement, so we don't have to worry
3868 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003869 }
3870
3871 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3872 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3873 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003874 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003875 lastCache->lruCount = ++mMaxLru;
3876
3877 return GL_NO_ERROR;
3878}
3879
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003880void VertexDeclarationCache::markStateDirty()
3881{
3882 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3883 {
3884 mAppliedVBs[i].serial = 0;
3885 }
3886
3887 mLastSetVDecl = NULL;
3888}
3889
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003890}
3891
3892extern "C"
3893{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003894gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003895{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003896 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003897}
3898
3899void glDestroyContext(gl::Context *context)
3900{
3901 delete context;
3902
3903 if (context == gl::getContext())
3904 {
3905 gl::makeCurrent(NULL, NULL, NULL);
3906 }
3907}
3908
3909void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3910{
3911 gl::makeCurrent(context, display, surface);
3912}
3913
3914gl::Context *glGetCurrentContext()
3915{
3916 return gl::getContext();
3917}
3918}