blob: fff588be392be7d0cfec8b1cf1e77b51dce0de2d [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
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000159 mSupportsCompressedTextures = false;
160 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000161 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000162 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000163 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
166Context::~Context()
167{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000168 if (mState.currentProgram != 0)
169 {
170 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
171 if (programObject)
172 {
173 programObject->release();
174 }
175 mState.currentProgram = 0;
176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000178 while (!mFramebufferMap.empty())
179 {
180 deleteFramebuffer(mFramebufferMap.begin()->first);
181 }
182
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000183 while (!mFenceMap.empty())
184 {
185 deleteFence(mFenceMap.begin()->first);
186 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000187
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000188 while (!mMultiSampleSupport.empty())
189 {
190 delete [] mMultiSampleSupport.begin()->second;
191 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
192 }
193
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000194 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000195 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000196 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000197 {
198 mState.samplerTexture[type][sampler].set(NULL);
199 }
200 }
201
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000202 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000203 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000204 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000205 }
206
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000207 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
208 {
209 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
210 }
211
212 mState.arrayBuffer.set(NULL);
213 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000214 mState.renderbuffer.set(NULL);
215
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000216 mTexture2DZero.set(NULL);
217 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000219 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000220 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000221 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000222 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000223
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000224 if (mMaskedClearSavedState)
225 {
226 mMaskedClearSavedState->Release();
227 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000228
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000229 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230}
231
232void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
233{
234 IDirect3DDevice9 *device = display->getDevice();
235
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000236 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000237 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000238 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000239
daniel@transgaming.com83921382011-01-08 05:46:00 +0000240 mVertexDataManager = new VertexDataManager(this, device);
241 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000242 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000243
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000244 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +0000245 mSupportsVertexTexture = display->getVertexTextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000246
247 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
248 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
249 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
250 mMaxRenderbufferDimension = mMaxTextureDimension;
251 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
252 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
253 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
254
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000255 const D3DFORMAT renderBufferFormats[] =
256 {
257 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000258 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000259 D3DFMT_R5G6B5,
260 D3DFMT_D24S8
261 };
262
263 int max = 0;
264 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
265 {
266 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
267 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
268 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
269
270 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
271 {
272 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
273 {
274 max = j;
275 }
276 }
277 }
278
279 mMaxSupportedSamples = max;
280
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000281 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000282 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000283 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
284 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000285 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
286 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000287
daniel@transgaming.com83921382011-01-08 05:46:00 +0000288 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
289
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000290 initExtensionString();
291
292 mState.viewportX = 0;
293 mState.viewportY = 0;
294 mState.viewportWidth = surface->getWidth();
295 mState.viewportHeight = surface->getHeight();
296
297 mState.scissorX = 0;
298 mState.scissorY = 0;
299 mState.scissorWidth = surface->getWidth();
300 mState.scissorHeight = surface->getHeight();
301
302 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000303 }
304
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
306 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000307 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000310 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000311 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312
313 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000315 if (defaultRenderTarget)
316 {
317 defaultRenderTarget->Release();
318 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000320 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000322 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000324
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000325 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326}
327
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000328// 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 +0000329void Context::markAllStateDirty()
330{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000331 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
332 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000333 mAppliedTextureSerialPS[t] = 0;
334 }
335
336 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
337 {
338 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000339 }
340
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000341 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000342 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000343 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000344 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000345 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000346
347 mClearStateDirty = true;
348 mCullStateDirty = true;
349 mDepthStateDirty = true;
350 mMaskStateDirty = true;
351 mBlendStateDirty = true;
352 mStencilStateDirty = true;
353 mPolygonOffsetStateDirty = true;
354 mScissorStateDirty = true;
355 mSampleStateDirty = true;
356 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000357 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000358}
359
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360void Context::setClearColor(float red, float green, float blue, float alpha)
361{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000362 mState.colorClearValue.red = red;
363 mState.colorClearValue.green = green;
364 mState.colorClearValue.blue = blue;
365 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366}
367
368void Context::setClearDepth(float depth)
369{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000370 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371}
372
373void Context::setClearStencil(int stencil)
374{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000375 mState.stencilClearValue = stencil;
376}
377
378void Context::setCullFace(bool enabled)
379{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000380 if (mState.cullFace != enabled)
381 {
382 mState.cullFace = enabled;
383 mCullStateDirty = true;
384 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000385}
386
387bool Context::isCullFaceEnabled() const
388{
389 return mState.cullFace;
390}
391
392void Context::setCullMode(GLenum mode)
393{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000394 if (mState.cullMode != mode)
395 {
396 mState.cullMode = mode;
397 mCullStateDirty = true;
398 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000399}
400
401void Context::setFrontFace(GLenum front)
402{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000403 if (mState.frontFace != front)
404 {
405 mState.frontFace = front;
406 mFrontFaceDirty = true;
407 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000408}
409
410void Context::setDepthTest(bool enabled)
411{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000412 if (mState.depthTest != enabled)
413 {
414 mState.depthTest = enabled;
415 mDepthStateDirty = true;
416 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000417}
418
419bool Context::isDepthTestEnabled() const
420{
421 return mState.depthTest;
422}
423
424void Context::setDepthFunc(GLenum depthFunc)
425{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000426 if (mState.depthFunc != depthFunc)
427 {
428 mState.depthFunc = depthFunc;
429 mDepthStateDirty = true;
430 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000431}
432
433void Context::setDepthRange(float zNear, float zFar)
434{
435 mState.zNear = zNear;
436 mState.zFar = zFar;
437}
438
439void Context::setBlend(bool enabled)
440{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000441 if (mState.blend != enabled)
442 {
443 mState.blend = enabled;
444 mBlendStateDirty = true;
445 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000446}
447
448bool Context::isBlendEnabled() const
449{
450 return mState.blend;
451}
452
453void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
454{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000455 if (mState.sourceBlendRGB != sourceRGB ||
456 mState.sourceBlendAlpha != sourceAlpha ||
457 mState.destBlendRGB != destRGB ||
458 mState.destBlendAlpha != destAlpha)
459 {
460 mState.sourceBlendRGB = sourceRGB;
461 mState.destBlendRGB = destRGB;
462 mState.sourceBlendAlpha = sourceAlpha;
463 mState.destBlendAlpha = destAlpha;
464 mBlendStateDirty = true;
465 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000466}
467
468void Context::setBlendColor(float red, float green, float blue, float alpha)
469{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000470 if (mState.blendColor.red != red ||
471 mState.blendColor.green != green ||
472 mState.blendColor.blue != blue ||
473 mState.blendColor.alpha != alpha)
474 {
475 mState.blendColor.red = red;
476 mState.blendColor.green = green;
477 mState.blendColor.blue = blue;
478 mState.blendColor.alpha = alpha;
479 mBlendStateDirty = true;
480 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000481}
482
483void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
484{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000485 if (mState.blendEquationRGB != rgbEquation ||
486 mState.blendEquationAlpha != alphaEquation)
487 {
488 mState.blendEquationRGB = rgbEquation;
489 mState.blendEquationAlpha = alphaEquation;
490 mBlendStateDirty = true;
491 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000492}
493
494void Context::setStencilTest(bool enabled)
495{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000496 if (mState.stencilTest != enabled)
497 {
498 mState.stencilTest = enabled;
499 mStencilStateDirty = true;
500 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000501}
502
503bool Context::isStencilTestEnabled() const
504{
505 return mState.stencilTest;
506}
507
508void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
509{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 if (mState.stencilFunc != stencilFunc ||
511 mState.stencilRef != stencilRef ||
512 mState.stencilMask != stencilMask)
513 {
514 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000515 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000516 mState.stencilMask = stencilMask;
517 mStencilStateDirty = true;
518 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000519}
520
521void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
522{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000523 if (mState.stencilBackFunc != stencilBackFunc ||
524 mState.stencilBackRef != stencilBackRef ||
525 mState.stencilBackMask != stencilBackMask)
526 {
527 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000528 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000529 mState.stencilBackMask = stencilBackMask;
530 mStencilStateDirty = true;
531 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000532}
533
534void Context::setStencilWritemask(GLuint stencilWritemask)
535{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000536 if (mState.stencilWritemask != stencilWritemask)
537 {
538 mState.stencilWritemask = stencilWritemask;
539 mStencilStateDirty = true;
540 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000541}
542
543void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
544{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000545 if (mState.stencilBackWritemask != stencilBackWritemask)
546 {
547 mState.stencilBackWritemask = stencilBackWritemask;
548 mStencilStateDirty = true;
549 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000550}
551
552void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
553{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000554 if (mState.stencilFail != stencilFail ||
555 mState.stencilPassDepthFail != stencilPassDepthFail ||
556 mState.stencilPassDepthPass != stencilPassDepthPass)
557 {
558 mState.stencilFail = stencilFail;
559 mState.stencilPassDepthFail = stencilPassDepthFail;
560 mState.stencilPassDepthPass = stencilPassDepthPass;
561 mStencilStateDirty = true;
562 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000563}
564
565void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
566{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000567 if (mState.stencilBackFail != stencilBackFail ||
568 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
569 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
570 {
571 mState.stencilBackFail = stencilBackFail;
572 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
573 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
574 mStencilStateDirty = true;
575 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000576}
577
578void Context::setPolygonOffsetFill(bool enabled)
579{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000580 if (mState.polygonOffsetFill != enabled)
581 {
582 mState.polygonOffsetFill = enabled;
583 mPolygonOffsetStateDirty = true;
584 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000585}
586
587bool Context::isPolygonOffsetFillEnabled() const
588{
589 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000590
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.polygonOffsetFactor != factor ||
596 mState.polygonOffsetUnits != units)
597 {
598 mState.polygonOffsetFactor = factor;
599 mState.polygonOffsetUnits = units;
600 mPolygonOffsetStateDirty = true;
601 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000602}
603
604void Context::setSampleAlphaToCoverage(bool enabled)
605{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000606 if (mState.sampleAlphaToCoverage != enabled)
607 {
608 mState.sampleAlphaToCoverage = enabled;
609 mSampleStateDirty = true;
610 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000611}
612
613bool Context::isSampleAlphaToCoverageEnabled() const
614{
615 return mState.sampleAlphaToCoverage;
616}
617
618void Context::setSampleCoverage(bool enabled)
619{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000620 if (mState.sampleCoverage != enabled)
621 {
622 mState.sampleCoverage = enabled;
623 mSampleStateDirty = true;
624 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000625}
626
627bool Context::isSampleCoverageEnabled() const
628{
629 return mState.sampleCoverage;
630}
631
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000632void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000633{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000634 if (mState.sampleCoverageValue != value ||
635 mState.sampleCoverageInvert != invert)
636 {
637 mState.sampleCoverageValue = value;
638 mState.sampleCoverageInvert = invert;
639 mSampleStateDirty = true;
640 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000641}
642
643void Context::setScissorTest(bool enabled)
644{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000645 if (mState.scissorTest != enabled)
646 {
647 mState.scissorTest = enabled;
648 mScissorStateDirty = true;
649 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000650}
651
652bool Context::isScissorTestEnabled() const
653{
654 return mState.scissorTest;
655}
656
657void Context::setDither(bool enabled)
658{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000659 if (mState.dither != enabled)
660 {
661 mState.dither = enabled;
662 mDitherStateDirty = true;
663 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000664}
665
666bool Context::isDitherEnabled() const
667{
668 return mState.dither;
669}
670
671void Context::setLineWidth(GLfloat width)
672{
673 mState.lineWidth = width;
674}
675
676void Context::setGenerateMipmapHint(GLenum hint)
677{
678 mState.generateMipmapHint = hint;
679}
680
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000681void Context::setFragmentShaderDerivativeHint(GLenum hint)
682{
683 mState.fragmentShaderDerivativeHint = hint;
684 // TODO: Propagate the hint to shader translator so we can write
685 // ddx, ddx_coarse, or ddx_fine depending on the hint.
686 // Ignore for now. It is valid for implementations to ignore hint.
687}
688
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000689void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
690{
691 mState.viewportX = x;
692 mState.viewportY = y;
693 mState.viewportWidth = width;
694 mState.viewportHeight = height;
695}
696
697void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
698{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000699 if (mState.scissorX != x || mState.scissorY != y ||
700 mState.scissorWidth != width || mState.scissorHeight != height)
701 {
702 mState.scissorX = x;
703 mState.scissorY = y;
704 mState.scissorWidth = width;
705 mState.scissorHeight = height;
706 mScissorStateDirty = true;
707 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000708}
709
710void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
711{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000712 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
713 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
714 {
715 mState.colorMaskRed = red;
716 mState.colorMaskGreen = green;
717 mState.colorMaskBlue = blue;
718 mState.colorMaskAlpha = alpha;
719 mMaskStateDirty = true;
720 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000721}
722
723void Context::setDepthMask(bool mask)
724{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000725 if (mState.depthMask != mask)
726 {
727 mState.depthMask = mask;
728 mMaskStateDirty = true;
729 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000730}
731
732void Context::setActiveSampler(int active)
733{
734 mState.activeSampler = active;
735}
736
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000737GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000738{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000739 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000740}
741
742GLuint Context::getDrawFramebufferHandle() const
743{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000744 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000745}
746
747GLuint Context::getRenderbufferHandle() const
748{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000749 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000750}
751
752GLuint Context::getArrayBufferHandle() const
753{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000754 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000755}
756
daniel@transgaming.com83921382011-01-08 05:46:00 +0000757void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000758{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000759 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000760}
761
daniel@transgaming.com83921382011-01-08 05:46:00 +0000762const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000763{
764 return mState.vertexAttribute[attribNum];
765}
766
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000767void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000768 GLsizei stride, const void *pointer)
769{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000770 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000771 mState.vertexAttribute[attribNum].mSize = size;
772 mState.vertexAttribute[attribNum].mType = type;
773 mState.vertexAttribute[attribNum].mNormalized = normalized;
774 mState.vertexAttribute[attribNum].mStride = stride;
775 mState.vertexAttribute[attribNum].mPointer = pointer;
776}
777
778const void *Context::getVertexAttribPointer(unsigned int attribNum) const
779{
780 return mState.vertexAttribute[attribNum].mPointer;
781}
782
daniel@transgaming.com83921382011-01-08 05:46:00 +0000783const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000784{
785 return mState.vertexAttribute;
786}
787
788void Context::setPackAlignment(GLint alignment)
789{
790 mState.packAlignment = alignment;
791}
792
793GLint Context::getPackAlignment() const
794{
795 return mState.packAlignment;
796}
797
798void Context::setUnpackAlignment(GLint alignment)
799{
800 mState.unpackAlignment = alignment;
801}
802
803GLint Context::getUnpackAlignment() const
804{
805 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806}
807
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808GLuint Context::createBuffer()
809{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000810 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811}
812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813GLuint Context::createProgram()
814{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000815 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816}
817
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000818GLuint Context::createShader(GLenum type)
819{
820 return mResourceManager->createShader(type);
821}
822
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823GLuint Context::createTexture()
824{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000825 return mResourceManager->createTexture();
826}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000828GLuint Context::createRenderbuffer()
829{
830 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831}
832
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000833// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834GLuint Context::createFramebuffer()
835{
benvanik@google.com1a233342011-04-28 19:44:39 +0000836 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000837
838 mFramebufferMap[handle] = NULL;
839
840 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841}
842
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000843GLuint Context::createFence()
844{
benvanik@google.com1a233342011-04-28 19:44:39 +0000845 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000846
847 mFenceMap[handle] = new Fence;
848
849 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000850}
851
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852void Context::deleteBuffer(GLuint buffer)
853{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000854 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855 {
856 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000858
859 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862void Context::deleteShader(GLuint shader)
863{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000864 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
867void Context::deleteProgram(GLuint program)
868{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870}
871
872void Context::deleteTexture(GLuint texture)
873{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000874 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 {
876 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000878
879 mResourceManager->deleteTexture(texture);
880}
881
882void Context::deleteRenderbuffer(GLuint renderbuffer)
883{
884 if (mResourceManager->getRenderbuffer(renderbuffer))
885 {
886 detachRenderbuffer(renderbuffer);
887 }
888
889 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890}
891
892void Context::deleteFramebuffer(GLuint framebuffer)
893{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000894 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
895
896 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897 {
898 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000899
benvanik@google.com1a233342011-04-28 19:44:39 +0000900 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000901 delete framebufferObject->second;
902 mFramebufferMap.erase(framebufferObject);
903 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000905
906void Context::deleteFence(GLuint fence)
907{
908 FenceMap::iterator fenceObject = mFenceMap.find(fence);
909
910 if (fenceObject != mFenceMap.end())
911 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000912 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000913 delete fenceObject->second;
914 mFenceMap.erase(fenceObject);
915 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000916}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000920 return mResourceManager->getBuffer(handle);
921}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000923Shader *Context::getShader(GLuint handle)
924{
925 return mResourceManager->getShader(handle);
926}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000928Program *Context::getProgram(GLuint handle)
929{
930 return mResourceManager->getProgram(handle);
931}
932
933Texture *Context::getTexture(GLuint handle)
934{
935 return mResourceManager->getTexture(handle);
936}
937
938Renderbuffer *Context::getRenderbuffer(GLuint handle)
939{
940 return mResourceManager->getRenderbuffer(handle);
941}
942
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000943Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000944{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000945 return getFramebuffer(mState.readFramebuffer);
946}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000947
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000948Framebuffer *Context::getDrawFramebuffer()
949{
950 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951}
952
953void Context::bindArrayBuffer(unsigned int buffer)
954{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000955 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000957 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958}
959
960void Context::bindElementArrayBuffer(unsigned int buffer)
961{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000962 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000964 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965}
966
967void Context::bindTexture2D(GLuint texture)
968{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000969 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000971 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
974void Context::bindTextureCubeMap(GLuint texture)
975{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000976 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000978 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979}
980
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000981void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000983 if (!getFramebuffer(framebuffer))
984 {
985 mFramebufferMap[framebuffer] = new Framebuffer();
986 }
987
988 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000989}
990
991void Context::bindDrawFramebuffer(GLuint framebuffer)
992{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000993 if (!getFramebuffer(framebuffer))
994 {
995 mFramebufferMap[framebuffer] = new Framebuffer();
996 }
997
998 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999}
1000
1001void Context::bindRenderbuffer(GLuint renderbuffer)
1002{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001003 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1004
1005 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006}
1007
1008void Context::useProgram(GLuint program)
1009{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001010 GLuint priorProgram = mState.currentProgram;
1011 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001012
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001013 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001015 Program *newProgram = mResourceManager->getProgram(program);
1016 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1017
1018 if (newProgram)
1019 {
1020 newProgram->addRef();
1021 }
1022
1023 if (oldProgram)
1024 {
1025 oldProgram->release();
1026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
1030void Context::setFramebufferZero(Framebuffer *buffer)
1031{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001032 delete mFramebufferMap[0];
1033 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034}
1035
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001036void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001038 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1039 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040}
1041
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001042Framebuffer *Context::getFramebuffer(unsigned int handle)
1043{
1044 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1045
1046 if (framebuffer == mFramebufferMap.end())
1047 {
1048 return NULL;
1049 }
1050 else
1051 {
1052 return framebuffer->second;
1053 }
1054}
1055
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001056Fence *Context::getFence(unsigned int handle)
1057{
1058 FenceMap::iterator fence = mFenceMap.find(handle);
1059
1060 if (fence == mFenceMap.end())
1061 {
1062 return NULL;
1063 }
1064 else
1065 {
1066 return fence->second;
1067 }
1068}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001069
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070Buffer *Context::getArrayBuffer()
1071{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001072 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073}
1074
1075Buffer *Context::getElementArrayBuffer()
1076{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001077 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078}
1079
1080Program *Context::getCurrentProgram()
1081{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001082 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083}
1084
1085Texture2D *Context::getTexture2D()
1086{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001087 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088}
1089
1090TextureCubeMap *Context::getTextureCubeMap()
1091{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001092 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093}
1094
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001095Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001097 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001098
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001099 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001100 {
1101 switch (type)
1102 {
1103 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001104 case TEXTURE_2D: return mTexture2DZero.get();
1105 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001106 }
1107 }
1108
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001109 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110}
1111
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001112bool Context::getBooleanv(GLenum pname, GLboolean *params)
1113{
1114 switch (pname)
1115 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001116 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1117 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1118 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001119 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001120 params[0] = mState.colorMaskRed;
1121 params[1] = mState.colorMaskGreen;
1122 params[2] = mState.colorMaskBlue;
1123 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001124 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001125 case GL_CULL_FACE: *params = mState.cullFace; break;
1126 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1127 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1128 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1129 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1130 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1131 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1132 case GL_BLEND: *params = mState.blend; break;
1133 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001134 default:
1135 return false;
1136 }
1137
1138 return true;
1139}
1140
1141bool Context::getFloatv(GLenum pname, GLfloat *params)
1142{
1143 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1144 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1145 // GetIntegerv as its native query function. As it would require conversion in any
1146 // case, this should make no difference to the calling application.
1147 switch (pname)
1148 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001149 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1150 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1151 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1152 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1153 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001154 case GL_ALIASED_LINE_WIDTH_RANGE:
1155 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1156 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1157 break;
1158 case GL_ALIASED_POINT_SIZE_RANGE:
1159 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001160 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 +00001161 break;
1162 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001163 params[0] = mState.zNear;
1164 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001165 break;
1166 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001167 params[0] = mState.colorClearValue.red;
1168 params[1] = mState.colorClearValue.green;
1169 params[2] = mState.colorClearValue.blue;
1170 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001171 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001172 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001173 params[0] = mState.blendColor.red;
1174 params[1] = mState.blendColor.green;
1175 params[2] = mState.blendColor.blue;
1176 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001177 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001178 default:
1179 return false;
1180 }
1181
1182 return true;
1183}
1184
1185bool Context::getIntegerv(GLenum pname, GLint *params)
1186{
1187 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1188 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1189 // GetIntegerv as its native query function. As it would require conversion in any
1190 // case, this should make no difference to the calling application. You may find it in
1191 // Context::getFloatv.
1192 switch (pname)
1193 {
1194 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1195 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001196 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001197 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1198 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001199 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001200 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001201 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001202 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001203 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001204 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1205 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001206 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1207 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1208 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001209 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001210 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1211 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1212 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1213 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001214 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001215 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1216 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1217 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1218 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1219 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1220 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1221 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1222 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1223 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1224 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1225 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1226 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1227 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1228 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1229 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1230 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1231 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1232 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1233 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1234 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1235 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1236 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1237 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1238 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001239 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1240 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001241 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1242 {
1243 if (supportsCompressedTextures())
1244 {
1245 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1246 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1247 *params = 2;
1248 }
1249 else
1250 {
1251 *params = 0;
1252 }
1253 }
1254 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001255 case GL_MAX_SAMPLES_ANGLE:
1256 {
1257 GLsizei maxSamples = getMaxSupportedSamples();
1258 if (maxSamples != 0)
1259 {
1260 *params = maxSamples;
1261 }
1262 else
1263 {
1264 return false;
1265 }
1266
1267 break;
1268 }
1269 case GL_SAMPLE_BUFFERS:
1270 case GL_SAMPLES:
1271 {
1272 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1273 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1274 {
1275 switch (pname)
1276 {
1277 case GL_SAMPLE_BUFFERS:
1278 if (framebuffer->getSamples() != 0)
1279 {
1280 *params = 1;
1281 }
1282 else
1283 {
1284 *params = 0;
1285 }
1286 break;
1287 case GL_SAMPLES:
1288 *params = framebuffer->getSamples();
1289 break;
1290 }
1291 }
1292 else
1293 {
1294 *params = 0;
1295 }
1296 }
1297 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001298 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1299 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1300 case GL_MAX_VIEWPORT_DIMS:
1301 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001302 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001303 params[0] = maxDimension;
1304 params[1] = maxDimension;
1305 }
1306 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001307 case GL_COMPRESSED_TEXTURE_FORMATS:
1308 {
1309 if (supportsCompressedTextures())
1310 {
1311 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1312 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1313 }
1314 }
1315 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001316 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001317 params[0] = mState.viewportX;
1318 params[1] = mState.viewportY;
1319 params[2] = mState.viewportWidth;
1320 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001321 break;
1322 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001323 params[0] = mState.scissorX;
1324 params[1] = mState.scissorY;
1325 params[2] = mState.scissorWidth;
1326 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001327 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001328 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1329 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001330 case GL_RED_BITS:
1331 case GL_GREEN_BITS:
1332 case GL_BLUE_BITS:
1333 case GL_ALPHA_BITS:
1334 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001335 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001336 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1337
1338 if (colorbuffer)
1339 {
1340 switch (pname)
1341 {
1342 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1343 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1344 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1345 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1346 }
1347 }
1348 else
1349 {
1350 *params = 0;
1351 }
1352 }
1353 break;
1354 case GL_DEPTH_BITS:
1355 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001356 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001357 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001358
1359 if (depthbuffer)
1360 {
1361 *params = depthbuffer->getDepthSize();
1362 }
1363 else
1364 {
1365 *params = 0;
1366 }
1367 }
1368 break;
1369 case GL_STENCIL_BITS:
1370 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001371 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001372 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001373
1374 if (stencilbuffer)
1375 {
1376 *params = stencilbuffer->getStencilSize();
1377 }
1378 else
1379 {
1380 *params = 0;
1381 }
1382 }
1383 break;
1384 case GL_TEXTURE_BINDING_2D:
1385 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001386 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001387 {
1388 error(GL_INVALID_OPERATION);
1389 return false;
1390 }
1391
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001392 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001393 }
1394 break;
1395 case GL_TEXTURE_BINDING_CUBE_MAP:
1396 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001397 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001398 {
1399 error(GL_INVALID_OPERATION);
1400 return false;
1401 }
1402
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001403 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001404 }
1405 break;
1406 default:
1407 return false;
1408 }
1409
1410 return true;
1411}
1412
1413bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1414{
1415 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1416 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1417 // to the fact that it is stored internally as a float, and so would require conversion
1418 // if returned from Context::getIntegerv. Since this conversion is already implemented
1419 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1420 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1421 // application.
1422 switch (pname)
1423 {
1424 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1425 case GL_SHADER_BINARY_FORMATS:
1426 {
1427 *type = GL_INT;
1428 *numParams = 0;
1429 }
1430 break;
1431 case GL_MAX_VERTEX_ATTRIBS:
1432 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1433 case GL_MAX_VARYING_VECTORS:
1434 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1435 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1436 case GL_MAX_TEXTURE_IMAGE_UNITS:
1437 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1438 case GL_MAX_RENDERBUFFER_SIZE:
1439 case GL_NUM_SHADER_BINARY_FORMATS:
1440 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1441 case GL_ARRAY_BUFFER_BINDING:
1442 case GL_FRAMEBUFFER_BINDING:
1443 case GL_RENDERBUFFER_BINDING:
1444 case GL_CURRENT_PROGRAM:
1445 case GL_PACK_ALIGNMENT:
1446 case GL_UNPACK_ALIGNMENT:
1447 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001448 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001449 case GL_RED_BITS:
1450 case GL_GREEN_BITS:
1451 case GL_BLUE_BITS:
1452 case GL_ALPHA_BITS:
1453 case GL_DEPTH_BITS:
1454 case GL_STENCIL_BITS:
1455 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1456 case GL_CULL_FACE_MODE:
1457 case GL_FRONT_FACE:
1458 case GL_ACTIVE_TEXTURE:
1459 case GL_STENCIL_FUNC:
1460 case GL_STENCIL_VALUE_MASK:
1461 case GL_STENCIL_REF:
1462 case GL_STENCIL_FAIL:
1463 case GL_STENCIL_PASS_DEPTH_FAIL:
1464 case GL_STENCIL_PASS_DEPTH_PASS:
1465 case GL_STENCIL_BACK_FUNC:
1466 case GL_STENCIL_BACK_VALUE_MASK:
1467 case GL_STENCIL_BACK_REF:
1468 case GL_STENCIL_BACK_FAIL:
1469 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1470 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1471 case GL_DEPTH_FUNC:
1472 case GL_BLEND_SRC_RGB:
1473 case GL_BLEND_SRC_ALPHA:
1474 case GL_BLEND_DST_RGB:
1475 case GL_BLEND_DST_ALPHA:
1476 case GL_BLEND_EQUATION_RGB:
1477 case GL_BLEND_EQUATION_ALPHA:
1478 case GL_STENCIL_WRITEMASK:
1479 case GL_STENCIL_BACK_WRITEMASK:
1480 case GL_STENCIL_CLEAR_VALUE:
1481 case GL_SUBPIXEL_BITS:
1482 case GL_MAX_TEXTURE_SIZE:
1483 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1484 case GL_SAMPLE_BUFFERS:
1485 case GL_SAMPLES:
1486 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1487 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1488 case GL_TEXTURE_BINDING_2D:
1489 case GL_TEXTURE_BINDING_CUBE_MAP:
1490 {
1491 *type = GL_INT;
1492 *numParams = 1;
1493 }
1494 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001495 case GL_MAX_SAMPLES_ANGLE:
1496 {
1497 if (getMaxSupportedSamples() != 0)
1498 {
1499 *type = GL_INT;
1500 *numParams = 1;
1501 }
1502 else
1503 {
1504 return false;
1505 }
1506 }
1507 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001508 case GL_MAX_VIEWPORT_DIMS:
1509 {
1510 *type = GL_INT;
1511 *numParams = 2;
1512 }
1513 break;
1514 case GL_VIEWPORT:
1515 case GL_SCISSOR_BOX:
1516 {
1517 *type = GL_INT;
1518 *numParams = 4;
1519 }
1520 break;
1521 case GL_SHADER_COMPILER:
1522 case GL_SAMPLE_COVERAGE_INVERT:
1523 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001524 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1525 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1526 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1527 case GL_SAMPLE_COVERAGE:
1528 case GL_SCISSOR_TEST:
1529 case GL_STENCIL_TEST:
1530 case GL_DEPTH_TEST:
1531 case GL_BLEND:
1532 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001533 {
1534 *type = GL_BOOL;
1535 *numParams = 1;
1536 }
1537 break;
1538 case GL_COLOR_WRITEMASK:
1539 {
1540 *type = GL_BOOL;
1541 *numParams = 4;
1542 }
1543 break;
1544 case GL_POLYGON_OFFSET_FACTOR:
1545 case GL_POLYGON_OFFSET_UNITS:
1546 case GL_SAMPLE_COVERAGE_VALUE:
1547 case GL_DEPTH_CLEAR_VALUE:
1548 case GL_LINE_WIDTH:
1549 {
1550 *type = GL_FLOAT;
1551 *numParams = 1;
1552 }
1553 break;
1554 case GL_ALIASED_LINE_WIDTH_RANGE:
1555 case GL_ALIASED_POINT_SIZE_RANGE:
1556 case GL_DEPTH_RANGE:
1557 {
1558 *type = GL_FLOAT;
1559 *numParams = 2;
1560 }
1561 break;
1562 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001563 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001564 {
1565 *type = GL_FLOAT;
1566 *numParams = 4;
1567 }
1568 break;
1569 default:
1570 return false;
1571 }
1572
1573 return true;
1574}
1575
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001576// Applies the render target surface, depth stencil surface, viewport rectangle and
1577// scissor rectangle to the Direct3D 9 device
1578bool Context::applyRenderTarget(bool ignoreViewport)
1579{
1580 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001581
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001582 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001583
1584 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1585 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001586 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587 }
1588
1589 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001590
1591 if (!renderTarget)
1592 {
1593 return false; // Context must be lost
1594 }
1595
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001596 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001598 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1599 if (renderTargetSerial != mAppliedRenderTargetSerial)
1600 {
1601 device->SetRenderTarget(0, renderTarget);
1602 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001603 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001604 }
1605
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001606 unsigned int depthbufferSerial = 0;
1607 unsigned int stencilbufferSerial = 0;
1608 if (framebufferObject->getDepthbufferType() != GL_NONE)
1609 {
1610 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001611 if (!depthStencil)
1612 {
1613 ERR("Depth stencil pointer unexpectedly null.");
1614 return false;
1615 }
1616
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001617 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1618 }
1619 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1620 {
1621 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001622 if (!depthStencil)
1623 {
1624 ERR("Depth stencil pointer unexpectedly null.");
1625 return false;
1626 }
1627
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001628 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1629 }
1630
1631 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001632 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001633 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001634 {
1635 device->SetDepthStencilSurface(depthStencil);
1636 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001637 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001638 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001639 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640
1641 D3DVIEWPORT9 viewport;
1642 D3DSURFACE_DESC desc;
1643 renderTarget->GetDesc(&desc);
1644
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001645 float zNear = clamp01(mState.zNear);
1646 float zFar = clamp01(mState.zFar);
1647
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648 if (ignoreViewport)
1649 {
1650 viewport.X = 0;
1651 viewport.Y = 0;
1652 viewport.Width = desc.Width;
1653 viewport.Height = desc.Height;
1654 viewport.MinZ = 0.0f;
1655 viewport.MaxZ = 1.0f;
1656 }
1657 else
1658 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001659 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1660 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1661 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1662 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1663 viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(desc.Height) - static_cast<LONG>(viewport.Y));
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001664 viewport.MinZ = zNear;
1665 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666 }
1667
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001668 if (viewport.Width <= 0 || viewport.Height <= 0)
1669 {
1670 return false; // Nothing to render
1671 }
1672
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673 device->SetViewport(&viewport);
1674
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001675 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001677 if (mState.scissorTest)
1678 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001679 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1680 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1681 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1682 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1683 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001684 device->SetScissorRect(&rect);
1685 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1686 }
1687 else
1688 {
1689 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1690 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001691
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001692 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693 }
1694
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 Program *programObject = getCurrentProgram();
1698
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001699 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001700 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001701 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001702
daniel@transgaming.com31754962010-11-28 02:02:52 +00001703 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001704 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1705 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1706 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001707 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001708
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001709 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001710 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001711 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001712
daniel@transgaming.com31754962010-11-28 02:02:52 +00001713 GLint depthRange = programObject->getDxDepthRangeLocation();
1714 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1715 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716 }
1717
1718 return true;
1719}
1720
1721// 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 +00001722void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723{
1724 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001725 Program *programObject = getCurrentProgram();
1726
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001727 Framebuffer *framebufferObject = getDrawFramebuffer();
1728
1729 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1730
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001731 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001732 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001733 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001735 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001736 GLint alwaysFront = !isTriangleMode(drawMode);
1737 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1738
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001739 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001743 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744 }
1745 else
1746 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748 }
1749
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001750 mCullStateDirty = false;
1751 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001753 if (mDepthStateDirty)
1754 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001755 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1758 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 }
1760 else
1761 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001762 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001764
1765 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 }
1767
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001768 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001770 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001771 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001772 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1773
1774 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1775 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1776 {
1777 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1778 }
1779 else
1780 {
1781 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1782 unorm<8>(mState.blendColor.alpha),
1783 unorm<8>(mState.blendColor.alpha),
1784 unorm<8>(mState.blendColor.alpha)));
1785 }
1786
1787 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1788 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1789 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1790
1791 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1792 mState.destBlendRGB != mState.destBlendAlpha ||
1793 mState.blendEquationRGB != mState.blendEquationAlpha)
1794 {
1795 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1796
1797 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1798 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1799 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001800 }
1801 else
1802 {
1803 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1804 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001805 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001806 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001807 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001808 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001809 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001810
1811 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812 }
1813
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001814 if (mStencilStateDirty || mFrontFaceDirty)
1815 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001816 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001817 {
1818 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1819 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1820
1821 // FIXME: Unsupported by D3D9
1822 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1823 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1824 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1825 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1826 mState.stencilRef != mState.stencilBackRef ||
1827 mState.stencilMask != mState.stencilBackMask)
1828 {
1829 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1830 return error(GL_INVALID_OPERATION);
1831 }
1832
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001833 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001834 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001835 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1836
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001837 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1838 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001839 es2dx::ConvertComparison(mState.stencilFunc));
1840
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001841 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1842 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001843
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001844 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001845 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001846 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001847 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001848 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001849 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1850
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001851 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1852 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001853 es2dx::ConvertComparison(mState.stencilBackFunc));
1854
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001855 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1856 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001857
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001858 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001859 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001860 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001861 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001862 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001863 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1864 }
1865 else
1866 {
1867 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1868 }
1869
1870 mStencilStateDirty = false;
1871 }
1872
1873 if (mMaskStateDirty)
1874 {
1875 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1876 mState.colorMaskBlue, mState.colorMaskAlpha));
1877 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1878
1879 mMaskStateDirty = false;
1880 }
1881
1882 if (mPolygonOffsetStateDirty)
1883 {
1884 if (mState.polygonOffsetFill)
1885 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001886 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001887 if (depthbuffer)
1888 {
1889 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1890 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1891 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1892 }
1893 }
1894 else
1895 {
1896 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1897 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1898 }
1899
1900 mPolygonOffsetStateDirty = false;
1901 }
1902
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001903 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001905 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001906 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001907 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001908 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001909 FIXME("Sample alpha to coverage is unimplemented.");
1910 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001911
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001912 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1913 if (mState.sampleCoverage)
1914 {
1915 unsigned int mask = 0;
1916 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001917 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001918 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001919
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001920 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001921 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001922 mask <<= 1;
1923
1924 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1925 {
1926 threshold += 1.0f;
1927 mask |= 1;
1928 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001929 }
1930 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001931
1932 if (mState.sampleCoverageInvert)
1933 {
1934 mask = ~mask;
1935 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001936
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001937 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1938 }
1939 else
1940 {
1941 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1942 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001943 }
1944 else
1945 {
1946 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001947 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001948
1949 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001950 }
1951
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001952 if (mDitherStateDirty)
1953 {
1954 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1955
1956 mDitherStateDirty = false;
1957 }
1958
1959 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960}
1961
daniel@transgaming.com83921382011-01-08 05:46:00 +00001962GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001963{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001964 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001965
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001966 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001967 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001968 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001969 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001970 }
1971
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00001972 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001973}
1974
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001975// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001976GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001977{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001978 IDirect3DDevice9 *device = getDevice();
1979 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001980
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001981 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001982 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001983 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001984 }
1985
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001986 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987}
1988
1989// Applies the shaders and shader constants to the Direct3D 9 device
1990void Context::applyShaders()
1991{
1992 IDirect3DDevice9 *device = getDevice();
1993 Program *programObject = getCurrentProgram();
1994 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1995 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1996
1997 device->SetVertexShader(vertexShader);
1998 device->SetPixelShader(pixelShader);
1999
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002000 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002001 {
2002 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002003 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002004 }
2005
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006 programObject->applyUniforms();
2007}
2008
2009// Applies the textures and sampler states to the Direct3D 9 device
2010void Context::applyTextures()
2011{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002012 applyTextures(SAMPLER_PIXEL);
2013
2014 if (mSupportsVertexTexture)
2015 {
2016 applyTextures(SAMPLER_VERTEX);
2017 }
2018}
2019
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002020// For each Direct3D 9 sampler of either the pixel or vertex stage,
2021// looks up the corresponding OpenGL texture image unit and texture type,
2022// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002023void Context::applyTextures(SamplerType type)
2024{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025 IDirect3DDevice9 *device = getDevice();
2026 Program *programObject = getCurrentProgram();
2027
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002028 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002029
2030 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002032 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002033 int d3dSampler = (type == SAMPLER_PIXEL) ? samplerIndex : D3DVERTEXTEXTURESAMPLER0 + samplerIndex;
2034 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2035
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002036 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002038 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002039
2040 Texture *texture = getSamplerTexture(textureUnit, textureType);
2041
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002042 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002043 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002044 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2045
2046 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002047 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002048 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002049 {
2050 GLenum wrapS = texture->getWrapS();
2051 GLenum wrapT = texture->getWrapT();
2052 GLenum minFilter = texture->getMinFilter();
2053 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002055 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2056 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002058 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002059 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2060 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002061 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2062 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002063 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002065 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002066 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002067 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002068 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002069 }
2070 else
2071 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002072 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002073 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002074
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002075 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002076 texture->resetDirty();
2077 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002079 else
2080 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002081 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002082 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002083 device->SetTexture(d3dSampler, NULL);
2084 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002085 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002086 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 }
2088}
2089
2090void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2091{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002092 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002093
2094 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2095 {
2096 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2097 }
2098
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002099 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2100 {
2101 return error(GL_INVALID_OPERATION);
2102 }
2103
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002105
2106 if (!renderTarget)
2107 {
2108 return; // Context must be lost, return silently
2109 }
2110
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111 IDirect3DDevice9 *device = getDevice();
2112
2113 D3DSURFACE_DESC desc;
2114 renderTarget->GetDesc(&desc);
2115
2116 IDirect3DSurface9 *systemSurface;
2117 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2118
2119 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2120 {
2121 return error(GL_OUT_OF_MEMORY);
2122 }
2123
2124 ASSERT(SUCCEEDED(result));
2125
2126 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2127 {
2128 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2129 }
2130
2131 result = device->GetRenderTargetData(renderTarget, systemSurface);
2132
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 if (FAILED(result))
2134 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135 systemSurface->Release();
2136
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002137 switch (result)
2138 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002139 case D3DERR_DRIVERINTERNALERROR:
2140 case D3DERR_DEVICELOST:
2141 return error(GL_OUT_OF_MEMORY);
2142 default:
2143 UNREACHABLE();
2144 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002145 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146 }
2147
2148 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002149 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2150 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2151 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2152 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2153 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154
2155 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2156
2157 if (FAILED(result))
2158 {
2159 UNREACHABLE();
2160 systemSurface->Release();
2161
2162 return; // No sensible error to generate
2163 }
2164
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002165 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002167 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002168 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002169 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002170
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002171 for (int j = 0; j < rect.bottom - rect.top; j++)
2172 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002173 if (desc.Format == D3DFMT_A8R8G8B8 &&
2174 format == GL_BGRA_EXT &&
2175 type == GL_UNSIGNED_BYTE)
2176 {
2177 // Fast path for EXT_read_format_bgra, given
2178 // an RGBA source buffer. Note that buffers with no
2179 // alpha go through the slow path below.
2180 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002181 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002182 (rect.right - rect.left) * 4);
2183 continue;
2184 }
2185
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186 for (int i = 0; i < rect.right - rect.left; i++)
2187 {
2188 float r;
2189 float g;
2190 float b;
2191 float a;
2192
2193 switch (desc.Format)
2194 {
2195 case D3DFMT_R5G6B5:
2196 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002197 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002198
2199 a = 1.0f;
2200 b = (rgb & 0x001F) * (1.0f / 0x001F);
2201 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2202 r = (rgb & 0xF800) * (1.0f / 0xF800);
2203 }
2204 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205 case D3DFMT_A1R5G5B5:
2206 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002207 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002208
2209 a = (argb & 0x8000) ? 1.0f : 0.0f;
2210 b = (argb & 0x001F) * (1.0f / 0x001F);
2211 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2212 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2213 }
2214 break;
2215 case D3DFMT_A8R8G8B8:
2216 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002217 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002218
2219 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2220 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2221 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2222 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2223 }
2224 break;
2225 case D3DFMT_X8R8G8B8:
2226 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002227 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002228
2229 a = 1.0f;
2230 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2231 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2232 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2233 }
2234 break;
2235 case D3DFMT_A2R10G10B10:
2236 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002237 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238
2239 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2240 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2241 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2242 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2243 }
2244 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002245 case D3DFMT_A32B32G32R32F:
2246 {
2247 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002248 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2249 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2250 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2251 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002252 }
2253 break;
2254 case D3DFMT_A16B16G16R16F:
2255 {
2256 // float formats in D3D are stored rgba, rather than the other way round
2257 float abgr[4];
2258
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002259 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002260
2261 a = abgr[3];
2262 b = abgr[2];
2263 g = abgr[1];
2264 r = abgr[0];
2265 }
2266 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267 default:
2268 UNIMPLEMENTED(); // FIXME
2269 UNREACHABLE();
2270 }
2271
2272 switch (format)
2273 {
2274 case GL_RGBA:
2275 switch (type)
2276 {
2277 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002278 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2279 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2280 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2281 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282 break;
2283 default: UNREACHABLE();
2284 }
2285 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002286 case GL_BGRA_EXT:
2287 switch (type)
2288 {
2289 case GL_UNSIGNED_BYTE:
2290 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2291 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2292 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2293 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2294 break;
2295 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2296 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2297 // this type is packed as follows:
2298 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2299 // --------------------------------------------------------------------------------
2300 // | 4th | 3rd | 2nd | 1st component |
2301 // --------------------------------------------------------------------------------
2302 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2303 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2304 ((unsigned short)(15 * a + 0.5f) << 12)|
2305 ((unsigned short)(15 * r + 0.5f) << 8) |
2306 ((unsigned short)(15 * g + 0.5f) << 4) |
2307 ((unsigned short)(15 * b + 0.5f) << 0);
2308 break;
2309 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2310 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2311 // this type is packed as follows:
2312 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2313 // --------------------------------------------------------------------------------
2314 // | 4th | 3rd | 2nd | 1st component |
2315 // --------------------------------------------------------------------------------
2316 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2317 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2318 ((unsigned short)( a + 0.5f) << 15) |
2319 ((unsigned short)(31 * r + 0.5f) << 10) |
2320 ((unsigned short)(31 * g + 0.5f) << 5) |
2321 ((unsigned short)(31 * b + 0.5f) << 0);
2322 break;
2323 default: UNREACHABLE();
2324 }
2325 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002326 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327 switch (type)
2328 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002329 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002330 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2331 ((unsigned short)(31 * b + 0.5f) << 0) |
2332 ((unsigned short)(63 * g + 0.5f) << 5) |
2333 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334 break;
2335 default: UNREACHABLE();
2336 }
2337 break;
2338 default: UNREACHABLE();
2339 }
2340 }
2341 }
2342
2343 systemSurface->UnlockRect();
2344
2345 systemSurface->Release();
2346}
2347
2348void Context::clear(GLbitfield mask)
2349{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002350 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002351
2352 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2353 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002354 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002355 }
2356
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002357 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358 IDirect3DDevice9 *device = getDevice();
2359 DWORD flags = 0;
2360
2361 if (mask & GL_COLOR_BUFFER_BIT)
2362 {
2363 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002364
2365 if (framebufferObject->getColorbufferType() != GL_NONE)
2366 {
2367 flags |= D3DCLEAR_TARGET;
2368 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002369 }
2370
2371 if (mask & GL_DEPTH_BUFFER_BIT)
2372 {
2373 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002374 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 {
2376 flags |= D3DCLEAR_ZBUFFER;
2377 }
2378 }
2379
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380 GLuint stencilUnmasked = 0x0;
2381
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002382 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002385 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002387 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002388 if (!depthStencil)
2389 {
2390 ERR("Depth stencil pointer unexpectedly null.");
2391 return;
2392 }
2393
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002394 D3DSURFACE_DESC desc;
2395 depthStencil->GetDesc(&desc);
2396
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002397 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002398 stencilUnmasked = (0x1 << stencilSize) - 1;
2399
2400 if (stencilUnmasked != 0x0)
2401 {
2402 flags |= D3DCLEAR_STENCIL;
2403 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404 }
2405 }
2406
2407 if (mask != 0)
2408 {
2409 return error(GL_INVALID_VALUE);
2410 }
2411
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002412 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2413 {
2414 return;
2415 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002417 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002418 unorm<8>(mState.colorClearValue.red),
2419 unorm<8>(mState.colorClearValue.green),
2420 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002421 float depth = clamp01(mState.depthClearValue);
2422 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423
2424 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2425
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002426 if (!renderTarget)
2427 {
2428 return; // Context must be lost, return silently
2429 }
2430
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 D3DSURFACE_DESC desc;
2432 renderTarget->GetDesc(&desc);
2433
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002434 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435
2436 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002437 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002439 !(mState.colorMaskRed && mState.colorMaskGreen &&
2440 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441
2442 if (needMaskedColorClear || needMaskedStencilClear)
2443 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002444 // State which is altered in all paths from this point to the clear call is saved.
2445 // State which is altered in only some paths will be flagged dirty in the case that
2446 // that path is taken.
2447 HRESULT hr;
2448 if (mMaskedClearSavedState == NULL)
2449 {
2450 hr = device->BeginStateBlock();
2451 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2452
2453 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2454 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2455 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2456 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2457 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2458 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2459 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2460 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2461 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2462 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2463 device->SetPixelShader(NULL);
2464 device->SetVertexShader(NULL);
2465 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002466 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2467 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2468 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2469 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2470 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2471 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2472 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002473
2474 hr = device->EndStateBlock(&mMaskedClearSavedState);
2475 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2476 }
2477
2478 ASSERT(mMaskedClearSavedState != NULL);
2479
2480 if (mMaskedClearSavedState != NULL)
2481 {
2482 hr = mMaskedClearSavedState->Capture();
2483 ASSERT(SUCCEEDED(hr));
2484 }
2485
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002486 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2487 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2488 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2489 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2490 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2491 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2492 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2493 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2494
2495 if (flags & D3DCLEAR_TARGET)
2496 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002497 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498 }
2499 else
2500 {
2501 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2502 }
2503
2504 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2505 {
2506 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2507 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2508 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2509 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002510 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002511 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2513 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002514 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002515 }
2516 else
2517 {
2518 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2519 }
2520
2521 device->SetPixelShader(NULL);
2522 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002523 device->SetFVF(D3DFVF_XYZRHW);
2524 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2525 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2526 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2527 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2528 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2529 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2530 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002532 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2533 quad[0][0] = -0.5f;
2534 quad[0][1] = desc.Height - 0.5f;
2535 quad[0][2] = 0.0f;
2536 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002538 quad[1][0] = desc.Width - 0.5f;
2539 quad[1][1] = desc.Height - 0.5f;
2540 quad[1][2] = 0.0f;
2541 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002543 quad[2][0] = -0.5f;
2544 quad[2][1] = -0.5f;
2545 quad[2][2] = 0.0f;
2546 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002547
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002548 quad[3][0] = desc.Width - 0.5f;
2549 quad[3][1] = -0.5f;
2550 quad[3][2] = 0.0f;
2551 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002553 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002554 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555
2556 if (flags & D3DCLEAR_ZBUFFER)
2557 {
2558 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2559 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2560 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2561 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002562
2563 if (mMaskedClearSavedState != NULL)
2564 {
2565 mMaskedClearSavedState->Apply();
2566 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002568 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569 {
2570 device->Clear(0, NULL, flags, color, depth, stencil);
2571 }
2572}
2573
2574void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2575{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002576 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577 {
2578 return error(GL_INVALID_OPERATION);
2579 }
2580
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002581 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002582 IDirect3DDevice9 *device = getDevice();
2583 D3DPRIMITIVETYPE primitiveType;
2584 int primitiveCount;
2585
2586 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2587 return error(GL_INVALID_ENUM);
2588
2589 if (primitiveCount <= 0)
2590 {
2591 return;
2592 }
2593
2594 if (!applyRenderTarget(false))
2595 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002596 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597 }
2598
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002599 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002600
daniel@transgaming.com83921382011-01-08 05:46:00 +00002601 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002602 if (err != GL_NO_ERROR)
2603 {
2604 return error(err);
2605 }
2606
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002607 applyShaders();
2608 applyTextures();
2609
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002610 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002611 {
2612 return error(GL_INVALID_OPERATION);
2613 }
2614
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002615 if (!cullSkipsDraw(mode))
2616 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002617 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002618
2619 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002620
2621 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2622 {
2623 drawClosingLine(first, first + count - 1);
2624 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002625 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626}
2627
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002628void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002630 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631 {
2632 return error(GL_INVALID_OPERATION);
2633 }
2634
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002635 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002636 {
2637 return error(GL_INVALID_OPERATION);
2638 }
2639
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002640 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641 IDirect3DDevice9 *device = getDevice();
2642 D3DPRIMITIVETYPE primitiveType;
2643 int primitiveCount;
2644
2645 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2646 return error(GL_INVALID_ENUM);
2647
2648 if (primitiveCount <= 0)
2649 {
2650 return;
2651 }
2652
2653 if (!applyRenderTarget(false))
2654 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002655 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656 }
2657
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002658 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002659
2660 TranslatedIndexData indexInfo;
2661 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2662 if (err != GL_NO_ERROR)
2663 {
2664 return error(err);
2665 }
2666
daniel@transgaming.com83921382011-01-08 05:46:00 +00002667 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2668 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002669 if (err != GL_NO_ERROR)
2670 {
2671 return error(err);
2672 }
2673
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002674 applyShaders();
2675 applyTextures();
2676
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002677 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002678 {
2679 return error(GL_INVALID_OPERATION);
2680 }
2681
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002682 if (!cullSkipsDraw(mode))
2683 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002684 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002685
daniel@transgaming.com83921382011-01-08 05:46:00 +00002686 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002687
2688 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2689 {
2690 drawClosingLine(count, type, indices);
2691 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002692 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002693}
2694
2695void Context::finish()
2696{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002697 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002698 IDirect3DDevice9 *device = getDevice();
2699 IDirect3DQuery9 *occlusionQuery = NULL;
2700
2701 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2702
2703 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2704 {
2705 return error(GL_OUT_OF_MEMORY);
2706 }
2707
2708 ASSERT(SUCCEEDED(result));
2709
2710 if (occlusionQuery)
2711 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002712 IDirect3DStateBlock9 *savedState = NULL;
2713 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2714
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002715 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2716 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002717
2718 // Render something outside the render target
2719 device->SetPixelShader(NULL);
2720 device->SetVertexShader(NULL);
2721 device->SetFVF(D3DFVF_XYZRHW);
2722 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002723 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002726 result = occlusionQuery->Issue(D3DISSUE_END);
2727 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002728
2729 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2730 {
2731 // Keep polling, but allow other threads to do something useful first
2732 Sleep(0);
2733 }
2734
2735 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002736
2737 if (savedState)
2738 {
2739 savedState->Apply();
2740 savedState->Release();
2741 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742 }
2743}
2744
2745void Context::flush()
2746{
2747 IDirect3DDevice9 *device = getDevice();
2748 IDirect3DQuery9 *eventQuery = NULL;
2749
2750 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2751
2752 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2753 {
2754 return error(GL_OUT_OF_MEMORY);
2755 }
2756
2757 ASSERT(SUCCEEDED(result));
2758
2759 if (eventQuery)
2760 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002761 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2762 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002763
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002764 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002765 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002766
2767 if (result == D3DERR_DEVICELOST)
2768 {
2769 error(GL_OUT_OF_MEMORY);
2770 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002771 }
2772}
2773
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002774void Context::drawClosingLine(unsigned int first, unsigned int last)
2775{
2776 IDirect3DDevice9 *device = getDevice();
2777 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002778 bool succeeded = false;
2779 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002780
2781 if (supports32bitIndices())
2782 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002783 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002784
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002785 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002786 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002787 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2788 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002789
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002790 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2791
2792 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2793 if (data)
2794 {
2795 data[0] = last;
2796 data[1] = first;
2797 mClosingIB->unmap();
2798 offset /= 4;
2799 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002800 }
2801 }
2802 else
2803 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002804 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002805
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002806 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002807 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002808 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2809 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002810
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002811 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2812
2813 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2814 if (data)
2815 {
2816 data[0] = last;
2817 data[1] = first;
2818 mClosingIB->unmap();
2819 offset /= 2;
2820 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002821 }
2822 }
2823
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002824 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002825 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002826 device->SetIndices(mClosingIB->getBuffer());
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002827
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002828 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002829 }
2830 else
2831 {
2832 ERR("Could not create an index buffer for closing a line loop.");
2833 error(GL_OUT_OF_MEMORY);
2834 }
2835}
2836
2837void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2838{
2839 unsigned int first = 0;
2840 unsigned int last = 0;
2841
2842 if (mState.elementArrayBuffer.get())
2843 {
2844 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2845 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2846 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2847 }
2848
2849 switch (type)
2850 {
2851 case GL_UNSIGNED_BYTE:
2852 first = static_cast<const GLubyte*>(indices)[0];
2853 last = static_cast<const GLubyte*>(indices)[count - 1];
2854 break;
2855 case GL_UNSIGNED_SHORT:
2856 first = static_cast<const GLushort*>(indices)[0];
2857 last = static_cast<const GLushort*>(indices)[count - 1];
2858 break;
2859 case GL_UNSIGNED_INT:
2860 first = static_cast<const GLuint*>(indices)[0];
2861 last = static_cast<const GLuint*>(indices)[count - 1];
2862 break;
2863 default: UNREACHABLE();
2864 }
2865
2866 drawClosingLine(first, last);
2867}
2868
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869void Context::recordInvalidEnum()
2870{
2871 mInvalidEnum = true;
2872}
2873
2874void Context::recordInvalidValue()
2875{
2876 mInvalidValue = true;
2877}
2878
2879void Context::recordInvalidOperation()
2880{
2881 mInvalidOperation = true;
2882}
2883
2884void Context::recordOutOfMemory()
2885{
2886 mOutOfMemory = true;
2887}
2888
2889void Context::recordInvalidFramebufferOperation()
2890{
2891 mInvalidFramebufferOperation = true;
2892}
2893
2894// Get one of the recorded errors and clear its flag, if any.
2895// [OpenGL ES 2.0.24] section 2.5 page 13.
2896GLenum Context::getError()
2897{
2898 if (mInvalidEnum)
2899 {
2900 mInvalidEnum = false;
2901
2902 return GL_INVALID_ENUM;
2903 }
2904
2905 if (mInvalidValue)
2906 {
2907 mInvalidValue = false;
2908
2909 return GL_INVALID_VALUE;
2910 }
2911
2912 if (mInvalidOperation)
2913 {
2914 mInvalidOperation = false;
2915
2916 return GL_INVALID_OPERATION;
2917 }
2918
2919 if (mOutOfMemory)
2920 {
2921 mOutOfMemory = false;
2922
2923 return GL_OUT_OF_MEMORY;
2924 }
2925
2926 if (mInvalidFramebufferOperation)
2927 {
2928 mInvalidFramebufferOperation = false;
2929
2930 return GL_INVALID_FRAMEBUFFER_OPERATION;
2931 }
2932
2933 return GL_NO_ERROR;
2934}
2935
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002936bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002937{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002938 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002939}
2940
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002941int Context::getMaximumVaryingVectors() const
2942{
2943 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2944}
2945
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002946int Context::getMaximumVertexTextureImageUnits() const
2947{
2948 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
2949}
2950
2951int Context::getMaximumCombinedTextureImageUnits() const
2952{
2953 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
2954}
2955
daniel@transgaming.com458da142010-11-28 02:03:02 +00002956int Context::getMaximumFragmentUniformVectors() const
2957{
2958 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2959}
2960
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002961int Context::getMaxSupportedSamples() const
2962{
2963 return mMaxSupportedSamples;
2964}
2965
2966int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2967{
2968 if (requested == 0)
2969 {
2970 return requested;
2971 }
2972
2973 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2974 if (itr == mMultiSampleSupport.end())
2975 {
2976 return -1;
2977 }
2978
2979 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2980 {
2981 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2982 {
2983 return i;
2984 }
2985 }
2986
2987 return -1;
2988}
2989
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002990bool Context::supportsEventQueries() const
2991{
2992 return mSupportsEventQueries;
2993}
2994
daniel@transgaming.com01868132010-08-24 19:21:17 +00002995bool Context::supportsCompressedTextures() const
2996{
2997 return mSupportsCompressedTextures;
2998}
2999
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003000bool Context::supportsFloatTextures() const
3001{
3002 return mSupportsFloatTextures;
3003}
3004
3005bool Context::supportsFloatLinearFilter() const
3006{
3007 return mSupportsFloatLinearFilter;
3008}
3009
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003010bool Context::supportsFloatRenderableTextures() const
3011{
3012 return mSupportsFloatRenderableTextures;
3013}
3014
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003015bool Context::supportsHalfFloatTextures() const
3016{
3017 return mSupportsHalfFloatTextures;
3018}
3019
3020bool Context::supportsHalfFloatLinearFilter() const
3021{
3022 return mSupportsHalfFloatLinearFilter;
3023}
3024
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003025bool Context::supportsHalfFloatRenderableTextures() const
3026{
3027 return mSupportsHalfFloatRenderableTextures;
3028}
3029
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003030int Context::getMaximumRenderbufferDimension() const
3031{
3032 return mMaxRenderbufferDimension;
3033}
3034
3035int Context::getMaximumTextureDimension() const
3036{
3037 return mMaxTextureDimension;
3038}
3039
3040int Context::getMaximumCubeTextureDimension() const
3041{
3042 return mMaxCubeTextureDimension;
3043}
3044
3045int Context::getMaximumTextureLevel() const
3046{
3047 return mMaxTextureLevel;
3048}
3049
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003050bool Context::supportsLuminanceTextures() const
3051{
3052 return mSupportsLuminanceTextures;
3053}
3054
3055bool Context::supportsLuminanceAlphaTextures() const
3056{
3057 return mSupportsLuminanceAlphaTextures;
3058}
3059
daniel@transgaming.com83921382011-01-08 05:46:00 +00003060bool Context::supports32bitIndices() const
3061{
3062 return mSupports32bitIndices;
3063}
3064
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003065void Context::detachBuffer(GLuint buffer)
3066{
3067 // [OpenGL ES 2.0.24] section 2.9 page 22:
3068 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3069 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3070
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003071 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003072 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003073 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003074 }
3075
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003076 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003077 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003078 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003079 }
3080
3081 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3082 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003083 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003084 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003085 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003086 }
3087 }
3088}
3089
3090void Context::detachTexture(GLuint texture)
3091{
3092 // [OpenGL ES 2.0.24] section 3.8 page 84:
3093 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3094 // rebound to texture object zero
3095
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003096 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003097 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003098 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003099 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003100 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003101 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003102 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003103 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003104 }
3105 }
3106
3107 // [OpenGL ES 2.0.24] section 4.4 page 112:
3108 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3109 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3110 // image was attached in the currently bound framebuffer.
3111
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003112 Framebuffer *readFramebuffer = getReadFramebuffer();
3113 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003114
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003115 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003116 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003117 readFramebuffer->detachTexture(texture);
3118 }
3119
3120 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3121 {
3122 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003123 }
3124}
3125
3126void Context::detachFramebuffer(GLuint framebuffer)
3127{
3128 // [OpenGL ES 2.0.24] section 4.4 page 107:
3129 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3130 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3131
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003132 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003133 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003134 bindReadFramebuffer(0);
3135 }
3136
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003137 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003138 {
3139 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003140 }
3141}
3142
3143void Context::detachRenderbuffer(GLuint renderbuffer)
3144{
3145 // [OpenGL ES 2.0.24] section 4.4 page 109:
3146 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3147 // had been executed with the target RENDERBUFFER and name of zero.
3148
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003149 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003150 {
3151 bindRenderbuffer(0);
3152 }
3153
3154 // [OpenGL ES 2.0.24] section 4.4 page 111:
3155 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3156 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3157 // point to which this image was attached in the currently bound framebuffer.
3158
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003159 Framebuffer *readFramebuffer = getReadFramebuffer();
3160 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003161
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003162 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003163 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003164 readFramebuffer->detachRenderbuffer(renderbuffer);
3165 }
3166
3167 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3168 {
3169 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003170 }
3171}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003172
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003173Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003174{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003175 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003176
3177 if (t == NULL)
3178 {
3179 static const GLubyte color[] = { 0, 0, 0, 255 };
3180
3181 switch (type)
3182 {
3183 default:
3184 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003185 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003186
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003187 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003188 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003189 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003190 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003191 t = incomplete2d;
3192 }
3193 break;
3194
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003195 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003196 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003197 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003198
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003199 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3200 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3201 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3202 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3203 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3204 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003205
3206 t = incompleteCube;
3207 }
3208 break;
3209 }
3210
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003211 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003212 }
3213
3214 return t;
3215}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003216
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003217bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003218{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003219 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003220}
3221
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003222bool Context::isTriangleMode(GLenum drawMode)
3223{
3224 switch (drawMode)
3225 {
3226 case GL_TRIANGLES:
3227 case GL_TRIANGLE_FAN:
3228 case GL_TRIANGLE_STRIP:
3229 return true;
3230 case GL_POINTS:
3231 case GL_LINES:
3232 case GL_LINE_LOOP:
3233 case GL_LINE_STRIP:
3234 return false;
3235 default: UNREACHABLE();
3236 }
3237
3238 return false;
3239}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003240
3241void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3242{
3243 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3244
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003245 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3246 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3247 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3248 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003249
daniel@transgaming.com83921382011-01-08 05:46:00 +00003250 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003251}
3252
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003253void Context::initExtensionString()
3254{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003255 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003256 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3257 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003258 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003259 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003260 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003261
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003262 if (supportsEventQueries())
3263 {
3264 mExtensionString += "GL_NV_fence ";
3265 }
3266
daniel@transgaming.com01868132010-08-24 19:21:17 +00003267 if (supportsCompressedTextures())
3268 {
3269 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3270 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003271
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003272 if (supportsFloatTextures())
3273 {
3274 mExtensionString += "GL_OES_texture_float ";
3275 }
3276
3277 if (supportsHalfFloatTextures())
3278 {
3279 mExtensionString += "GL_OES_texture_half_float ";
3280 }
3281
3282 if (supportsFloatLinearFilter())
3283 {
3284 mExtensionString += "GL_OES_texture_float_linear ";
3285 }
3286
3287 if (supportsHalfFloatLinearFilter())
3288 {
3289 mExtensionString += "GL_OES_texture_half_float_linear ";
3290 }
3291
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003292 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003293 {
3294 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3295 }
3296
daniel@transgaming.com83921382011-01-08 05:46:00 +00003297 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003298 {
3299 mExtensionString += "GL_OES_element_index_uint ";
3300 }
3301
3302 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3303 if (end != std::string::npos)
3304 {
3305 mExtensionString.resize(end+1);
3306 }
3307}
3308
3309const char *Context::getExtensionString() const
3310{
3311 return mExtensionString.c_str();
3312}
3313
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003314void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3315 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3316 GLbitfield mask)
3317{
3318 IDirect3DDevice9 *device = getDevice();
3319
3320 Framebuffer *readFramebuffer = getReadFramebuffer();
3321 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3322
3323 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3324 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3325 {
3326 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3327 }
3328
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003329 if (drawFramebuffer->getSamples() != 0)
3330 {
3331 return error(GL_INVALID_OPERATION);
3332 }
3333
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003334 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3335 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3336 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3337 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3338
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003339 RECT sourceRect;
3340 RECT destRect;
3341
3342 if (srcX0 < srcX1)
3343 {
3344 sourceRect.left = srcX0;
3345 sourceRect.right = srcX1;
3346 destRect.left = dstX0;
3347 destRect.right = dstX1;
3348 }
3349 else
3350 {
3351 sourceRect.left = srcX1;
3352 destRect.left = dstX1;
3353 sourceRect.right = srcX0;
3354 destRect.right = dstX0;
3355 }
3356
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003357 if (srcY0 < srcY1)
3358 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003359 sourceRect.top = readBufferHeight - srcY1;
3360 destRect.top = drawBufferHeight - dstY1;
3361 sourceRect.bottom = readBufferHeight - srcY0;
3362 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003363 }
3364 else
3365 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003366 sourceRect.top = readBufferHeight - srcY0;
3367 destRect.top = drawBufferHeight - dstY0;
3368 sourceRect.bottom = readBufferHeight - srcY1;
3369 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003370 }
3371
3372 RECT sourceScissoredRect = sourceRect;
3373 RECT destScissoredRect = destRect;
3374
3375 if (mState.scissorTest)
3376 {
3377 // Only write to parts of the destination framebuffer which pass the scissor test
3378 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3379 // rect will be checked against scissorY, rather than the bottom.
3380 if (destRect.left < mState.scissorX)
3381 {
3382 int xDiff = mState.scissorX - destRect.left;
3383 destScissoredRect.left = mState.scissorX;
3384 sourceScissoredRect.left += xDiff;
3385 }
3386
3387 if (destRect.right > mState.scissorX + mState.scissorWidth)
3388 {
3389 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3390 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3391 sourceScissoredRect.right -= xDiff;
3392 }
3393
3394 if (destRect.top < mState.scissorY)
3395 {
3396 int yDiff = mState.scissorY - destRect.top;
3397 destScissoredRect.top = mState.scissorY;
3398 sourceScissoredRect.top += yDiff;
3399 }
3400
3401 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3402 {
3403 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3404 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3405 sourceScissoredRect.bottom -= yDiff;
3406 }
3407 }
3408
3409 bool blitRenderTarget = false;
3410 bool blitDepthStencil = false;
3411
3412 RECT sourceTrimmedRect = sourceScissoredRect;
3413 RECT destTrimmedRect = destScissoredRect;
3414
3415 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3416 // the actual draw and read surfaces.
3417 if (sourceTrimmedRect.left < 0)
3418 {
3419 int xDiff = 0 - sourceTrimmedRect.left;
3420 sourceTrimmedRect.left = 0;
3421 destTrimmedRect.left += xDiff;
3422 }
3423
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003424 if (sourceTrimmedRect.right > readBufferWidth)
3425 {
3426 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3427 sourceTrimmedRect.right = readBufferWidth;
3428 destTrimmedRect.right -= xDiff;
3429 }
3430
3431 if (sourceTrimmedRect.top < 0)
3432 {
3433 int yDiff = 0 - sourceTrimmedRect.top;
3434 sourceTrimmedRect.top = 0;
3435 destTrimmedRect.top += yDiff;
3436 }
3437
3438 if (sourceTrimmedRect.bottom > readBufferHeight)
3439 {
3440 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3441 sourceTrimmedRect.bottom = readBufferHeight;
3442 destTrimmedRect.bottom -= yDiff;
3443 }
3444
3445 if (destTrimmedRect.left < 0)
3446 {
3447 int xDiff = 0 - destTrimmedRect.left;
3448 destTrimmedRect.left = 0;
3449 sourceTrimmedRect.left += xDiff;
3450 }
3451
3452 if (destTrimmedRect.right > drawBufferWidth)
3453 {
3454 int xDiff = destTrimmedRect.right - drawBufferWidth;
3455 destTrimmedRect.right = drawBufferWidth;
3456 sourceTrimmedRect.right -= xDiff;
3457 }
3458
3459 if (destTrimmedRect.top < 0)
3460 {
3461 int yDiff = 0 - destTrimmedRect.top;
3462 destTrimmedRect.top = 0;
3463 sourceTrimmedRect.top += yDiff;
3464 }
3465
3466 if (destTrimmedRect.bottom > drawBufferHeight)
3467 {
3468 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3469 destTrimmedRect.bottom = drawBufferHeight;
3470 sourceTrimmedRect.bottom -= yDiff;
3471 }
3472
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003473 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003474 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3475 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3476 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3477 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003478 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3479 {
3480 partialBufferCopy = true;
3481 }
3482
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003483 if (mask & GL_COLOR_BUFFER_BIT)
3484 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003485 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3486 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3487 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3488 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3489 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003490 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3491 {
3492 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3493 return error(GL_INVALID_OPERATION);
3494 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003495
3496 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3497 {
3498 return error(GL_INVALID_OPERATION);
3499 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003500
3501 blitRenderTarget = true;
3502
3503 }
3504
3505 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3506 {
3507 DepthStencilbuffer *readDSBuffer = NULL;
3508 DepthStencilbuffer *drawDSBuffer = NULL;
3509
3510 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3511 // both a depth and stencil buffer, it will be the same buffer.
3512
3513 if (mask & GL_DEPTH_BUFFER_BIT)
3514 {
3515 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3516 {
3517 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3518 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3519 {
3520 return error(GL_INVALID_OPERATION);
3521 }
3522
3523 blitDepthStencil = true;
3524 readDSBuffer = readFramebuffer->getDepthbuffer();
3525 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3526 }
3527 }
3528
3529 if (mask & GL_STENCIL_BUFFER_BIT)
3530 {
3531 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3532 {
3533 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3534 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3535 {
3536 return error(GL_INVALID_OPERATION);
3537 }
3538
3539 blitDepthStencil = true;
3540 readDSBuffer = readFramebuffer->getStencilbuffer();
3541 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3542 }
3543 }
3544
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003545 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003546 {
3547 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3548 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3549 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003550
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003551 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3552 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003553 {
3554 return error(GL_INVALID_OPERATION);
3555 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003556 }
3557
3558 if (blitRenderTarget || blitDepthStencil)
3559 {
3560 egl::Display *display = getDisplay();
3561 display->endScene();
3562
3563 if (blitRenderTarget)
3564 {
3565 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3566 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3567
3568 if (FAILED(result))
3569 {
3570 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3571 return;
3572 }
3573 }
3574
3575 if (blitDepthStencil)
3576 {
3577 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3578
3579 if (FAILED(result))
3580 {
3581 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3582 return;
3583 }
3584 }
3585 }
3586}
3587
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003588VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3589{
3590 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3591 {
3592 mVertexDeclCache[i].vertexDeclaration = NULL;
3593 mVertexDeclCache[i].lruCount = 0;
3594 }
3595}
3596
3597VertexDeclarationCache::~VertexDeclarationCache()
3598{
3599 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3600 {
3601 if (mVertexDeclCache[i].vertexDeclaration)
3602 {
3603 mVertexDeclCache[i].vertexDeclaration->Release();
3604 }
3605 }
3606}
3607
3608GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3609{
3610 IDirect3DDevice9 *device = getDevice();
3611
3612 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3613 D3DVERTEXELEMENT9 *element = &elements[0];
3614
3615 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3616 {
3617 if (attributes[i].active)
3618 {
3619 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3620
3621 element->Stream = i;
3622 element->Offset = 0;
3623 element->Type = attributes[i].type;
3624 element->Method = D3DDECLMETHOD_DEFAULT;
3625 element->Usage = D3DDECLUSAGE_TEXCOORD;
3626 element->UsageIndex = program->getSemanticIndex(i);
3627 element++;
3628 }
3629 }
3630
3631 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3632 *(element++) = end;
3633
3634 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3635 {
3636 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3637 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3638 {
3639 entry->lruCount = ++mMaxLru;
3640 device->SetVertexDeclaration(entry->vertexDeclaration);
3641
3642 return GL_NO_ERROR;
3643 }
3644 }
3645
3646 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3647
3648 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3649 {
3650 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3651 {
3652 lastCache = &mVertexDeclCache[i];
3653 }
3654 }
3655
3656 if (lastCache->vertexDeclaration != NULL)
3657 {
3658 lastCache->vertexDeclaration->Release();
3659 lastCache->vertexDeclaration = NULL;
3660 }
3661
3662 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3663 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3664 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3665 lastCache->lruCount = ++mMaxLru;
3666
3667 return GL_NO_ERROR;
3668}
3669
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003670}
3671
3672extern "C"
3673{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003674gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003675{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003676 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003677}
3678
3679void glDestroyContext(gl::Context *context)
3680{
3681 delete context;
3682
3683 if (context == gl::getContext())
3684 {
3685 gl::makeCurrent(NULL, NULL, NULL);
3686 }
3687}
3688
3689void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3690{
3691 gl::makeCurrent(context, display, surface);
3692}
3693
3694gl::Context *glGetCurrentContext()
3695{
3696 return gl::getContext();
3697}
3698}