blob: aaed7bb958d5ac6dbcd2d3e05068beb6561ba3ae [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000028#include "libGLESv2/VertexDataManager.h"
29#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com86487c22010-03-11 19:41:43 +000031#undef near
32#undef far
33
jbauman@chromium.org399c35f2011-04-28 23:19:51 +000034namespace
35{
36 enum { CLOSING_INDEX_BUFFER_SIZE = 4096 };
37}
38
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039namespace gl
40{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +000041Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042{
benvanik@google.com1a233342011-04-28 19:44:39 +000043 mFenceHandleAllocator.setBaseHandle(0);
44
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000046
daniel@transgaming.com428d1582010-05-04 03:35:25 +000047 mState.depthClearValue = 1.0f;
48 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049
daniel@transgaming.com428d1582010-05-04 03:35:25 +000050 mState.cullFace = false;
51 mState.cullMode = GL_BACK;
52 mState.frontFace = GL_CCW;
53 mState.depthTest = false;
54 mState.depthFunc = GL_LESS;
55 mState.blend = false;
56 mState.sourceBlendRGB = GL_ONE;
57 mState.sourceBlendAlpha = GL_ONE;
58 mState.destBlendRGB = GL_ZERO;
59 mState.destBlendAlpha = GL_ZERO;
60 mState.blendEquationRGB = GL_FUNC_ADD;
61 mState.blendEquationAlpha = GL_FUNC_ADD;
62 mState.blendColor.red = 0;
63 mState.blendColor.green = 0;
64 mState.blendColor.blue = 0;
65 mState.blendColor.alpha = 0;
66 mState.stencilTest = false;
67 mState.stencilFunc = GL_ALWAYS;
68 mState.stencilRef = 0;
69 mState.stencilMask = -1;
70 mState.stencilWritemask = -1;
71 mState.stencilBackFunc = GL_ALWAYS;
72 mState.stencilBackRef = 0;
73 mState.stencilBackMask = - 1;
74 mState.stencilBackWritemask = -1;
75 mState.stencilFail = GL_KEEP;
76 mState.stencilPassDepthFail = GL_KEEP;
77 mState.stencilPassDepthPass = GL_KEEP;
78 mState.stencilBackFail = GL_KEEP;
79 mState.stencilBackPassDepthFail = GL_KEEP;
80 mState.stencilBackPassDepthPass = GL_KEEP;
81 mState.polygonOffsetFill = false;
82 mState.polygonOffsetFactor = 0.0f;
83 mState.polygonOffsetUnits = 0.0f;
84 mState.sampleAlphaToCoverage = false;
85 mState.sampleCoverage = false;
86 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000087 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.scissorTest = false;
89 mState.dither = true;
90 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000091 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092
daniel@transgaming.com428d1582010-05-04 03:35:25 +000093 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000094
daniel@transgaming.com428d1582010-05-04 03:35:25 +000095 mState.viewportX = 0;
96 mState.viewportY = 0;
97 mState.viewportWidth = config->mDisplayMode.Width;
98 mState.viewportHeight = config->mDisplayMode.Height;
99 mState.zNear = 0.0f;
100 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000102 mState.scissorX = 0;
103 mState.scissorY = 0;
104 mState.scissorWidth = config->mDisplayMode.Width;
105 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000107 mState.colorMaskRed = true;
108 mState.colorMaskGreen = true;
109 mState.colorMaskBlue = true;
110 mState.colorMaskAlpha = true;
111 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000113 if (shareContext != NULL)
114 {
115 mResourceManager = shareContext->mResourceManager;
116 mResourceManager->addRef();
117 }
118 else
119 {
120 mResourceManager = new ResourceManager();
121 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 // [OpenGL ES 2.0.24] section 3.7 page 83:
124 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
125 // and cube map texture state vectors respectively associated with them.
126 // In order that access to these initial textures not be lost, they are treated as texture
127 // objects all of whose names are 0.
128
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000129 mTexture2DZero.set(new Texture2D(0));
130 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000132 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000133 bindArrayBuffer(0);
134 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindTextureCubeMap(0);
136 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000137 bindReadFramebuffer(0);
138 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 bindRenderbuffer(0);
140
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000141 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.packAlignment = 4;
144 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000145
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000147 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000148 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000149 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000150
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 mInvalidEnum = false;
152 mInvalidValue = false;
153 mInvalidOperation = false;
154 mOutOfMemory = false;
155 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000156
157 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000158
gman@chromium.org50c526d2011-08-10 05:19:44 +0000159 mSupportsDXT1Textures = false;
160 mSupportsDXT3Textures = false;
161 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000162 mSupportsEventQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000163 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000164 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000165 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000166 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167}
168
169Context::~Context()
170{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000171 if (mState.currentProgram != 0)
172 {
173 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
174 if (programObject)
175 {
176 programObject->release();
177 }
178 mState.currentProgram = 0;
179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000181 while (!mFramebufferMap.empty())
182 {
183 deleteFramebuffer(mFramebufferMap.begin()->first);
184 }
185
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000186 while (!mFenceMap.empty())
187 {
188 deleteFence(mFenceMap.begin()->first);
189 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000190
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000191 while (!mMultiSampleSupport.empty())
192 {
193 delete [] mMultiSampleSupport.begin()->second;
194 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
195 }
196
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000197 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000198 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000199 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000200 {
201 mState.samplerTexture[type][sampler].set(NULL);
202 }
203 }
204
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000205 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000206 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000207 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000208 }
209
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
211 {
212 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
213 }
214
215 mState.arrayBuffer.set(NULL);
216 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000217 mState.renderbuffer.set(NULL);
218
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000219 mTexture2DZero.set(NULL);
220 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000222 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000223 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000224 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000225 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000226
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000227 if (mMaskedClearSavedState)
228 {
229 mMaskedClearSavedState->Release();
230 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000231
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000232 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233}
234
235void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
236{
237 IDirect3DDevice9 *device = display->getDevice();
238
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000239 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000240 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000241 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000242
daniel@transgaming.com83921382011-01-08 05:46:00 +0000243 mVertexDataManager = new VertexDataManager(this, device);
244 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000245 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000246
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000247 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +0000248 mSupportsVertexTexture = display->getVertexTextureSupport();
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000249 mSupportsNonPower2Texture = display->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000250
251 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
252 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
253 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
254 mMaxRenderbufferDimension = mMaxTextureDimension;
255 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
256 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
257 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
258
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000259 const D3DFORMAT renderBufferFormats[] =
260 {
261 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000262 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000263 D3DFMT_R5G6B5,
264 D3DFMT_D24S8
265 };
266
267 int max = 0;
268 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
269 {
270 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
271 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
272 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
273
274 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
275 {
276 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
277 {
278 max = j;
279 }
280 }
281 }
282
283 mMaxSupportedSamples = max;
284
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000285 mSupportsEventQueries = display->getEventQuerySupport();
gman@chromium.org50c526d2011-08-10 05:19:44 +0000286 mSupportsDXT1Textures = display->getDXT1TextureSupport();
287 mSupportsDXT3Textures = display->getDXT3TextureSupport();
288 mSupportsDXT5Textures = display->getDXT5TextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000289 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
290 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000291 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
292 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000293
daniel@transgaming.com83921382011-01-08 05:46:00 +0000294 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
295
gman@chromium.org50c526d2011-08-10 05:19:44 +0000296 mNumCompressedTextureFormats = 0;
297 if (supportsDXT1Textures())
298 {
299 mNumCompressedTextureFormats += 2;
300 }
301 if (supportsDXT3Textures())
302 {
303 mNumCompressedTextureFormats += 1;
304 }
305 if (supportsDXT5Textures())
306 {
307 mNumCompressedTextureFormats += 1;
308 }
309
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000310 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000311 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000312
313 mState.viewportX = 0;
314 mState.viewportY = 0;
315 mState.viewportWidth = surface->getWidth();
316 mState.viewportHeight = surface->getHeight();
317
318 mState.scissorX = 0;
319 mState.scissorY = 0;
320 mState.scissorWidth = surface->getWidth();
321 mState.scissorHeight = surface->getHeight();
322
323 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000324 }
325
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
327 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000328 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000331 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000332 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
334 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000336 if (defaultRenderTarget)
337 {
338 defaultRenderTarget->Release();
339 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000341 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000343 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000345
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000346 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347}
348
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000349// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000350void Context::markAllStateDirty()
351{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000352 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
353 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000354 mAppliedTextureSerialPS[t] = 0;
355 }
356
357 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
358 {
359 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000360 }
361
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000362 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000363 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000364 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000365 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000366 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000367 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000368 mViewportInitialized = false;
369 mRenderTargetDescInitialized = false;
370
371 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000372
373 mClearStateDirty = true;
374 mCullStateDirty = true;
375 mDepthStateDirty = true;
376 mMaskStateDirty = true;
377 mBlendStateDirty = true;
378 mStencilStateDirty = true;
379 mPolygonOffsetStateDirty = true;
380 mScissorStateDirty = true;
381 mSampleStateDirty = true;
382 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000383 mFrontFaceDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000384 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000385}
386
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387void Context::setClearColor(float red, float green, float blue, float alpha)
388{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000389 mState.colorClearValue.red = red;
390 mState.colorClearValue.green = green;
391 mState.colorClearValue.blue = blue;
392 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393}
394
395void Context::setClearDepth(float depth)
396{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000397 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398}
399
400void Context::setClearStencil(int stencil)
401{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000402 mState.stencilClearValue = stencil;
403}
404
405void Context::setCullFace(bool enabled)
406{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000407 if (mState.cullFace != enabled)
408 {
409 mState.cullFace = enabled;
410 mCullStateDirty = true;
411 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000412}
413
414bool Context::isCullFaceEnabled() const
415{
416 return mState.cullFace;
417}
418
419void Context::setCullMode(GLenum mode)
420{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000421 if (mState.cullMode != mode)
422 {
423 mState.cullMode = mode;
424 mCullStateDirty = true;
425 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000426}
427
428void Context::setFrontFace(GLenum front)
429{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000430 if (mState.frontFace != front)
431 {
432 mState.frontFace = front;
433 mFrontFaceDirty = true;
434 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435}
436
437void Context::setDepthTest(bool enabled)
438{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000439 if (mState.depthTest != enabled)
440 {
441 mState.depthTest = enabled;
442 mDepthStateDirty = true;
443 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000444}
445
446bool Context::isDepthTestEnabled() const
447{
448 return mState.depthTest;
449}
450
451void Context::setDepthFunc(GLenum depthFunc)
452{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000453 if (mState.depthFunc != depthFunc)
454 {
455 mState.depthFunc = depthFunc;
456 mDepthStateDirty = true;
457 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000458}
459
460void Context::setDepthRange(float zNear, float zFar)
461{
462 mState.zNear = zNear;
463 mState.zFar = zFar;
464}
465
466void Context::setBlend(bool enabled)
467{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000468 if (mState.blend != enabled)
469 {
470 mState.blend = enabled;
471 mBlendStateDirty = true;
472 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000473}
474
475bool Context::isBlendEnabled() const
476{
477 return mState.blend;
478}
479
480void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
481{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000482 if (mState.sourceBlendRGB != sourceRGB ||
483 mState.sourceBlendAlpha != sourceAlpha ||
484 mState.destBlendRGB != destRGB ||
485 mState.destBlendAlpha != destAlpha)
486 {
487 mState.sourceBlendRGB = sourceRGB;
488 mState.destBlendRGB = destRGB;
489 mState.sourceBlendAlpha = sourceAlpha;
490 mState.destBlendAlpha = destAlpha;
491 mBlendStateDirty = true;
492 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000493}
494
495void Context::setBlendColor(float red, float green, float blue, float alpha)
496{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 if (mState.blendColor.red != red ||
498 mState.blendColor.green != green ||
499 mState.blendColor.blue != blue ||
500 mState.blendColor.alpha != alpha)
501 {
502 mState.blendColor.red = red;
503 mState.blendColor.green = green;
504 mState.blendColor.blue = blue;
505 mState.blendColor.alpha = alpha;
506 mBlendStateDirty = true;
507 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000508}
509
510void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
511{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000512 if (mState.blendEquationRGB != rgbEquation ||
513 mState.blendEquationAlpha != alphaEquation)
514 {
515 mState.blendEquationRGB = rgbEquation;
516 mState.blendEquationAlpha = alphaEquation;
517 mBlendStateDirty = true;
518 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000519}
520
521void Context::setStencilTest(bool enabled)
522{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000523 if (mState.stencilTest != enabled)
524 {
525 mState.stencilTest = enabled;
526 mStencilStateDirty = true;
527 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000528}
529
530bool Context::isStencilTestEnabled() const
531{
532 return mState.stencilTest;
533}
534
535void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
536{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000537 if (mState.stencilFunc != stencilFunc ||
538 mState.stencilRef != stencilRef ||
539 mState.stencilMask != stencilMask)
540 {
541 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000542 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000543 mState.stencilMask = stencilMask;
544 mStencilStateDirty = true;
545 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000546}
547
548void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
549{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000550 if (mState.stencilBackFunc != stencilBackFunc ||
551 mState.stencilBackRef != stencilBackRef ||
552 mState.stencilBackMask != stencilBackMask)
553 {
554 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000555 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000556 mState.stencilBackMask = stencilBackMask;
557 mStencilStateDirty = true;
558 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000559}
560
561void Context::setStencilWritemask(GLuint stencilWritemask)
562{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000563 if (mState.stencilWritemask != stencilWritemask)
564 {
565 mState.stencilWritemask = stencilWritemask;
566 mStencilStateDirty = true;
567 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000568}
569
570void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
571{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000572 if (mState.stencilBackWritemask != stencilBackWritemask)
573 {
574 mState.stencilBackWritemask = stencilBackWritemask;
575 mStencilStateDirty = true;
576 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000577}
578
579void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
580{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000581 if (mState.stencilFail != stencilFail ||
582 mState.stencilPassDepthFail != stencilPassDepthFail ||
583 mState.stencilPassDepthPass != stencilPassDepthPass)
584 {
585 mState.stencilFail = stencilFail;
586 mState.stencilPassDepthFail = stencilPassDepthFail;
587 mState.stencilPassDepthPass = stencilPassDepthPass;
588 mStencilStateDirty = true;
589 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000590}
591
592void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
593{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000594 if (mState.stencilBackFail != stencilBackFail ||
595 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
596 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
597 {
598 mState.stencilBackFail = stencilBackFail;
599 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
600 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
601 mStencilStateDirty = true;
602 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000603}
604
605void Context::setPolygonOffsetFill(bool enabled)
606{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000607 if (mState.polygonOffsetFill != enabled)
608 {
609 mState.polygonOffsetFill = enabled;
610 mPolygonOffsetStateDirty = true;
611 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000612}
613
614bool Context::isPolygonOffsetFillEnabled() const
615{
616 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000617
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000618}
619
620void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
621{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000622 if (mState.polygonOffsetFactor != factor ||
623 mState.polygonOffsetUnits != units)
624 {
625 mState.polygonOffsetFactor = factor;
626 mState.polygonOffsetUnits = units;
627 mPolygonOffsetStateDirty = true;
628 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000629}
630
631void Context::setSampleAlphaToCoverage(bool enabled)
632{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000633 if (mState.sampleAlphaToCoverage != enabled)
634 {
635 mState.sampleAlphaToCoverage = enabled;
636 mSampleStateDirty = true;
637 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000638}
639
640bool Context::isSampleAlphaToCoverageEnabled() const
641{
642 return mState.sampleAlphaToCoverage;
643}
644
645void Context::setSampleCoverage(bool enabled)
646{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000647 if (mState.sampleCoverage != enabled)
648 {
649 mState.sampleCoverage = enabled;
650 mSampleStateDirty = true;
651 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000652}
653
654bool Context::isSampleCoverageEnabled() const
655{
656 return mState.sampleCoverage;
657}
658
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000659void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000660{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000661 if (mState.sampleCoverageValue != value ||
662 mState.sampleCoverageInvert != invert)
663 {
664 mState.sampleCoverageValue = value;
665 mState.sampleCoverageInvert = invert;
666 mSampleStateDirty = true;
667 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000668}
669
670void Context::setScissorTest(bool enabled)
671{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000672 if (mState.scissorTest != enabled)
673 {
674 mState.scissorTest = enabled;
675 mScissorStateDirty = true;
676 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000677}
678
679bool Context::isScissorTestEnabled() const
680{
681 return mState.scissorTest;
682}
683
684void Context::setDither(bool enabled)
685{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000686 if (mState.dither != enabled)
687 {
688 mState.dither = enabled;
689 mDitherStateDirty = true;
690 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000691}
692
693bool Context::isDitherEnabled() const
694{
695 return mState.dither;
696}
697
698void Context::setLineWidth(GLfloat width)
699{
700 mState.lineWidth = width;
701}
702
703void Context::setGenerateMipmapHint(GLenum hint)
704{
705 mState.generateMipmapHint = hint;
706}
707
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000708void Context::setFragmentShaderDerivativeHint(GLenum hint)
709{
710 mState.fragmentShaderDerivativeHint = hint;
711 // TODO: Propagate the hint to shader translator so we can write
712 // ddx, ddx_coarse, or ddx_fine depending on the hint.
713 // Ignore for now. It is valid for implementations to ignore hint.
714}
715
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000716void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
717{
718 mState.viewportX = x;
719 mState.viewportY = y;
720 mState.viewportWidth = width;
721 mState.viewportHeight = height;
722}
723
724void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
725{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000726 if (mState.scissorX != x || mState.scissorY != y ||
727 mState.scissorWidth != width || mState.scissorHeight != height)
728 {
729 mState.scissorX = x;
730 mState.scissorY = y;
731 mState.scissorWidth = width;
732 mState.scissorHeight = height;
733 mScissorStateDirty = true;
734 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000735}
736
737void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
738{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000739 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
740 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
741 {
742 mState.colorMaskRed = red;
743 mState.colorMaskGreen = green;
744 mState.colorMaskBlue = blue;
745 mState.colorMaskAlpha = alpha;
746 mMaskStateDirty = true;
747 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000748}
749
750void Context::setDepthMask(bool mask)
751{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000752 if (mState.depthMask != mask)
753 {
754 mState.depthMask = mask;
755 mMaskStateDirty = true;
756 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000757}
758
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000759void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000760{
761 mState.activeSampler = active;
762}
763
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000764GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000765{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000766 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000767}
768
769GLuint Context::getDrawFramebufferHandle() const
770{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000771 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000772}
773
774GLuint Context::getRenderbufferHandle() const
775{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000776 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000777}
778
779GLuint Context::getArrayBufferHandle() const
780{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000781 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000782}
783
daniel@transgaming.com83921382011-01-08 05:46:00 +0000784void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000785{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000786 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000787}
788
daniel@transgaming.com83921382011-01-08 05:46:00 +0000789const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000790{
791 return mState.vertexAttribute[attribNum];
792}
793
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000794void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000795 GLsizei stride, const void *pointer)
796{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000797 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000798 mState.vertexAttribute[attribNum].mSize = size;
799 mState.vertexAttribute[attribNum].mType = type;
800 mState.vertexAttribute[attribNum].mNormalized = normalized;
801 mState.vertexAttribute[attribNum].mStride = stride;
802 mState.vertexAttribute[attribNum].mPointer = pointer;
803}
804
805const void *Context::getVertexAttribPointer(unsigned int attribNum) const
806{
807 return mState.vertexAttribute[attribNum].mPointer;
808}
809
daniel@transgaming.com83921382011-01-08 05:46:00 +0000810const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000811{
812 return mState.vertexAttribute;
813}
814
815void Context::setPackAlignment(GLint alignment)
816{
817 mState.packAlignment = alignment;
818}
819
820GLint Context::getPackAlignment() const
821{
822 return mState.packAlignment;
823}
824
825void Context::setUnpackAlignment(GLint alignment)
826{
827 mState.unpackAlignment = alignment;
828}
829
830GLint Context::getUnpackAlignment() const
831{
832 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833}
834
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835GLuint Context::createBuffer()
836{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000837 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838}
839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840GLuint Context::createProgram()
841{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000842 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843}
844
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000845GLuint Context::createShader(GLenum type)
846{
847 return mResourceManager->createShader(type);
848}
849
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850GLuint Context::createTexture()
851{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000852 return mResourceManager->createTexture();
853}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000855GLuint Context::createRenderbuffer()
856{
857 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000860// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861GLuint Context::createFramebuffer()
862{
benvanik@google.com1a233342011-04-28 19:44:39 +0000863 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000864
865 mFramebufferMap[handle] = NULL;
866
867 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868}
869
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000870GLuint Context::createFence()
871{
benvanik@google.com1a233342011-04-28 19:44:39 +0000872 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000873
874 mFenceMap[handle] = new Fence;
875
876 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000877}
878
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879void Context::deleteBuffer(GLuint buffer)
880{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000881 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882 {
883 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000885
886 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
889void Context::deleteShader(GLuint shader)
890{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000891 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892}
893
894void Context::deleteProgram(GLuint program)
895{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000896 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000897 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
900void Context::deleteTexture(GLuint texture)
901{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000902 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903 {
904 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000906
907 mResourceManager->deleteTexture(texture);
908}
909
910void Context::deleteRenderbuffer(GLuint renderbuffer)
911{
912 if (mResourceManager->getRenderbuffer(renderbuffer))
913 {
914 detachRenderbuffer(renderbuffer);
915 }
916
917 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918}
919
920void Context::deleteFramebuffer(GLuint framebuffer)
921{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000922 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
923
924 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925 {
926 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000927
benvanik@google.com1a233342011-04-28 19:44:39 +0000928 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000929 delete framebufferObject->second;
930 mFramebufferMap.erase(framebufferObject);
931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000933
934void Context::deleteFence(GLuint fence)
935{
936 FenceMap::iterator fenceObject = mFenceMap.find(fence);
937
938 if (fenceObject != mFenceMap.end())
939 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000940 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000941 delete fenceObject->second;
942 mFenceMap.erase(fenceObject);
943 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000944}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000946Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000948 return mResourceManager->getBuffer(handle);
949}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000951Shader *Context::getShader(GLuint handle)
952{
953 return mResourceManager->getShader(handle);
954}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000956Program *Context::getProgram(GLuint handle)
957{
958 return mResourceManager->getProgram(handle);
959}
960
961Texture *Context::getTexture(GLuint handle)
962{
963 return mResourceManager->getTexture(handle);
964}
965
966Renderbuffer *Context::getRenderbuffer(GLuint handle)
967{
968 return mResourceManager->getRenderbuffer(handle);
969}
970
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000971Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000972{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000973 return getFramebuffer(mState.readFramebuffer);
974}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000975
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000976Framebuffer *Context::getDrawFramebuffer()
977{
978 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979}
980
981void Context::bindArrayBuffer(unsigned int buffer)
982{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000983 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000985 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
988void Context::bindElementArrayBuffer(unsigned int buffer)
989{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000990 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000992 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
995void Context::bindTexture2D(GLuint texture)
996{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000997 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000999 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000}
1001
1002void Context::bindTextureCubeMap(GLuint texture)
1003{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001004 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001006 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007}
1008
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001009void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001011 if (!getFramebuffer(framebuffer))
1012 {
1013 mFramebufferMap[framebuffer] = new Framebuffer();
1014 }
1015
1016 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001017}
1018
1019void Context::bindDrawFramebuffer(GLuint framebuffer)
1020{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001021 if (!getFramebuffer(framebuffer))
1022 {
1023 mFramebufferMap[framebuffer] = new Framebuffer();
1024 }
1025
1026 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027}
1028
1029void Context::bindRenderbuffer(GLuint renderbuffer)
1030{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001031 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1032
1033 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034}
1035
1036void Context::useProgram(GLuint program)
1037{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001038 GLuint priorProgram = mState.currentProgram;
1039 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001040
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001041 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001043 Program *newProgram = mResourceManager->getProgram(program);
1044 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001045 mCachedCurrentProgram = NULL;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001046
1047 if (newProgram)
1048 {
1049 newProgram->addRef();
1050 }
1051
1052 if (oldProgram)
1053 {
1054 oldProgram->release();
1055 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057}
1058
1059void Context::setFramebufferZero(Framebuffer *buffer)
1060{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001061 delete mFramebufferMap[0];
1062 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063}
1064
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001065void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001067 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1068 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069}
1070
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001071Framebuffer *Context::getFramebuffer(unsigned int handle)
1072{
1073 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1074
1075 if (framebuffer == mFramebufferMap.end())
1076 {
1077 return NULL;
1078 }
1079 else
1080 {
1081 return framebuffer->second;
1082 }
1083}
1084
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001085Fence *Context::getFence(unsigned int handle)
1086{
1087 FenceMap::iterator fence = mFenceMap.find(handle);
1088
1089 if (fence == mFenceMap.end())
1090 {
1091 return NULL;
1092 }
1093 else
1094 {
1095 return fence->second;
1096 }
1097}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001098
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099Buffer *Context::getArrayBuffer()
1100{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001101 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102}
1103
1104Buffer *Context::getElementArrayBuffer()
1105{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001106 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107}
1108
1109Program *Context::getCurrentProgram()
1110{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001111 if (!mCachedCurrentProgram)
1112 {
1113 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1114 }
1115 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116}
1117
1118Texture2D *Context::getTexture2D()
1119{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001120 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121}
1122
1123TextureCubeMap *Context::getTextureCubeMap()
1124{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001125 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001126}
1127
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001128Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001130 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001131
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001132 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001133 {
1134 switch (type)
1135 {
1136 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001137 case TEXTURE_2D: return mTexture2DZero.get();
1138 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001139 }
1140 }
1141
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001142 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001143}
1144
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001145bool Context::getBooleanv(GLenum pname, GLboolean *params)
1146{
1147 switch (pname)
1148 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001149 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1150 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1151 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001152 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001153 params[0] = mState.colorMaskRed;
1154 params[1] = mState.colorMaskGreen;
1155 params[2] = mState.colorMaskBlue;
1156 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001157 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001158 case GL_CULL_FACE: *params = mState.cullFace; break;
1159 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1160 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1161 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1162 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1163 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1164 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1165 case GL_BLEND: *params = mState.blend; break;
1166 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001167 default:
1168 return false;
1169 }
1170
1171 return true;
1172}
1173
1174bool Context::getFloatv(GLenum pname, GLfloat *params)
1175{
1176 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1177 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1178 // GetIntegerv as its native query function. As it would require conversion in any
1179 // case, this should make no difference to the calling application.
1180 switch (pname)
1181 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001182 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1183 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1184 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1185 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1186 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001187 case GL_ALIASED_LINE_WIDTH_RANGE:
1188 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1189 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1190 break;
1191 case GL_ALIASED_POINT_SIZE_RANGE:
1192 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001193 params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001194 break;
1195 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001196 params[0] = mState.zNear;
1197 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001198 break;
1199 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001200 params[0] = mState.colorClearValue.red;
1201 params[1] = mState.colorClearValue.green;
1202 params[2] = mState.colorClearValue.blue;
1203 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001204 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001205 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001206 params[0] = mState.blendColor.red;
1207 params[1] = mState.blendColor.green;
1208 params[2] = mState.blendColor.blue;
1209 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001210 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001211 default:
1212 return false;
1213 }
1214
1215 return true;
1216}
1217
1218bool Context::getIntegerv(GLenum pname, GLint *params)
1219{
1220 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1221 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1222 // GetIntegerv as its native query function. As it would require conversion in any
1223 // case, this should make no difference to the calling application. You may find it in
1224 // Context::getFloatv.
1225 switch (pname)
1226 {
1227 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1228 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001229 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001230 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1231 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001232 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001233 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001234 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001235 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001236 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001237 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1238 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001239 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1240 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1241 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001242 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001243 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1244 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1245 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1246 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001247 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001248 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1249 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1250 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1251 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1252 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1253 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1254 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1255 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1256 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1257 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1258 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1259 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1260 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1261 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1262 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1263 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1264 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1265 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1266 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1267 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1268 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1269 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1270 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1271 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001272 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1273 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001274 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001275 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001276 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001277 case GL_MAX_SAMPLES_ANGLE:
1278 {
1279 GLsizei maxSamples = getMaxSupportedSamples();
1280 if (maxSamples != 0)
1281 {
1282 *params = maxSamples;
1283 }
1284 else
1285 {
1286 return false;
1287 }
1288
1289 break;
1290 }
1291 case GL_SAMPLE_BUFFERS:
1292 case GL_SAMPLES:
1293 {
1294 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1295 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1296 {
1297 switch (pname)
1298 {
1299 case GL_SAMPLE_BUFFERS:
1300 if (framebuffer->getSamples() != 0)
1301 {
1302 *params = 1;
1303 }
1304 else
1305 {
1306 *params = 0;
1307 }
1308 break;
1309 case GL_SAMPLES:
1310 *params = framebuffer->getSamples();
1311 break;
1312 }
1313 }
1314 else
1315 {
1316 *params = 0;
1317 }
1318 }
1319 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001320 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1321 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1322 case GL_MAX_VIEWPORT_DIMS:
1323 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001324 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001325 params[0] = maxDimension;
1326 params[1] = maxDimension;
1327 }
1328 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001329 case GL_COMPRESSED_TEXTURE_FORMATS:
1330 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001331 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001332 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001333 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1334 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1335 }
1336 if (supportsDXT3Textures())
1337 {
1338 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1339 }
1340 if (supportsDXT5Textures())
1341 {
1342 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001343 }
1344 }
1345 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001346 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001347 params[0] = mState.viewportX;
1348 params[1] = mState.viewportY;
1349 params[2] = mState.viewportWidth;
1350 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001351 break;
1352 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001353 params[0] = mState.scissorX;
1354 params[1] = mState.scissorY;
1355 params[2] = mState.scissorWidth;
1356 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001357 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001358 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1359 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001360 case GL_RED_BITS:
1361 case GL_GREEN_BITS:
1362 case GL_BLUE_BITS:
1363 case GL_ALPHA_BITS:
1364 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001365 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001366 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1367
1368 if (colorbuffer)
1369 {
1370 switch (pname)
1371 {
1372 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1373 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1374 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1375 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1376 }
1377 }
1378 else
1379 {
1380 *params = 0;
1381 }
1382 }
1383 break;
1384 case GL_DEPTH_BITS:
1385 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001386 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001387 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001388
1389 if (depthbuffer)
1390 {
1391 *params = depthbuffer->getDepthSize();
1392 }
1393 else
1394 {
1395 *params = 0;
1396 }
1397 }
1398 break;
1399 case GL_STENCIL_BITS:
1400 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001401 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001402 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001403
1404 if (stencilbuffer)
1405 {
1406 *params = stencilbuffer->getStencilSize();
1407 }
1408 else
1409 {
1410 *params = 0;
1411 }
1412 }
1413 break;
1414 case GL_TEXTURE_BINDING_2D:
1415 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001416 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001417 {
1418 error(GL_INVALID_OPERATION);
1419 return false;
1420 }
1421
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001422 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001423 }
1424 break;
1425 case GL_TEXTURE_BINDING_CUBE_MAP:
1426 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001427 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001428 {
1429 error(GL_INVALID_OPERATION);
1430 return false;
1431 }
1432
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001433 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001434 }
1435 break;
1436 default:
1437 return false;
1438 }
1439
1440 return true;
1441}
1442
1443bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1444{
1445 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1446 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1447 // to the fact that it is stored internally as a float, and so would require conversion
1448 // if returned from Context::getIntegerv. Since this conversion is already implemented
1449 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1450 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1451 // application.
1452 switch (pname)
1453 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001454 case GL_COMPRESSED_TEXTURE_FORMATS:
1455 {
1456 *type = GL_INT;
1457 *numParams = mNumCompressedTextureFormats;
1458 }
1459 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001460 case GL_SHADER_BINARY_FORMATS:
1461 {
1462 *type = GL_INT;
1463 *numParams = 0;
1464 }
1465 break;
1466 case GL_MAX_VERTEX_ATTRIBS:
1467 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1468 case GL_MAX_VARYING_VECTORS:
1469 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1470 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1471 case GL_MAX_TEXTURE_IMAGE_UNITS:
1472 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1473 case GL_MAX_RENDERBUFFER_SIZE:
1474 case GL_NUM_SHADER_BINARY_FORMATS:
1475 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1476 case GL_ARRAY_BUFFER_BINDING:
1477 case GL_FRAMEBUFFER_BINDING:
1478 case GL_RENDERBUFFER_BINDING:
1479 case GL_CURRENT_PROGRAM:
1480 case GL_PACK_ALIGNMENT:
1481 case GL_UNPACK_ALIGNMENT:
1482 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001483 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001484 case GL_RED_BITS:
1485 case GL_GREEN_BITS:
1486 case GL_BLUE_BITS:
1487 case GL_ALPHA_BITS:
1488 case GL_DEPTH_BITS:
1489 case GL_STENCIL_BITS:
1490 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1491 case GL_CULL_FACE_MODE:
1492 case GL_FRONT_FACE:
1493 case GL_ACTIVE_TEXTURE:
1494 case GL_STENCIL_FUNC:
1495 case GL_STENCIL_VALUE_MASK:
1496 case GL_STENCIL_REF:
1497 case GL_STENCIL_FAIL:
1498 case GL_STENCIL_PASS_DEPTH_FAIL:
1499 case GL_STENCIL_PASS_DEPTH_PASS:
1500 case GL_STENCIL_BACK_FUNC:
1501 case GL_STENCIL_BACK_VALUE_MASK:
1502 case GL_STENCIL_BACK_REF:
1503 case GL_STENCIL_BACK_FAIL:
1504 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1505 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1506 case GL_DEPTH_FUNC:
1507 case GL_BLEND_SRC_RGB:
1508 case GL_BLEND_SRC_ALPHA:
1509 case GL_BLEND_DST_RGB:
1510 case GL_BLEND_DST_ALPHA:
1511 case GL_BLEND_EQUATION_RGB:
1512 case GL_BLEND_EQUATION_ALPHA:
1513 case GL_STENCIL_WRITEMASK:
1514 case GL_STENCIL_BACK_WRITEMASK:
1515 case GL_STENCIL_CLEAR_VALUE:
1516 case GL_SUBPIXEL_BITS:
1517 case GL_MAX_TEXTURE_SIZE:
1518 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1519 case GL_SAMPLE_BUFFERS:
1520 case GL_SAMPLES:
1521 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1522 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1523 case GL_TEXTURE_BINDING_2D:
1524 case GL_TEXTURE_BINDING_CUBE_MAP:
1525 {
1526 *type = GL_INT;
1527 *numParams = 1;
1528 }
1529 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001530 case GL_MAX_SAMPLES_ANGLE:
1531 {
1532 if (getMaxSupportedSamples() != 0)
1533 {
1534 *type = GL_INT;
1535 *numParams = 1;
1536 }
1537 else
1538 {
1539 return false;
1540 }
1541 }
1542 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001543 case GL_MAX_VIEWPORT_DIMS:
1544 {
1545 *type = GL_INT;
1546 *numParams = 2;
1547 }
1548 break;
1549 case GL_VIEWPORT:
1550 case GL_SCISSOR_BOX:
1551 {
1552 *type = GL_INT;
1553 *numParams = 4;
1554 }
1555 break;
1556 case GL_SHADER_COMPILER:
1557 case GL_SAMPLE_COVERAGE_INVERT:
1558 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001559 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1560 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1561 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1562 case GL_SAMPLE_COVERAGE:
1563 case GL_SCISSOR_TEST:
1564 case GL_STENCIL_TEST:
1565 case GL_DEPTH_TEST:
1566 case GL_BLEND:
1567 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001568 {
1569 *type = GL_BOOL;
1570 *numParams = 1;
1571 }
1572 break;
1573 case GL_COLOR_WRITEMASK:
1574 {
1575 *type = GL_BOOL;
1576 *numParams = 4;
1577 }
1578 break;
1579 case GL_POLYGON_OFFSET_FACTOR:
1580 case GL_POLYGON_OFFSET_UNITS:
1581 case GL_SAMPLE_COVERAGE_VALUE:
1582 case GL_DEPTH_CLEAR_VALUE:
1583 case GL_LINE_WIDTH:
1584 {
1585 *type = GL_FLOAT;
1586 *numParams = 1;
1587 }
1588 break;
1589 case GL_ALIASED_LINE_WIDTH_RANGE:
1590 case GL_ALIASED_POINT_SIZE_RANGE:
1591 case GL_DEPTH_RANGE:
1592 {
1593 *type = GL_FLOAT;
1594 *numParams = 2;
1595 }
1596 break;
1597 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001598 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001599 {
1600 *type = GL_FLOAT;
1601 *numParams = 4;
1602 }
1603 break;
1604 default:
1605 return false;
1606 }
1607
1608 return true;
1609}
1610
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611// Applies the render target surface, depth stencil surface, viewport rectangle and
1612// scissor rectangle to the Direct3D 9 device
1613bool Context::applyRenderTarget(bool ignoreViewport)
1614{
1615 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001616
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001617 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618
1619 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1620 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001621 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001622 }
1623
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001624 IDirect3DSurface9 *renderTarget = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001625 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001627 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001628 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1629 if (renderTargetSerial != mAppliedRenderTargetSerial)
1630 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001631 renderTarget = framebufferObject->getRenderTarget();
1632
1633 if (!renderTarget)
1634 {
1635 return false; // Context must be lost
1636 }
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001637 device->SetRenderTarget(0, renderTarget);
1638 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001639 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001640 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001641 }
1642
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001643 unsigned int depthbufferSerial = 0;
1644 unsigned int stencilbufferSerial = 0;
1645 if (framebufferObject->getDepthbufferType() != GL_NONE)
1646 {
1647 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001648 if (!depthStencil)
1649 {
1650 ERR("Depth stencil pointer unexpectedly null.");
1651 return false;
1652 }
1653
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001654 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1655 }
1656 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1657 {
1658 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001659 if (!depthStencil)
1660 {
1661 ERR("Depth stencil pointer unexpectedly null.");
1662 return false;
1663 }
1664
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001665 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1666 }
1667
1668 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001669 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001670 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001671 {
1672 device->SetDepthStencilSurface(depthStencil);
1673 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001674 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001675 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001676 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001678 if (!mRenderTargetDescInitialized || renderTargetChanged)
1679 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001680 if (!renderTarget)
1681 {
1682 renderTarget = framebufferObject->getRenderTarget();
1683
1684 if (!renderTarget)
1685 {
1686 return false; // Context must be lost
1687 }
1688 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001689 renderTarget->GetDesc(&mRenderTargetDesc);
1690 mRenderTargetDescInitialized = true;
1691 }
1692
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001695 float zNear = clamp01(mState.zNear);
1696 float zFar = clamp01(mState.zFar);
1697
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001698 if (ignoreViewport)
1699 {
1700 viewport.X = 0;
1701 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001702 viewport.Width = mRenderTargetDesc.Width;
1703 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 viewport.MinZ = 0.0f;
1705 viewport.MaxZ = 1.0f;
1706 }
1707 else
1708 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001709 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1710 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1711 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1712 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1713 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 +00001714 viewport.MinZ = zNear;
1715 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716 }
1717
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001718 if (viewport.Width <= 0 || viewport.Height <= 0)
1719 {
1720 return false; // Nothing to render
1721 }
1722
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001723 if (!mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
1724 {
1725 device->SetViewport(&viewport);
1726 mSetViewport = viewport;
1727 mViewportInitialized = true;
1728 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001730 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001732 if (mState.scissorTest)
1733 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001734 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1735 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1736 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1737 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1738 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001739 device->SetScissorRect(&rect);
1740 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1741 }
1742 else
1743 {
1744 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1745 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001746
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748 }
1749
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001750 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 Program *programObject = getCurrentProgram();
1753
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001754 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001755 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001756 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001757
daniel@transgaming.com31754962010-11-28 02:02:52 +00001758 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001759 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1760 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1761 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001762 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001763
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001764 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001765 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001766 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001767
daniel@transgaming.com31754962010-11-28 02:02:52 +00001768 GLint depthRange = programObject->getDxDepthRangeLocation();
1769 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1770 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771 }
1772
1773 return true;
1774}
1775
1776// 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 +00001777void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001778{
1779 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001780 Program *programObject = getCurrentProgram();
1781
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001782 Framebuffer *framebufferObject = getDrawFramebuffer();
1783
1784 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1785
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001786 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001787 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001788 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001789
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001790 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001791 GLint alwaysFront = !isTriangleMode(drawMode);
1792 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1793
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001794 egl::Display *display = getDisplay();
1795 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
1796 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1797 // Apparently some ATI cards have a bug where a draw with a zero color
1798 // write mask can cause later draws to have incorrect results. Instead,
1799 // set a nonzero color write mask but modify the blend state so that no
1800 // drawing is done.
1801 // http://code.google.com/p/angleproject/issues/detail?id=169
1802
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001803 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001805 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001807 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808 }
1809 else
1810 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001811 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812 }
1813
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001814 mCullStateDirty = false;
1815 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001817 if (mDepthStateDirty)
1818 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001819 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001821 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1822 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001823 }
1824 else
1825 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001828
1829 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 }
1831
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001832 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1833 {
1834 mBlendStateDirty = true;
1835 mMaskStateDirty = true;
1836 }
1837
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001838 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001840 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001841 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001842 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1843
1844 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1845 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1846 {
1847 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1848 }
1849 else
1850 {
1851 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1852 unorm<8>(mState.blendColor.alpha),
1853 unorm<8>(mState.blendColor.alpha),
1854 unorm<8>(mState.blendColor.alpha)));
1855 }
1856
1857 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1858 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1859 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1860
1861 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1862 mState.destBlendRGB != mState.destBlendAlpha ||
1863 mState.blendEquationRGB != mState.blendEquationAlpha)
1864 {
1865 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1866
1867 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1868 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1869 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001870 }
1871 else
1872 {
1873 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1874 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001875 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001876 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001877 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001878 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001879 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001880
1881 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001882 }
1883
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001884 if (mStencilStateDirty || mFrontFaceDirty)
1885 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001886 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001887 {
1888 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1889 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1890
1891 // FIXME: Unsupported by D3D9
1892 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1893 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1894 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1895 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1896 mState.stencilRef != mState.stencilBackRef ||
1897 mState.stencilMask != mState.stencilBackMask)
1898 {
1899 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1900 return error(GL_INVALID_OPERATION);
1901 }
1902
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001903 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001904 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001905 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1906
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001907 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1908 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001909 es2dx::ConvertComparison(mState.stencilFunc));
1910
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001911 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1912 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001913
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001914 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001915 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001916 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001917 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001918 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001919 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1920
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001921 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1922 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001923 es2dx::ConvertComparison(mState.stencilBackFunc));
1924
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001925 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1926 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001927
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001928 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001929 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001930 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001931 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001932 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001933 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1934 }
1935 else
1936 {
1937 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1938 }
1939
1940 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001941 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001942 }
1943
1944 if (mMaskStateDirty)
1945 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001946 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1947 mState.colorMaskBlue, mState.colorMaskAlpha);
1948 if (colorMask == 0 && !zeroColorMaskAllowed)
1949 {
1950 // Enable green channel, but set blending so nothing will be drawn.
1951 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1952 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1953
1954 device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1955 device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1956 device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
1957 }
1958 else
1959 {
1960 device->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
1961 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001962 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1963
1964 mMaskStateDirty = false;
1965 }
1966
1967 if (mPolygonOffsetStateDirty)
1968 {
1969 if (mState.polygonOffsetFill)
1970 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001971 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001972 if (depthbuffer)
1973 {
1974 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1975 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1976 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1977 }
1978 }
1979 else
1980 {
1981 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1982 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1983 }
1984
1985 mPolygonOffsetStateDirty = false;
1986 }
1987
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001988 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001990 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001991 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001992 FIXME("Sample alpha to coverage is unimplemented.");
1993 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001994
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001995 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1996 if (mState.sampleCoverage)
1997 {
1998 unsigned int mask = 0;
1999 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002000 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002001 float threshold = 0.5f;
2002
2003 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002004 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002005 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002006
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002007 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002008 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002009 threshold += 1.0f;
2010 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002011 }
2012 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002013 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002014
2015 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002016 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002017 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002018 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002019
2020 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002021 }
2022 else
2023 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002024 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002025 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002026
2027 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028 }
2029
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002030 if (mDitherStateDirty)
2031 {
2032 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
2033
2034 mDitherStateDirty = false;
2035 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002036}
2037
daniel@transgaming.com83921382011-01-08 05:46:00 +00002038GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002039{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002040 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002041
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002042 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002043 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002044 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002045 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002046 }
2047
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00002048 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002049}
2050
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002051// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002052GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002053{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002054 IDirect3DDevice9 *device = getDevice();
2055 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002056
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002057 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002058 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002059 if (indexInfo->serial != mAppliedIBSerial)
2060 {
2061 device->SetIndices(indexInfo->indexBuffer);
2062 mAppliedIBSerial = indexInfo->serial;
2063 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002064 }
2065
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002066 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002067}
2068
2069// Applies the shaders and shader constants to the Direct3D 9 device
2070void Context::applyShaders()
2071{
2072 IDirect3DDevice9 *device = getDevice();
2073 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002074 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002075 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002076 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2077 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2078
2079 device->SetPixelShader(pixelShader);
2080 device->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002081 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002082 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002083 }
2084
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085 programObject->applyUniforms();
2086}
2087
2088// Applies the textures and sampler states to the Direct3D 9 device
2089void Context::applyTextures()
2090{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002091 applyTextures(SAMPLER_PIXEL);
2092
2093 if (mSupportsVertexTexture)
2094 {
2095 applyTextures(SAMPLER_VERTEX);
2096 }
2097}
2098
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002099// For each Direct3D 9 sampler of either the pixel or vertex stage,
2100// looks up the corresponding OpenGL texture image unit and texture type,
2101// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002102void Context::applyTextures(SamplerType type)
2103{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 IDirect3DDevice9 *device = getDevice();
2105 Program *programObject = getCurrentProgram();
2106
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002107 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 +00002108 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2109 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002110
2111 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002113 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002114 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002115
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002116 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002118 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002119
2120 Texture *texture = getSamplerTexture(textureUnit, textureType);
2121
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002122 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002123 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002124 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2125
2126 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002127 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002128 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002129 {
2130 GLenum wrapS = texture->getWrapS();
2131 GLenum wrapT = texture->getWrapT();
2132 GLenum minFilter = texture->getMinFilter();
2133 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002135 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2136 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002137
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002138 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002139 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2140 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002141 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2142 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002143 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002145 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002146 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002147 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002148 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002149 }
2150 else
2151 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002152 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002153 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002154
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002155 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002156 texture->resetDirty();
2157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002159 else
2160 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002161 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002162 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002163 device->SetTexture(d3dSampler, NULL);
2164 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002165 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002166 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167 }
2168}
2169
2170void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2171{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002172 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002173
2174 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2175 {
2176 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2177 }
2178
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002179 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2180 {
2181 return error(GL_INVALID_OPERATION);
2182 }
2183
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002184 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002185
2186 if (!renderTarget)
2187 {
2188 return; // Context must be lost, return silently
2189 }
2190
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002191 IDirect3DDevice9 *device = getDevice();
2192
2193 D3DSURFACE_DESC desc;
2194 renderTarget->GetDesc(&desc);
2195
2196 IDirect3DSurface9 *systemSurface;
2197 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2198
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002199 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002200 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002201 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002202 return error(GL_OUT_OF_MEMORY);
2203 }
2204
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2206 {
2207 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002208 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209 }
2210
2211 result = device->GetRenderTargetData(renderTarget, systemSurface);
2212
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213 if (FAILED(result))
2214 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002215 systemSurface->Release();
2216
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002217 switch (result)
2218 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002219 // It turns out that D3D will sometimes produce more error
2220 // codes than those documented.
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002221 case D3DERR_DRIVERINTERNALERROR:
2222 case D3DERR_DEVICELOST:
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002223 case D3DERR_DEVICEHUNG:
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002224 return error(GL_OUT_OF_MEMORY);
2225 default:
2226 UNREACHABLE();
2227 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002228 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002229 }
2230
2231 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002232 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2233 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2234 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2235 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2236 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002237
2238 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2239
2240 if (FAILED(result))
2241 {
2242 UNREACHABLE();
2243 systemSurface->Release();
2244
2245 return; // No sensible error to generate
2246 }
2247
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002248 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002249 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002250 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002251 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002252 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002253
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254 for (int j = 0; j < rect.bottom - rect.top; j++)
2255 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002256 if (desc.Format == D3DFMT_A8R8G8B8 &&
2257 format == GL_BGRA_EXT &&
2258 type == GL_UNSIGNED_BYTE)
2259 {
2260 // Fast path for EXT_read_format_bgra, given
2261 // an RGBA source buffer. Note that buffers with no
2262 // alpha go through the slow path below.
2263 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002264 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002265 (rect.right - rect.left) * 4);
2266 continue;
2267 }
2268
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 for (int i = 0; i < rect.right - rect.left; i++)
2270 {
2271 float r;
2272 float g;
2273 float b;
2274 float a;
2275
2276 switch (desc.Format)
2277 {
2278 case D3DFMT_R5G6B5:
2279 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002280 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281
2282 a = 1.0f;
2283 b = (rgb & 0x001F) * (1.0f / 0x001F);
2284 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2285 r = (rgb & 0xF800) * (1.0f / 0xF800);
2286 }
2287 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 case D3DFMT_A1R5G5B5:
2289 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002290 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291
2292 a = (argb & 0x8000) ? 1.0f : 0.0f;
2293 b = (argb & 0x001F) * (1.0f / 0x001F);
2294 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2295 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2296 }
2297 break;
2298 case D3DFMT_A8R8G8B8:
2299 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002300 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
2302 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2303 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2304 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2305 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2306 }
2307 break;
2308 case D3DFMT_X8R8G8B8:
2309 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002310 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311
2312 a = 1.0f;
2313 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2314 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2315 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2316 }
2317 break;
2318 case D3DFMT_A2R10G10B10:
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 & 0xC0000000) * (1.0f / 0xC0000000);
2323 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2324 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2325 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2326 }
2327 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002328 case D3DFMT_A32B32G32R32F:
2329 {
2330 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002331 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2332 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2333 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2334 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002335 }
2336 break;
2337 case D3DFMT_A16B16G16R16F:
2338 {
2339 // float formats in D3D are stored rgba, rather than the other way round
2340 float abgr[4];
2341
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002342 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002343
2344 a = abgr[3];
2345 b = abgr[2];
2346 g = abgr[1];
2347 r = abgr[0];
2348 }
2349 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350 default:
2351 UNIMPLEMENTED(); // FIXME
2352 UNREACHABLE();
2353 }
2354
2355 switch (format)
2356 {
2357 case GL_RGBA:
2358 switch (type)
2359 {
2360 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002361 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2362 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2363 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2364 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002365 break;
2366 default: UNREACHABLE();
2367 }
2368 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002369 case GL_BGRA_EXT:
2370 switch (type)
2371 {
2372 case GL_UNSIGNED_BYTE:
2373 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2374 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2375 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2376 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2377 break;
2378 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2379 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2380 // this type is packed as follows:
2381 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2382 // --------------------------------------------------------------------------------
2383 // | 4th | 3rd | 2nd | 1st component |
2384 // --------------------------------------------------------------------------------
2385 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2386 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2387 ((unsigned short)(15 * a + 0.5f) << 12)|
2388 ((unsigned short)(15 * r + 0.5f) << 8) |
2389 ((unsigned short)(15 * g + 0.5f) << 4) |
2390 ((unsigned short)(15 * b + 0.5f) << 0);
2391 break;
2392 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2393 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2394 // this type is packed as follows:
2395 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2396 // --------------------------------------------------------------------------------
2397 // | 4th | 3rd | 2nd | 1st component |
2398 // --------------------------------------------------------------------------------
2399 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2400 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2401 ((unsigned short)( a + 0.5f) << 15) |
2402 ((unsigned short)(31 * r + 0.5f) << 10) |
2403 ((unsigned short)(31 * g + 0.5f) << 5) |
2404 ((unsigned short)(31 * b + 0.5f) << 0);
2405 break;
2406 default: UNREACHABLE();
2407 }
2408 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002409 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410 switch (type)
2411 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002412 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002413 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2414 ((unsigned short)(31 * b + 0.5f) << 0) |
2415 ((unsigned short)(63 * g + 0.5f) << 5) |
2416 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417 break;
2418 default: UNREACHABLE();
2419 }
2420 break;
2421 default: UNREACHABLE();
2422 }
2423 }
2424 }
2425
2426 systemSurface->UnlockRect();
2427
2428 systemSurface->Release();
2429}
2430
2431void Context::clear(GLbitfield mask)
2432{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002433 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002434
2435 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2436 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002437 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002438 }
2439
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002440 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 IDirect3DDevice9 *device = getDevice();
2442 DWORD flags = 0;
2443
2444 if (mask & GL_COLOR_BUFFER_BIT)
2445 {
2446 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002447
2448 if (framebufferObject->getColorbufferType() != GL_NONE)
2449 {
2450 flags |= D3DCLEAR_TARGET;
2451 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 }
2453
2454 if (mask & GL_DEPTH_BUFFER_BIT)
2455 {
2456 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002457 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 {
2459 flags |= D3DCLEAR_ZBUFFER;
2460 }
2461 }
2462
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 GLuint stencilUnmasked = 0x0;
2464
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002465 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002468 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002470 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002471 if (!depthStencil)
2472 {
2473 ERR("Depth stencil pointer unexpectedly null.");
2474 return;
2475 }
2476
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002477 D3DSURFACE_DESC desc;
2478 depthStencil->GetDesc(&desc);
2479
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002480 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002481 stencilUnmasked = (0x1 << stencilSize) - 1;
2482
2483 if (stencilUnmasked != 0x0)
2484 {
2485 flags |= D3DCLEAR_STENCIL;
2486 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 }
2488 }
2489
2490 if (mask != 0)
2491 {
2492 return error(GL_INVALID_VALUE);
2493 }
2494
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002495 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2496 {
2497 return;
2498 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002500 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002501 unorm<8>(mState.colorClearValue.red),
2502 unorm<8>(mState.colorClearValue.green),
2503 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002504 float depth = clamp01(mState.depthClearValue);
2505 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506
2507 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2508
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002509 if (!renderTarget)
2510 {
2511 return; // Context must be lost, return silently
2512 }
2513
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 D3DSURFACE_DESC desc;
2515 renderTarget->GetDesc(&desc);
2516
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002517 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518
2519 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002520 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002521 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002522 !(mState.colorMaskRed && mState.colorMaskGreen &&
2523 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524
2525 if (needMaskedColorClear || needMaskedStencilClear)
2526 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002527 // State which is altered in all paths from this point to the clear call is saved.
2528 // State which is altered in only some paths will be flagged dirty in the case that
2529 // that path is taken.
2530 HRESULT hr;
2531 if (mMaskedClearSavedState == NULL)
2532 {
2533 hr = device->BeginStateBlock();
2534 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2535
2536 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2537 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2538 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2539 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2540 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2541 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2542 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2543 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2544 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2545 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2546 device->SetPixelShader(NULL);
2547 device->SetVertexShader(NULL);
2548 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
jbauman@chromium.org23c9e312011-09-21 22:16:44 +00002549 device->SetStreamSource(0, NULL, 0, 0);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002550 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2551 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2552 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2553 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2554 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2555 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2556 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002557
2558 hr = device->EndStateBlock(&mMaskedClearSavedState);
2559 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2560 }
2561
2562 ASSERT(mMaskedClearSavedState != NULL);
2563
2564 if (mMaskedClearSavedState != NULL)
2565 {
2566 hr = mMaskedClearSavedState->Capture();
2567 ASSERT(SUCCEEDED(hr));
2568 }
2569
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2571 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2572 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2573 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2574 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2575 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2576 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2577 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2578
2579 if (flags & D3DCLEAR_TARGET)
2580 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002581 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002582 }
2583 else
2584 {
2585 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2586 }
2587
2588 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2589 {
2590 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2591 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2592 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2593 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002594 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002595 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002596 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2597 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002598 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599 }
2600 else
2601 {
2602 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2603 }
2604
2605 device->SetPixelShader(NULL);
2606 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002607 device->SetFVF(D3DFVF_XYZRHW);
2608 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2609 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2610 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2611 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2612 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2613 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2614 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002616 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2617 quad[0][0] = -0.5f;
2618 quad[0][1] = desc.Height - 0.5f;
2619 quad[0][2] = 0.0f;
2620 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002622 quad[1][0] = desc.Width - 0.5f;
2623 quad[1][1] = desc.Height - 0.5f;
2624 quad[1][2] = 0.0f;
2625 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002627 quad[2][0] = -0.5f;
2628 quad[2][1] = -0.5f;
2629 quad[2][2] = 0.0f;
2630 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002632 quad[3][0] = desc.Width - 0.5f;
2633 quad[3][1] = -0.5f;
2634 quad[3][2] = 0.0f;
2635 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002636
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002637 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002638 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002639
2640 if (flags & D3DCLEAR_ZBUFFER)
2641 {
2642 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2643 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2644 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2645 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002646
2647 if (mMaskedClearSavedState != NULL)
2648 {
2649 mMaskedClearSavedState->Apply();
2650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002652 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653 {
2654 device->Clear(0, NULL, flags, color, depth, stencil);
2655 }
2656}
2657
2658void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2659{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002660 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661 {
2662 return error(GL_INVALID_OPERATION);
2663 }
2664
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002665 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 IDirect3DDevice9 *device = getDevice();
2667 D3DPRIMITIVETYPE primitiveType;
2668 int primitiveCount;
2669
2670 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2671 return error(GL_INVALID_ENUM);
2672
2673 if (primitiveCount <= 0)
2674 {
2675 return;
2676 }
2677
2678 if (!applyRenderTarget(false))
2679 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002680 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 }
2682
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002683 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002684
daniel@transgaming.com83921382011-01-08 05:46:00 +00002685 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002686 if (err != GL_NO_ERROR)
2687 {
2688 return error(err);
2689 }
2690
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002691 applyShaders();
2692 applyTextures();
2693
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002694 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002695 {
2696 return error(GL_INVALID_OPERATION);
2697 }
2698
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002699 if (!cullSkipsDraw(mode))
2700 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002701 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002702
2703 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002704
2705 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2706 {
2707 drawClosingLine(first, first + count - 1);
2708 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002709 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710}
2711
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002712void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002714 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715 {
2716 return error(GL_INVALID_OPERATION);
2717 }
2718
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002719 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720 {
2721 return error(GL_INVALID_OPERATION);
2722 }
2723
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002724 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725 IDirect3DDevice9 *device = getDevice();
2726 D3DPRIMITIVETYPE primitiveType;
2727 int primitiveCount;
2728
2729 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2730 return error(GL_INVALID_ENUM);
2731
2732 if (primitiveCount <= 0)
2733 {
2734 return;
2735 }
2736
2737 if (!applyRenderTarget(false))
2738 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002739 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740 }
2741
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002742 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002743
2744 TranslatedIndexData indexInfo;
2745 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2746 if (err != GL_NO_ERROR)
2747 {
2748 return error(err);
2749 }
2750
daniel@transgaming.com83921382011-01-08 05:46:00 +00002751 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2752 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002753 if (err != GL_NO_ERROR)
2754 {
2755 return error(err);
2756 }
2757
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758 applyShaders();
2759 applyTextures();
2760
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002761 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002762 {
2763 return error(GL_INVALID_OPERATION);
2764 }
2765
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002766 if (!cullSkipsDraw(mode))
2767 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002768 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002769
daniel@transgaming.com83921382011-01-08 05:46:00 +00002770 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002771
2772 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2773 {
2774 drawClosingLine(count, type, indices);
2775 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002776 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002777}
2778
2779void Context::finish()
2780{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002781 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002782 IDirect3DDevice9 *device = getDevice();
2783 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002784 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002785
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002786 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2787 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002789 ERR("CreateQuery failed hr=%x\n", result);
2790 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2791 {
2792 return error(GL_OUT_OF_MEMORY);
2793 }
2794 ASSERT(false);
2795 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796 }
2797
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002798 IDirect3DStateBlock9 *savedState = NULL;
2799 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2800 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002801 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002802 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002803 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002804
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002805 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002806 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002807 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002808 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002809 ASSERT(false);
2810 return;
2811 }
2812
2813 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2814 if (FAILED(result))
2815 {
2816 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2817 occlusionQuery->Release();
2818 savedState->Release();
2819 ASSERT(false);
2820 return;
2821 }
2822
2823 // Render something outside the render target
2824 device->SetPixelShader(NULL);
2825 device->SetVertexShader(NULL);
2826 device->SetFVF(D3DFVF_XYZRHW);
2827 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2828 display->startScene();
2829 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2830
2831 result = occlusionQuery->Issue(D3DISSUE_END);
2832 if (FAILED(result))
2833 {
2834 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2835 occlusionQuery->Release();
2836 savedState->Apply();
2837 savedState->Release();
2838 ASSERT(false);
2839 return;
2840 }
2841
2842 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2843 {
2844 // Keep polling, but allow other threads to do something useful first
2845 Sleep(0);
2846 }
2847
2848 occlusionQuery->Release();
2849 savedState->Apply();
2850 savedState->Release();
2851
2852 if (result == D3DERR_DEVICELOST)
2853 {
2854 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002855 }
2856}
2857
2858void Context::flush()
2859{
2860 IDirect3DDevice9 *device = getDevice();
2861 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002862 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002863
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002864 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2865 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002866 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002867 ERR("CreateQuery failed hr=%x\n", result);
2868 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2869 {
2870 return error(GL_OUT_OF_MEMORY);
2871 }
2872 ASSERT(false);
2873 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874 }
2875
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002876 result = eventQuery->Issue(D3DISSUE_END);
2877 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002878 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002879 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2880 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002881 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002882 return;
2883 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002884
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002885 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2886 eventQuery->Release();
2887
2888 if (result == D3DERR_DEVICELOST)
2889 {
2890 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002891 }
2892}
2893
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002894void Context::drawClosingLine(unsigned int first, unsigned int last)
2895{
2896 IDirect3DDevice9 *device = getDevice();
2897 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002898 bool succeeded = false;
2899 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002900
2901 if (supports32bitIndices())
2902 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002903 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002904
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002905 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002906 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002907 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2908 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002909
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002910 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2911
2912 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2913 if (data)
2914 {
2915 data[0] = last;
2916 data[1] = first;
2917 mClosingIB->unmap();
2918 offset /= 4;
2919 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002920 }
2921 }
2922 else
2923 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002924 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002925
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002926 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002927 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002928 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2929 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002930
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002931 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2932
2933 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2934 if (data)
2935 {
2936 data[0] = last;
2937 data[1] = first;
2938 mClosingIB->unmap();
2939 offset /= 2;
2940 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002941 }
2942 }
2943
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002944 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002945 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002946 device->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002947 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002948
jbauman@chromium.org2c199b12011-06-13 22:20:06 +00002949 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002950 }
2951 else
2952 {
2953 ERR("Could not create an index buffer for closing a line loop.");
2954 error(GL_OUT_OF_MEMORY);
2955 }
2956}
2957
2958void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2959{
2960 unsigned int first = 0;
2961 unsigned int last = 0;
2962
2963 if (mState.elementArrayBuffer.get())
2964 {
2965 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2966 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2967 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2968 }
2969
2970 switch (type)
2971 {
2972 case GL_UNSIGNED_BYTE:
2973 first = static_cast<const GLubyte*>(indices)[0];
2974 last = static_cast<const GLubyte*>(indices)[count - 1];
2975 break;
2976 case GL_UNSIGNED_SHORT:
2977 first = static_cast<const GLushort*>(indices)[0];
2978 last = static_cast<const GLushort*>(indices)[count - 1];
2979 break;
2980 case GL_UNSIGNED_INT:
2981 first = static_cast<const GLuint*>(indices)[0];
2982 last = static_cast<const GLuint*>(indices)[count - 1];
2983 break;
2984 default: UNREACHABLE();
2985 }
2986
2987 drawClosingLine(first, last);
2988}
2989
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002990void Context::recordInvalidEnum()
2991{
2992 mInvalidEnum = true;
2993}
2994
2995void Context::recordInvalidValue()
2996{
2997 mInvalidValue = true;
2998}
2999
3000void Context::recordInvalidOperation()
3001{
3002 mInvalidOperation = true;
3003}
3004
3005void Context::recordOutOfMemory()
3006{
3007 mOutOfMemory = true;
3008}
3009
3010void Context::recordInvalidFramebufferOperation()
3011{
3012 mInvalidFramebufferOperation = true;
3013}
3014
3015// Get one of the recorded errors and clear its flag, if any.
3016// [OpenGL ES 2.0.24] section 2.5 page 13.
3017GLenum Context::getError()
3018{
3019 if (mInvalidEnum)
3020 {
3021 mInvalidEnum = false;
3022
3023 return GL_INVALID_ENUM;
3024 }
3025
3026 if (mInvalidValue)
3027 {
3028 mInvalidValue = false;
3029
3030 return GL_INVALID_VALUE;
3031 }
3032
3033 if (mInvalidOperation)
3034 {
3035 mInvalidOperation = false;
3036
3037 return GL_INVALID_OPERATION;
3038 }
3039
3040 if (mOutOfMemory)
3041 {
3042 mOutOfMemory = false;
3043
3044 return GL_OUT_OF_MEMORY;
3045 }
3046
3047 if (mInvalidFramebufferOperation)
3048 {
3049 mInvalidFramebufferOperation = false;
3050
3051 return GL_INVALID_FRAMEBUFFER_OPERATION;
3052 }
3053
3054 return GL_NO_ERROR;
3055}
3056
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003057bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003058{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003059 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003060}
3061
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003062int Context::getMaximumVaryingVectors() const
3063{
3064 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3065}
3066
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003067unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003068{
3069 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3070}
3071
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003072unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003073{
3074 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3075}
3076
daniel@transgaming.com458da142010-11-28 02:03:02 +00003077int Context::getMaximumFragmentUniformVectors() const
3078{
3079 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3080}
3081
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003082int Context::getMaxSupportedSamples() const
3083{
3084 return mMaxSupportedSamples;
3085}
3086
3087int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3088{
3089 if (requested == 0)
3090 {
3091 return requested;
3092 }
3093
3094 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3095 if (itr == mMultiSampleSupport.end())
3096 {
3097 return -1;
3098 }
3099
3100 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3101 {
3102 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3103 {
3104 return i;
3105 }
3106 }
3107
3108 return -1;
3109}
3110
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003111bool Context::supportsEventQueries() const
3112{
3113 return mSupportsEventQueries;
3114}
3115
gman@chromium.org50c526d2011-08-10 05:19:44 +00003116bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003117{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003118 return mSupportsDXT1Textures;
3119}
3120
3121bool Context::supportsDXT3Textures() const
3122{
3123 return mSupportsDXT3Textures;
3124}
3125
3126bool Context::supportsDXT5Textures() const
3127{
3128 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003129}
3130
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003131bool Context::supportsFloatTextures() const
3132{
3133 return mSupportsFloatTextures;
3134}
3135
3136bool Context::supportsFloatLinearFilter() const
3137{
3138 return mSupportsFloatLinearFilter;
3139}
3140
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003141bool Context::supportsFloatRenderableTextures() const
3142{
3143 return mSupportsFloatRenderableTextures;
3144}
3145
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003146bool Context::supportsHalfFloatTextures() const
3147{
3148 return mSupportsHalfFloatTextures;
3149}
3150
3151bool Context::supportsHalfFloatLinearFilter() const
3152{
3153 return mSupportsHalfFloatLinearFilter;
3154}
3155
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003156bool Context::supportsHalfFloatRenderableTextures() const
3157{
3158 return mSupportsHalfFloatRenderableTextures;
3159}
3160
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003161int Context::getMaximumRenderbufferDimension() const
3162{
3163 return mMaxRenderbufferDimension;
3164}
3165
3166int Context::getMaximumTextureDimension() const
3167{
3168 return mMaxTextureDimension;
3169}
3170
3171int Context::getMaximumCubeTextureDimension() const
3172{
3173 return mMaxCubeTextureDimension;
3174}
3175
3176int Context::getMaximumTextureLevel() const
3177{
3178 return mMaxTextureLevel;
3179}
3180
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003181bool Context::supportsLuminanceTextures() const
3182{
3183 return mSupportsLuminanceTextures;
3184}
3185
3186bool Context::supportsLuminanceAlphaTextures() const
3187{
3188 return mSupportsLuminanceAlphaTextures;
3189}
3190
daniel@transgaming.com83921382011-01-08 05:46:00 +00003191bool Context::supports32bitIndices() const
3192{
3193 return mSupports32bitIndices;
3194}
3195
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003196bool Context::supportsNonPower2Texture() const
3197{
3198 return mSupportsNonPower2Texture;
3199}
3200
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003201void Context::detachBuffer(GLuint buffer)
3202{
3203 // [OpenGL ES 2.0.24] section 2.9 page 22:
3204 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3205 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3206
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003207 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003208 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003209 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003210 }
3211
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003212 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003213 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003214 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003215 }
3216
3217 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3218 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003219 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003220 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003221 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003222 }
3223 }
3224}
3225
3226void Context::detachTexture(GLuint texture)
3227{
3228 // [OpenGL ES 2.0.24] section 3.8 page 84:
3229 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3230 // rebound to texture object zero
3231
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003232 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003233 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003234 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003235 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003236 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003237 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003238 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003239 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003240 }
3241 }
3242
3243 // [OpenGL ES 2.0.24] section 4.4 page 112:
3244 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3245 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3246 // image was attached in the currently bound framebuffer.
3247
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003248 Framebuffer *readFramebuffer = getReadFramebuffer();
3249 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003250
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003251 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003252 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003253 readFramebuffer->detachTexture(texture);
3254 }
3255
3256 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3257 {
3258 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003259 }
3260}
3261
3262void Context::detachFramebuffer(GLuint framebuffer)
3263{
3264 // [OpenGL ES 2.0.24] section 4.4 page 107:
3265 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3266 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3267
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003268 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003269 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003270 bindReadFramebuffer(0);
3271 }
3272
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003273 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003274 {
3275 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003276 }
3277}
3278
3279void Context::detachRenderbuffer(GLuint renderbuffer)
3280{
3281 // [OpenGL ES 2.0.24] section 4.4 page 109:
3282 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3283 // had been executed with the target RENDERBUFFER and name of zero.
3284
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003285 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003286 {
3287 bindRenderbuffer(0);
3288 }
3289
3290 // [OpenGL ES 2.0.24] section 4.4 page 111:
3291 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3292 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3293 // point to which this image was attached in the currently bound framebuffer.
3294
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003295 Framebuffer *readFramebuffer = getReadFramebuffer();
3296 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003297
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003298 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003299 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003300 readFramebuffer->detachRenderbuffer(renderbuffer);
3301 }
3302
3303 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3304 {
3305 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003306 }
3307}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003308
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003309Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003310{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003311 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003312
3313 if (t == NULL)
3314 {
3315 static const GLubyte color[] = { 0, 0, 0, 255 };
3316
3317 switch (type)
3318 {
3319 default:
3320 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003321 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003322
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003323 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003324 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003325 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003326 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003327 t = incomplete2d;
3328 }
3329 break;
3330
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003331 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003332 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003333 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003334
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003335 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3336 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3337 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3338 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3339 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3340 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003341
3342 t = incompleteCube;
3343 }
3344 break;
3345 }
3346
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003347 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003348 }
3349
3350 return t;
3351}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003352
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003353bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003354{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003355 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003356}
3357
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003358bool Context::isTriangleMode(GLenum drawMode)
3359{
3360 switch (drawMode)
3361 {
3362 case GL_TRIANGLES:
3363 case GL_TRIANGLE_FAN:
3364 case GL_TRIANGLE_STRIP:
3365 return true;
3366 case GL_POINTS:
3367 case GL_LINES:
3368 case GL_LINE_LOOP:
3369 case GL_LINE_STRIP:
3370 return false;
3371 default: UNREACHABLE();
3372 }
3373
3374 return false;
3375}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003376
3377void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3378{
3379 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3380
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003381 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3382 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3383 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3384 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003385
daniel@transgaming.com83921382011-01-08 05:46:00 +00003386 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003387}
3388
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003389// keep list sorted in following order
3390// OES extensions
3391// EXT extensions
3392// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003393void Context::initExtensionString()
3394{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003395 mExtensionString = "";
3396
3397 // OES extensions
3398 if (supports32bitIndices())
3399 {
3400 mExtensionString += "GL_OES_element_index_uint ";
3401 }
3402
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003403 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003404 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003405 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003406
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003407 if (supportsHalfFloatTextures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003408 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003409 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003410 }
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003411 if (supportsHalfFloatLinearFilter())
3412 {
3413 mExtensionString += "GL_OES_texture_half_float_linear ";
3414 }
3415 if (supportsFloatTextures())
3416 {
3417 mExtensionString += "GL_OES_texture_float ";
3418 }
3419 if (supportsFloatLinearFilter())
3420 {
3421 mExtensionString += "GL_OES_texture_float_linear ";
3422 }
3423
3424 if (supportsNonPower2Texture())
3425 {
3426 mExtensionString += "GL_OES_texture_npot ";
3427 }
3428
3429 // Multi-vendor (EXT) extensions
3430 mExtensionString += "GL_EXT_read_format_bgra ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003431
gman@chromium.org50c526d2011-08-10 05:19:44 +00003432 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003433 {
3434 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3435 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003436
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003437 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003438
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003439 // ANGLE-specific extensions
3440 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003441 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003442 {
3443 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3444 }
3445
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003446 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003447 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003448 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3449 }
3450 if (supportsDXT5Textures())
3451 {
3452 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003453 }
zmo@google.coma574f782011-10-03 21:45:23 +00003454 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003455
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003456 // Other vendor-specific extensions
3457 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003458 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003459 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003460 }
3461
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003462 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3463 if (end != std::string::npos)
3464 {
3465 mExtensionString.resize(end+1);
3466 }
3467}
3468
3469const char *Context::getExtensionString() const
3470{
3471 return mExtensionString.c_str();
3472}
3473
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003474void Context::initRendererString()
3475{
3476 egl::Display *display = getDisplay();
3477 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
3478
3479 mRendererString = "ANGLE (";
3480 mRendererString += identifier->Description;
3481 mRendererString += ")";
3482}
3483
3484const char *Context::getRendererString() const
3485{
3486 return mRendererString.c_str();
3487}
3488
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003489void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3490 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3491 GLbitfield mask)
3492{
3493 IDirect3DDevice9 *device = getDevice();
3494
3495 Framebuffer *readFramebuffer = getReadFramebuffer();
3496 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3497
3498 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3499 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3500 {
3501 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3502 }
3503
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003504 if (drawFramebuffer->getSamples() != 0)
3505 {
3506 return error(GL_INVALID_OPERATION);
3507 }
3508
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003509 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3510 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3511 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3512 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3513
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003514 RECT sourceRect;
3515 RECT destRect;
3516
3517 if (srcX0 < srcX1)
3518 {
3519 sourceRect.left = srcX0;
3520 sourceRect.right = srcX1;
3521 destRect.left = dstX0;
3522 destRect.right = dstX1;
3523 }
3524 else
3525 {
3526 sourceRect.left = srcX1;
3527 destRect.left = dstX1;
3528 sourceRect.right = srcX0;
3529 destRect.right = dstX0;
3530 }
3531
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003532 if (srcY0 < srcY1)
3533 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003534 sourceRect.top = readBufferHeight - srcY1;
3535 destRect.top = drawBufferHeight - dstY1;
3536 sourceRect.bottom = readBufferHeight - srcY0;
3537 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003538 }
3539 else
3540 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003541 sourceRect.top = readBufferHeight - srcY0;
3542 destRect.top = drawBufferHeight - dstY0;
3543 sourceRect.bottom = readBufferHeight - srcY1;
3544 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003545 }
3546
3547 RECT sourceScissoredRect = sourceRect;
3548 RECT destScissoredRect = destRect;
3549
3550 if (mState.scissorTest)
3551 {
3552 // Only write to parts of the destination framebuffer which pass the scissor test
3553 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3554 // rect will be checked against scissorY, rather than the bottom.
3555 if (destRect.left < mState.scissorX)
3556 {
3557 int xDiff = mState.scissorX - destRect.left;
3558 destScissoredRect.left = mState.scissorX;
3559 sourceScissoredRect.left += xDiff;
3560 }
3561
3562 if (destRect.right > mState.scissorX + mState.scissorWidth)
3563 {
3564 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3565 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3566 sourceScissoredRect.right -= xDiff;
3567 }
3568
3569 if (destRect.top < mState.scissorY)
3570 {
3571 int yDiff = mState.scissorY - destRect.top;
3572 destScissoredRect.top = mState.scissorY;
3573 sourceScissoredRect.top += yDiff;
3574 }
3575
3576 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3577 {
3578 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3579 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3580 sourceScissoredRect.bottom -= yDiff;
3581 }
3582 }
3583
3584 bool blitRenderTarget = false;
3585 bool blitDepthStencil = false;
3586
3587 RECT sourceTrimmedRect = sourceScissoredRect;
3588 RECT destTrimmedRect = destScissoredRect;
3589
3590 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3591 // the actual draw and read surfaces.
3592 if (sourceTrimmedRect.left < 0)
3593 {
3594 int xDiff = 0 - sourceTrimmedRect.left;
3595 sourceTrimmedRect.left = 0;
3596 destTrimmedRect.left += xDiff;
3597 }
3598
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003599 if (sourceTrimmedRect.right > readBufferWidth)
3600 {
3601 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3602 sourceTrimmedRect.right = readBufferWidth;
3603 destTrimmedRect.right -= xDiff;
3604 }
3605
3606 if (sourceTrimmedRect.top < 0)
3607 {
3608 int yDiff = 0 - sourceTrimmedRect.top;
3609 sourceTrimmedRect.top = 0;
3610 destTrimmedRect.top += yDiff;
3611 }
3612
3613 if (sourceTrimmedRect.bottom > readBufferHeight)
3614 {
3615 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3616 sourceTrimmedRect.bottom = readBufferHeight;
3617 destTrimmedRect.bottom -= yDiff;
3618 }
3619
3620 if (destTrimmedRect.left < 0)
3621 {
3622 int xDiff = 0 - destTrimmedRect.left;
3623 destTrimmedRect.left = 0;
3624 sourceTrimmedRect.left += xDiff;
3625 }
3626
3627 if (destTrimmedRect.right > drawBufferWidth)
3628 {
3629 int xDiff = destTrimmedRect.right - drawBufferWidth;
3630 destTrimmedRect.right = drawBufferWidth;
3631 sourceTrimmedRect.right -= xDiff;
3632 }
3633
3634 if (destTrimmedRect.top < 0)
3635 {
3636 int yDiff = 0 - destTrimmedRect.top;
3637 destTrimmedRect.top = 0;
3638 sourceTrimmedRect.top += yDiff;
3639 }
3640
3641 if (destTrimmedRect.bottom > drawBufferHeight)
3642 {
3643 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3644 destTrimmedRect.bottom = drawBufferHeight;
3645 sourceTrimmedRect.bottom -= yDiff;
3646 }
3647
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003648 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003649 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3650 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3651 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3652 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003653 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3654 {
3655 partialBufferCopy = true;
3656 }
3657
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003658 if (mask & GL_COLOR_BUFFER_BIT)
3659 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003660 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3661 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3662 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3663 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3664 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003665 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3666 {
3667 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3668 return error(GL_INVALID_OPERATION);
3669 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003670
3671 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3672 {
3673 return error(GL_INVALID_OPERATION);
3674 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003675
3676 blitRenderTarget = true;
3677
3678 }
3679
3680 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3681 {
3682 DepthStencilbuffer *readDSBuffer = NULL;
3683 DepthStencilbuffer *drawDSBuffer = NULL;
3684
3685 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3686 // both a depth and stencil buffer, it will be the same buffer.
3687
3688 if (mask & GL_DEPTH_BUFFER_BIT)
3689 {
3690 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3691 {
3692 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3693 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3694 {
3695 return error(GL_INVALID_OPERATION);
3696 }
3697
3698 blitDepthStencil = true;
3699 readDSBuffer = readFramebuffer->getDepthbuffer();
3700 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3701 }
3702 }
3703
3704 if (mask & GL_STENCIL_BUFFER_BIT)
3705 {
3706 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3707 {
3708 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3709 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3710 {
3711 return error(GL_INVALID_OPERATION);
3712 }
3713
3714 blitDepthStencil = true;
3715 readDSBuffer = readFramebuffer->getStencilbuffer();
3716 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3717 }
3718 }
3719
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003720 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003721 {
3722 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3723 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3724 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003725
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003726 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3727 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003728 {
3729 return error(GL_INVALID_OPERATION);
3730 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003731 }
3732
3733 if (blitRenderTarget || blitDepthStencil)
3734 {
3735 egl::Display *display = getDisplay();
3736 display->endScene();
3737
3738 if (blitRenderTarget)
3739 {
3740 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3741 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3742
3743 if (FAILED(result))
3744 {
3745 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3746 return;
3747 }
3748 }
3749
3750 if (blitDepthStencil)
3751 {
3752 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3753
3754 if (FAILED(result))
3755 {
3756 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3757 return;
3758 }
3759 }
3760 }
3761}
3762
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003763VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3764{
3765 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3766 {
3767 mVertexDeclCache[i].vertexDeclaration = NULL;
3768 mVertexDeclCache[i].lruCount = 0;
3769 }
3770}
3771
3772VertexDeclarationCache::~VertexDeclarationCache()
3773{
3774 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3775 {
3776 if (mVertexDeclCache[i].vertexDeclaration)
3777 {
3778 mVertexDeclCache[i].vertexDeclaration->Release();
3779 }
3780 }
3781}
3782
3783GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3784{
3785 IDirect3DDevice9 *device = getDevice();
3786
3787 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3788 D3DVERTEXELEMENT9 *element = &elements[0];
3789
3790 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3791 {
3792 if (attributes[i].active)
3793 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003794 if (mAppliedVBs[i].serial != attributes[i].serial ||
3795 mAppliedVBs[i].stride != attributes[i].stride ||
3796 mAppliedVBs[i].offset != attributes[i].offset)
3797 {
3798 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3799 mAppliedVBs[i].serial = attributes[i].serial;
3800 mAppliedVBs[i].stride = attributes[i].stride;
3801 mAppliedVBs[i].offset = attributes[i].offset;
3802 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003803
3804 element->Stream = i;
3805 element->Offset = 0;
3806 element->Type = attributes[i].type;
3807 element->Method = D3DDECLMETHOD_DEFAULT;
3808 element->Usage = D3DDECLUSAGE_TEXCOORD;
3809 element->UsageIndex = program->getSemanticIndex(i);
3810 element++;
3811 }
3812 }
3813
3814 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3815 *(element++) = end;
3816
3817 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3818 {
3819 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3820 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3821 {
3822 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003823 if(entry->vertexDeclaration != mLastSetVDecl)
3824 {
3825 device->SetVertexDeclaration(entry->vertexDeclaration);
3826 mLastSetVDecl = entry->vertexDeclaration;
3827 }
3828
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003829 return GL_NO_ERROR;
3830 }
3831 }
3832
3833 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3834
3835 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3836 {
3837 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3838 {
3839 lastCache = &mVertexDeclCache[i];
3840 }
3841 }
3842
3843 if (lastCache->vertexDeclaration != NULL)
3844 {
3845 lastCache->vertexDeclaration->Release();
3846 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003847 // mLastSetVDecl is set to the replacement, so we don't have to worry
3848 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003849 }
3850
3851 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3852 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3853 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003854 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003855 lastCache->lruCount = ++mMaxLru;
3856
3857 return GL_NO_ERROR;
3858}
3859
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003860void VertexDeclarationCache::markStateDirty()
3861{
3862 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3863 {
3864 mAppliedVBs[i].serial = 0;
3865 }
3866
3867 mLastSetVDecl = NULL;
3868}
3869
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003870}
3871
3872extern "C"
3873{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003874gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003875{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003876 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003877}
3878
3879void glDestroyContext(gl::Context *context)
3880{
3881 delete context;
3882
3883 if (context == gl::getContext())
3884 {
3885 gl::makeCurrent(NULL, NULL, NULL);
3886 }
3887}
3888
3889void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3890{
3891 gl::makeCurrent(context, display, surface);
3892}
3893
3894gl::Context *glGetCurrentContext()
3895{
3896 return gl::getContext();
3897}
3898}