blob: 2105823cb3eb9db4b882a898380771e3dc7ecf78 [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.com9ecb9f92010-07-28 19:21:12 +0000194 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
195 {
196 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
197 {
198 mState.samplerTexture[type][sampler].set(NULL);
199 }
200 }
201
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000202 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
203 {
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.coma9eb5da2011-03-21 16:39:16 +0000333 mAppliedTextureSerial[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000334 }
335
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000336 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000337 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000338 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000339 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000340 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000341
342 mClearStateDirty = true;
343 mCullStateDirty = true;
344 mDepthStateDirty = true;
345 mMaskStateDirty = true;
346 mBlendStateDirty = true;
347 mStencilStateDirty = true;
348 mPolygonOffsetStateDirty = true;
349 mScissorStateDirty = true;
350 mSampleStateDirty = true;
351 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000352 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000353}
354
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355void Context::setClearColor(float red, float green, float blue, float alpha)
356{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000357 mState.colorClearValue.red = red;
358 mState.colorClearValue.green = green;
359 mState.colorClearValue.blue = blue;
360 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361}
362
363void Context::setClearDepth(float depth)
364{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000365 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366}
367
368void Context::setClearStencil(int stencil)
369{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000370 mState.stencilClearValue = stencil;
371}
372
373void Context::setCullFace(bool enabled)
374{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000375 if (mState.cullFace != enabled)
376 {
377 mState.cullFace = enabled;
378 mCullStateDirty = true;
379 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000380}
381
382bool Context::isCullFaceEnabled() const
383{
384 return mState.cullFace;
385}
386
387void Context::setCullMode(GLenum mode)
388{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000389 if (mState.cullMode != mode)
390 {
391 mState.cullMode = mode;
392 mCullStateDirty = true;
393 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000394}
395
396void Context::setFrontFace(GLenum front)
397{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000398 if (mState.frontFace != front)
399 {
400 mState.frontFace = front;
401 mFrontFaceDirty = true;
402 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000403}
404
405void Context::setDepthTest(bool enabled)
406{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000407 if (mState.depthTest != enabled)
408 {
409 mState.depthTest = enabled;
410 mDepthStateDirty = true;
411 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000412}
413
414bool Context::isDepthTestEnabled() const
415{
416 return mState.depthTest;
417}
418
419void Context::setDepthFunc(GLenum depthFunc)
420{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000421 if (mState.depthFunc != depthFunc)
422 {
423 mState.depthFunc = depthFunc;
424 mDepthStateDirty = true;
425 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000426}
427
428void Context::setDepthRange(float zNear, float zFar)
429{
430 mState.zNear = zNear;
431 mState.zFar = zFar;
432}
433
434void Context::setBlend(bool enabled)
435{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000436 if (mState.blend != enabled)
437 {
438 mState.blend = enabled;
439 mBlendStateDirty = true;
440 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000441}
442
443bool Context::isBlendEnabled() const
444{
445 return mState.blend;
446}
447
448void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
449{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000450 if (mState.sourceBlendRGB != sourceRGB ||
451 mState.sourceBlendAlpha != sourceAlpha ||
452 mState.destBlendRGB != destRGB ||
453 mState.destBlendAlpha != destAlpha)
454 {
455 mState.sourceBlendRGB = sourceRGB;
456 mState.destBlendRGB = destRGB;
457 mState.sourceBlendAlpha = sourceAlpha;
458 mState.destBlendAlpha = destAlpha;
459 mBlendStateDirty = true;
460 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000461}
462
463void Context::setBlendColor(float red, float green, float blue, float alpha)
464{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000465 if (mState.blendColor.red != red ||
466 mState.blendColor.green != green ||
467 mState.blendColor.blue != blue ||
468 mState.blendColor.alpha != alpha)
469 {
470 mState.blendColor.red = red;
471 mState.blendColor.green = green;
472 mState.blendColor.blue = blue;
473 mState.blendColor.alpha = alpha;
474 mBlendStateDirty = true;
475 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000476}
477
478void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
479{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000480 if (mState.blendEquationRGB != rgbEquation ||
481 mState.blendEquationAlpha != alphaEquation)
482 {
483 mState.blendEquationRGB = rgbEquation;
484 mState.blendEquationAlpha = alphaEquation;
485 mBlendStateDirty = true;
486 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000487}
488
489void Context::setStencilTest(bool enabled)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.stencilTest != enabled)
492 {
493 mState.stencilTest = enabled;
494 mStencilStateDirty = true;
495 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000496}
497
498bool Context::isStencilTestEnabled() const
499{
500 return mState.stencilTest;
501}
502
503void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
504{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000505 if (mState.stencilFunc != stencilFunc ||
506 mState.stencilRef != stencilRef ||
507 mState.stencilMask != stencilMask)
508 {
509 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000510 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000511 mState.stencilMask = stencilMask;
512 mStencilStateDirty = true;
513 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000514}
515
516void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
517{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000518 if (mState.stencilBackFunc != stencilBackFunc ||
519 mState.stencilBackRef != stencilBackRef ||
520 mState.stencilBackMask != stencilBackMask)
521 {
522 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000523 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000524 mState.stencilBackMask = stencilBackMask;
525 mStencilStateDirty = true;
526 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000527}
528
529void Context::setStencilWritemask(GLuint stencilWritemask)
530{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000531 if (mState.stencilWritemask != stencilWritemask)
532 {
533 mState.stencilWritemask = stencilWritemask;
534 mStencilStateDirty = true;
535 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000536}
537
538void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
539{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000540 if (mState.stencilBackWritemask != stencilBackWritemask)
541 {
542 mState.stencilBackWritemask = stencilBackWritemask;
543 mStencilStateDirty = true;
544 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000545}
546
547void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
548{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000549 if (mState.stencilFail != stencilFail ||
550 mState.stencilPassDepthFail != stencilPassDepthFail ||
551 mState.stencilPassDepthPass != stencilPassDepthPass)
552 {
553 mState.stencilFail = stencilFail;
554 mState.stencilPassDepthFail = stencilPassDepthFail;
555 mState.stencilPassDepthPass = stencilPassDepthPass;
556 mStencilStateDirty = true;
557 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000558}
559
560void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
561{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000562 if (mState.stencilBackFail != stencilBackFail ||
563 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
564 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
565 {
566 mState.stencilBackFail = stencilBackFail;
567 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
568 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
569 mStencilStateDirty = true;
570 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000571}
572
573void Context::setPolygonOffsetFill(bool enabled)
574{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000575 if (mState.polygonOffsetFill != enabled)
576 {
577 mState.polygonOffsetFill = enabled;
578 mPolygonOffsetStateDirty = true;
579 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000580}
581
582bool Context::isPolygonOffsetFillEnabled() const
583{
584 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000585
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000586}
587
588void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
589{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000590 if (mState.polygonOffsetFactor != factor ||
591 mState.polygonOffsetUnits != units)
592 {
593 mState.polygonOffsetFactor = factor;
594 mState.polygonOffsetUnits = units;
595 mPolygonOffsetStateDirty = true;
596 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000597}
598
599void Context::setSampleAlphaToCoverage(bool enabled)
600{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000601 if (mState.sampleAlphaToCoverage != enabled)
602 {
603 mState.sampleAlphaToCoverage = enabled;
604 mSampleStateDirty = true;
605 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000606}
607
608bool Context::isSampleAlphaToCoverageEnabled() const
609{
610 return mState.sampleAlphaToCoverage;
611}
612
613void Context::setSampleCoverage(bool enabled)
614{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000615 if (mState.sampleCoverage != enabled)
616 {
617 mState.sampleCoverage = enabled;
618 mSampleStateDirty = true;
619 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000620}
621
622bool Context::isSampleCoverageEnabled() const
623{
624 return mState.sampleCoverage;
625}
626
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000627void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000628{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000629 if (mState.sampleCoverageValue != value ||
630 mState.sampleCoverageInvert != invert)
631 {
632 mState.sampleCoverageValue = value;
633 mState.sampleCoverageInvert = invert;
634 mSampleStateDirty = true;
635 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000636}
637
638void Context::setScissorTest(bool enabled)
639{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000640 if (mState.scissorTest != enabled)
641 {
642 mState.scissorTest = enabled;
643 mScissorStateDirty = true;
644 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000645}
646
647bool Context::isScissorTestEnabled() const
648{
649 return mState.scissorTest;
650}
651
652void Context::setDither(bool enabled)
653{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000654 if (mState.dither != enabled)
655 {
656 mState.dither = enabled;
657 mDitherStateDirty = true;
658 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000659}
660
661bool Context::isDitherEnabled() const
662{
663 return mState.dither;
664}
665
666void Context::setLineWidth(GLfloat width)
667{
668 mState.lineWidth = width;
669}
670
671void Context::setGenerateMipmapHint(GLenum hint)
672{
673 mState.generateMipmapHint = hint;
674}
675
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000676void Context::setFragmentShaderDerivativeHint(GLenum hint)
677{
678 mState.fragmentShaderDerivativeHint = hint;
679 // TODO: Propagate the hint to shader translator so we can write
680 // ddx, ddx_coarse, or ddx_fine depending on the hint.
681 // Ignore for now. It is valid for implementations to ignore hint.
682}
683
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000684void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
685{
686 mState.viewportX = x;
687 mState.viewportY = y;
688 mState.viewportWidth = width;
689 mState.viewportHeight = height;
690}
691
692void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
693{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000694 if (mState.scissorX != x || mState.scissorY != y ||
695 mState.scissorWidth != width || mState.scissorHeight != height)
696 {
697 mState.scissorX = x;
698 mState.scissorY = y;
699 mState.scissorWidth = width;
700 mState.scissorHeight = height;
701 mScissorStateDirty = true;
702 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000703}
704
705void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
706{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000707 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
708 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
709 {
710 mState.colorMaskRed = red;
711 mState.colorMaskGreen = green;
712 mState.colorMaskBlue = blue;
713 mState.colorMaskAlpha = alpha;
714 mMaskStateDirty = true;
715 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000716}
717
718void Context::setDepthMask(bool mask)
719{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000720 if (mState.depthMask != mask)
721 {
722 mState.depthMask = mask;
723 mMaskStateDirty = true;
724 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000725}
726
727void Context::setActiveSampler(int active)
728{
729 mState.activeSampler = active;
730}
731
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000732GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000733{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000734 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000735}
736
737GLuint Context::getDrawFramebufferHandle() const
738{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000739 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000740}
741
742GLuint Context::getRenderbufferHandle() const
743{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000744 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000745}
746
747GLuint Context::getArrayBufferHandle() const
748{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000749 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000750}
751
daniel@transgaming.com83921382011-01-08 05:46:00 +0000752void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000753{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000754 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000755}
756
daniel@transgaming.com83921382011-01-08 05:46:00 +0000757const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000758{
759 return mState.vertexAttribute[attribNum];
760}
761
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000762void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000763 GLsizei stride, const void *pointer)
764{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000765 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000766 mState.vertexAttribute[attribNum].mSize = size;
767 mState.vertexAttribute[attribNum].mType = type;
768 mState.vertexAttribute[attribNum].mNormalized = normalized;
769 mState.vertexAttribute[attribNum].mStride = stride;
770 mState.vertexAttribute[attribNum].mPointer = pointer;
771}
772
773const void *Context::getVertexAttribPointer(unsigned int attribNum) const
774{
775 return mState.vertexAttribute[attribNum].mPointer;
776}
777
daniel@transgaming.com83921382011-01-08 05:46:00 +0000778const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000779{
780 return mState.vertexAttribute;
781}
782
783void Context::setPackAlignment(GLint alignment)
784{
785 mState.packAlignment = alignment;
786}
787
788GLint Context::getPackAlignment() const
789{
790 return mState.packAlignment;
791}
792
793void Context::setUnpackAlignment(GLint alignment)
794{
795 mState.unpackAlignment = alignment;
796}
797
798GLint Context::getUnpackAlignment() const
799{
800 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801}
802
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803GLuint Context::createBuffer()
804{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000805 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806}
807
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808GLuint Context::createProgram()
809{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000810 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811}
812
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000813GLuint Context::createShader(GLenum type)
814{
815 return mResourceManager->createShader(type);
816}
817
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818GLuint Context::createTexture()
819{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000820 return mResourceManager->createTexture();
821}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000823GLuint Context::createRenderbuffer()
824{
825 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826}
827
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000828// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829GLuint Context::createFramebuffer()
830{
benvanik@google.com1a233342011-04-28 19:44:39 +0000831 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000832
833 mFramebufferMap[handle] = NULL;
834
835 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836}
837
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000838GLuint Context::createFence()
839{
benvanik@google.com1a233342011-04-28 19:44:39 +0000840 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000841
842 mFenceMap[handle] = new Fence;
843
844 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000845}
846
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847void Context::deleteBuffer(GLuint buffer)
848{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000849 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850 {
851 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000853
854 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855}
856
857void Context::deleteShader(GLuint shader)
858{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000859 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862void Context::deleteProgram(GLuint program)
863{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000864 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
867void Context::deleteTexture(GLuint texture)
868{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870 {
871 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000873
874 mResourceManager->deleteTexture(texture);
875}
876
877void Context::deleteRenderbuffer(GLuint renderbuffer)
878{
879 if (mResourceManager->getRenderbuffer(renderbuffer))
880 {
881 detachRenderbuffer(renderbuffer);
882 }
883
884 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885}
886
887void Context::deleteFramebuffer(GLuint framebuffer)
888{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000889 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
890
891 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892 {
893 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000894
benvanik@google.com1a233342011-04-28 19:44:39 +0000895 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000896 delete framebufferObject->second;
897 mFramebufferMap.erase(framebufferObject);
898 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000900
901void Context::deleteFence(GLuint fence)
902{
903 FenceMap::iterator fenceObject = mFenceMap.find(fence);
904
905 if (fenceObject != mFenceMap.end())
906 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000907 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000908 delete fenceObject->second;
909 mFenceMap.erase(fenceObject);
910 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000911}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000913Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000915 return mResourceManager->getBuffer(handle);
916}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918Shader *Context::getShader(GLuint handle)
919{
920 return mResourceManager->getShader(handle);
921}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000923Program *Context::getProgram(GLuint handle)
924{
925 return mResourceManager->getProgram(handle);
926}
927
928Texture *Context::getTexture(GLuint handle)
929{
930 return mResourceManager->getTexture(handle);
931}
932
933Renderbuffer *Context::getRenderbuffer(GLuint handle)
934{
935 return mResourceManager->getRenderbuffer(handle);
936}
937
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000938Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000939{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000940 return getFramebuffer(mState.readFramebuffer);
941}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000942
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000943Framebuffer *Context::getDrawFramebuffer()
944{
945 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946}
947
948void Context::bindArrayBuffer(unsigned int buffer)
949{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000950 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000952 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953}
954
955void Context::bindElementArrayBuffer(unsigned int buffer)
956{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000957 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000959 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960}
961
962void Context::bindTexture2D(GLuint texture)
963{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000964 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000966 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967}
968
969void Context::bindTextureCubeMap(GLuint texture)
970{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000971 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000973 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974}
975
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000976void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000978 if (!getFramebuffer(framebuffer))
979 {
980 mFramebufferMap[framebuffer] = new Framebuffer();
981 }
982
983 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000984}
985
986void Context::bindDrawFramebuffer(GLuint framebuffer)
987{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000988 if (!getFramebuffer(framebuffer))
989 {
990 mFramebufferMap[framebuffer] = new Framebuffer();
991 }
992
993 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994}
995
996void Context::bindRenderbuffer(GLuint renderbuffer)
997{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000998 mResourceManager->checkRenderbufferAllocation(renderbuffer);
999
1000 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001}
1002
1003void Context::useProgram(GLuint program)
1004{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001005 GLuint priorProgram = mState.currentProgram;
1006 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001007
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001008 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001010 Program *newProgram = mResourceManager->getProgram(program);
1011 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1012
1013 if (newProgram)
1014 {
1015 newProgram->addRef();
1016 }
1017
1018 if (oldProgram)
1019 {
1020 oldProgram->release();
1021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023}
1024
1025void Context::setFramebufferZero(Framebuffer *buffer)
1026{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001027 delete mFramebufferMap[0];
1028 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001031void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001033 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1034 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035}
1036
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001037Framebuffer *Context::getFramebuffer(unsigned int handle)
1038{
1039 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1040
1041 if (framebuffer == mFramebufferMap.end())
1042 {
1043 return NULL;
1044 }
1045 else
1046 {
1047 return framebuffer->second;
1048 }
1049}
1050
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001051Fence *Context::getFence(unsigned int handle)
1052{
1053 FenceMap::iterator fence = mFenceMap.find(handle);
1054
1055 if (fence == mFenceMap.end())
1056 {
1057 return NULL;
1058 }
1059 else
1060 {
1061 return fence->second;
1062 }
1063}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001064
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065Buffer *Context::getArrayBuffer()
1066{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001067 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068}
1069
1070Buffer *Context::getElementArrayBuffer()
1071{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001072 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073}
1074
1075Program *Context::getCurrentProgram()
1076{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001077 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078}
1079
1080Texture2D *Context::getTexture2D()
1081{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001082 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, SAMPLER_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083}
1084
1085TextureCubeMap *Context::getTextureCubeMap()
1086{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001087 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, SAMPLER_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088}
1089
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001090Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001092 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001093
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001094 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001095 {
1096 switch (type)
1097 {
1098 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001099 case SAMPLER_2D: return mTexture2DZero.get();
1100 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001101 }
1102 }
1103
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001104 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105}
1106
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001107bool Context::getBooleanv(GLenum pname, GLboolean *params)
1108{
1109 switch (pname)
1110 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001111 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1112 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1113 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001114 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001115 params[0] = mState.colorMaskRed;
1116 params[1] = mState.colorMaskGreen;
1117 params[2] = mState.colorMaskBlue;
1118 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001119 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001120 case GL_CULL_FACE: *params = mState.cullFace; break;
1121 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1122 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1123 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1124 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1125 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1126 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1127 case GL_BLEND: *params = mState.blend; break;
1128 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001129 default:
1130 return false;
1131 }
1132
1133 return true;
1134}
1135
1136bool Context::getFloatv(GLenum pname, GLfloat *params)
1137{
1138 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1139 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1140 // GetIntegerv as its native query function. As it would require conversion in any
1141 // case, this should make no difference to the calling application.
1142 switch (pname)
1143 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001144 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1145 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1146 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1147 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1148 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001149 case GL_ALIASED_LINE_WIDTH_RANGE:
1150 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1151 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1152 break;
1153 case GL_ALIASED_POINT_SIZE_RANGE:
1154 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001155 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 +00001156 break;
1157 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001158 params[0] = mState.zNear;
1159 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001160 break;
1161 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001162 params[0] = mState.colorClearValue.red;
1163 params[1] = mState.colorClearValue.green;
1164 params[2] = mState.colorClearValue.blue;
1165 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001166 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001167 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001168 params[0] = mState.blendColor.red;
1169 params[1] = mState.blendColor.green;
1170 params[2] = mState.blendColor.blue;
1171 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001172 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001173 default:
1174 return false;
1175 }
1176
1177 return true;
1178}
1179
1180bool Context::getIntegerv(GLenum pname, GLint *params)
1181{
1182 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1183 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1184 // GetIntegerv as its native query function. As it would require conversion in any
1185 // case, this should make no difference to the calling application. You may find it in
1186 // Context::getFloatv.
1187 switch (pname)
1188 {
1189 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1190 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001191 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001192 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1193 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001194 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001195 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001196 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001197 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001198 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001199 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1200 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001201 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1202 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1203 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001204 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001205 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1206 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1207 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1208 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001209 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001210 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1211 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1212 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1213 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1214 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1215 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1216 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1217 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1218 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1219 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1220 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1221 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1222 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1223 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1224 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1225 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1226 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1227 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1228 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1229 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1230 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1231 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1232 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1233 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001234 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1235 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001236 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1237 {
1238 if (supportsCompressedTextures())
1239 {
1240 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1241 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1242 *params = 2;
1243 }
1244 else
1245 {
1246 *params = 0;
1247 }
1248 }
1249 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001250 case GL_MAX_SAMPLES_ANGLE:
1251 {
1252 GLsizei maxSamples = getMaxSupportedSamples();
1253 if (maxSamples != 0)
1254 {
1255 *params = maxSamples;
1256 }
1257 else
1258 {
1259 return false;
1260 }
1261
1262 break;
1263 }
1264 case GL_SAMPLE_BUFFERS:
1265 case GL_SAMPLES:
1266 {
1267 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1268 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1269 {
1270 switch (pname)
1271 {
1272 case GL_SAMPLE_BUFFERS:
1273 if (framebuffer->getSamples() != 0)
1274 {
1275 *params = 1;
1276 }
1277 else
1278 {
1279 *params = 0;
1280 }
1281 break;
1282 case GL_SAMPLES:
1283 *params = framebuffer->getSamples();
1284 break;
1285 }
1286 }
1287 else
1288 {
1289 *params = 0;
1290 }
1291 }
1292 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001293 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1294 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1295 case GL_MAX_VIEWPORT_DIMS:
1296 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001297 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001298 params[0] = maxDimension;
1299 params[1] = maxDimension;
1300 }
1301 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001302 case GL_COMPRESSED_TEXTURE_FORMATS:
1303 {
1304 if (supportsCompressedTextures())
1305 {
1306 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1307 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1308 }
1309 }
1310 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001311 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001312 params[0] = mState.viewportX;
1313 params[1] = mState.viewportY;
1314 params[2] = mState.viewportWidth;
1315 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001316 break;
1317 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001318 params[0] = mState.scissorX;
1319 params[1] = mState.scissorY;
1320 params[2] = mState.scissorWidth;
1321 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001322 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001323 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1324 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001325 case GL_RED_BITS:
1326 case GL_GREEN_BITS:
1327 case GL_BLUE_BITS:
1328 case GL_ALPHA_BITS:
1329 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001330 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001331 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1332
1333 if (colorbuffer)
1334 {
1335 switch (pname)
1336 {
1337 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1338 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1339 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1340 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1341 }
1342 }
1343 else
1344 {
1345 *params = 0;
1346 }
1347 }
1348 break;
1349 case GL_DEPTH_BITS:
1350 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001351 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001352 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001353
1354 if (depthbuffer)
1355 {
1356 *params = depthbuffer->getDepthSize();
1357 }
1358 else
1359 {
1360 *params = 0;
1361 }
1362 }
1363 break;
1364 case GL_STENCIL_BITS:
1365 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001366 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001367 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001368
1369 if (stencilbuffer)
1370 {
1371 *params = stencilbuffer->getStencilSize();
1372 }
1373 else
1374 {
1375 *params = 0;
1376 }
1377 }
1378 break;
1379 case GL_TEXTURE_BINDING_2D:
1380 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001381 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001382 {
1383 error(GL_INVALID_OPERATION);
1384 return false;
1385 }
1386
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001387 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001388 }
1389 break;
1390 case GL_TEXTURE_BINDING_CUBE_MAP:
1391 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001392 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001393 {
1394 error(GL_INVALID_OPERATION);
1395 return false;
1396 }
1397
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001398 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001399 }
1400 break;
1401 default:
1402 return false;
1403 }
1404
1405 return true;
1406}
1407
1408bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1409{
1410 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1411 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1412 // to the fact that it is stored internally as a float, and so would require conversion
1413 // if returned from Context::getIntegerv. Since this conversion is already implemented
1414 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1415 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1416 // application.
1417 switch (pname)
1418 {
1419 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1420 case GL_SHADER_BINARY_FORMATS:
1421 {
1422 *type = GL_INT;
1423 *numParams = 0;
1424 }
1425 break;
1426 case GL_MAX_VERTEX_ATTRIBS:
1427 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1428 case GL_MAX_VARYING_VECTORS:
1429 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1430 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1431 case GL_MAX_TEXTURE_IMAGE_UNITS:
1432 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1433 case GL_MAX_RENDERBUFFER_SIZE:
1434 case GL_NUM_SHADER_BINARY_FORMATS:
1435 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1436 case GL_ARRAY_BUFFER_BINDING:
1437 case GL_FRAMEBUFFER_BINDING:
1438 case GL_RENDERBUFFER_BINDING:
1439 case GL_CURRENT_PROGRAM:
1440 case GL_PACK_ALIGNMENT:
1441 case GL_UNPACK_ALIGNMENT:
1442 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001443 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001444 case GL_RED_BITS:
1445 case GL_GREEN_BITS:
1446 case GL_BLUE_BITS:
1447 case GL_ALPHA_BITS:
1448 case GL_DEPTH_BITS:
1449 case GL_STENCIL_BITS:
1450 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1451 case GL_CULL_FACE_MODE:
1452 case GL_FRONT_FACE:
1453 case GL_ACTIVE_TEXTURE:
1454 case GL_STENCIL_FUNC:
1455 case GL_STENCIL_VALUE_MASK:
1456 case GL_STENCIL_REF:
1457 case GL_STENCIL_FAIL:
1458 case GL_STENCIL_PASS_DEPTH_FAIL:
1459 case GL_STENCIL_PASS_DEPTH_PASS:
1460 case GL_STENCIL_BACK_FUNC:
1461 case GL_STENCIL_BACK_VALUE_MASK:
1462 case GL_STENCIL_BACK_REF:
1463 case GL_STENCIL_BACK_FAIL:
1464 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1465 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1466 case GL_DEPTH_FUNC:
1467 case GL_BLEND_SRC_RGB:
1468 case GL_BLEND_SRC_ALPHA:
1469 case GL_BLEND_DST_RGB:
1470 case GL_BLEND_DST_ALPHA:
1471 case GL_BLEND_EQUATION_RGB:
1472 case GL_BLEND_EQUATION_ALPHA:
1473 case GL_STENCIL_WRITEMASK:
1474 case GL_STENCIL_BACK_WRITEMASK:
1475 case GL_STENCIL_CLEAR_VALUE:
1476 case GL_SUBPIXEL_BITS:
1477 case GL_MAX_TEXTURE_SIZE:
1478 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1479 case GL_SAMPLE_BUFFERS:
1480 case GL_SAMPLES:
1481 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1482 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1483 case GL_TEXTURE_BINDING_2D:
1484 case GL_TEXTURE_BINDING_CUBE_MAP:
1485 {
1486 *type = GL_INT;
1487 *numParams = 1;
1488 }
1489 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001490 case GL_MAX_SAMPLES_ANGLE:
1491 {
1492 if (getMaxSupportedSamples() != 0)
1493 {
1494 *type = GL_INT;
1495 *numParams = 1;
1496 }
1497 else
1498 {
1499 return false;
1500 }
1501 }
1502 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001503 case GL_MAX_VIEWPORT_DIMS:
1504 {
1505 *type = GL_INT;
1506 *numParams = 2;
1507 }
1508 break;
1509 case GL_VIEWPORT:
1510 case GL_SCISSOR_BOX:
1511 {
1512 *type = GL_INT;
1513 *numParams = 4;
1514 }
1515 break;
1516 case GL_SHADER_COMPILER:
1517 case GL_SAMPLE_COVERAGE_INVERT:
1518 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001519 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1520 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1521 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1522 case GL_SAMPLE_COVERAGE:
1523 case GL_SCISSOR_TEST:
1524 case GL_STENCIL_TEST:
1525 case GL_DEPTH_TEST:
1526 case GL_BLEND:
1527 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001528 {
1529 *type = GL_BOOL;
1530 *numParams = 1;
1531 }
1532 break;
1533 case GL_COLOR_WRITEMASK:
1534 {
1535 *type = GL_BOOL;
1536 *numParams = 4;
1537 }
1538 break;
1539 case GL_POLYGON_OFFSET_FACTOR:
1540 case GL_POLYGON_OFFSET_UNITS:
1541 case GL_SAMPLE_COVERAGE_VALUE:
1542 case GL_DEPTH_CLEAR_VALUE:
1543 case GL_LINE_WIDTH:
1544 {
1545 *type = GL_FLOAT;
1546 *numParams = 1;
1547 }
1548 break;
1549 case GL_ALIASED_LINE_WIDTH_RANGE:
1550 case GL_ALIASED_POINT_SIZE_RANGE:
1551 case GL_DEPTH_RANGE:
1552 {
1553 *type = GL_FLOAT;
1554 *numParams = 2;
1555 }
1556 break;
1557 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001558 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001559 {
1560 *type = GL_FLOAT;
1561 *numParams = 4;
1562 }
1563 break;
1564 default:
1565 return false;
1566 }
1567
1568 return true;
1569}
1570
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001571// Applies the render target surface, depth stencil surface, viewport rectangle and
1572// scissor rectangle to the Direct3D 9 device
1573bool Context::applyRenderTarget(bool ignoreViewport)
1574{
1575 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001576
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001577 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001578
1579 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1580 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001581 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001582 }
1583
1584 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001585
1586 if (!renderTarget)
1587 {
1588 return false; // Context must be lost
1589 }
1590
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001591 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001593 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1594 if (renderTargetSerial != mAppliedRenderTargetSerial)
1595 {
1596 device->SetRenderTarget(0, renderTarget);
1597 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001598 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 +00001599 }
1600
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001601 unsigned int depthbufferSerial = 0;
1602 unsigned int stencilbufferSerial = 0;
1603 if (framebufferObject->getDepthbufferType() != GL_NONE)
1604 {
1605 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001606 if (!depthStencil)
1607 {
1608 ERR("Depth stencil pointer unexpectedly null.");
1609 return false;
1610 }
1611
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001612 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1613 }
1614 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1615 {
1616 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001617 if (!depthStencil)
1618 {
1619 ERR("Depth stencil pointer unexpectedly null.");
1620 return false;
1621 }
1622
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001623 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1624 }
1625
1626 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001627 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001628 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001629 {
1630 device->SetDepthStencilSurface(depthStencil);
1631 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001632 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001633 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001634 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001635
1636 D3DVIEWPORT9 viewport;
1637 D3DSURFACE_DESC desc;
1638 renderTarget->GetDesc(&desc);
1639
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001640 float zNear = clamp01(mState.zNear);
1641 float zFar = clamp01(mState.zFar);
1642
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001643 if (ignoreViewport)
1644 {
1645 viewport.X = 0;
1646 viewport.Y = 0;
1647 viewport.Width = desc.Width;
1648 viewport.Height = desc.Height;
1649 viewport.MinZ = 0.0f;
1650 viewport.MaxZ = 1.0f;
1651 }
1652 else
1653 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001654 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1655 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1656 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1657 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1658 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 +00001659 viewport.MinZ = zNear;
1660 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001661 }
1662
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001663 if (viewport.Width <= 0 || viewport.Height <= 0)
1664 {
1665 return false; // Nothing to render
1666 }
1667
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 device->SetViewport(&viewport);
1669
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001670 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001672 if (mState.scissorTest)
1673 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001674 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1675 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1676 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1677 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1678 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001679 device->SetScissorRect(&rect);
1680 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1681 }
1682 else
1683 {
1684 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1685 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001686
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001687 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 }
1689
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001690 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692 Program *programObject = getCurrentProgram();
1693
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001694 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001695 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001696 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001697
daniel@transgaming.com31754962010-11-28 02:02:52 +00001698 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001699 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1700 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1701 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001702 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001703
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001704 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001705 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001706 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001707
daniel@transgaming.com31754962010-11-28 02:02:52 +00001708 GLint depthRange = programObject->getDxDepthRangeLocation();
1709 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1710 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711 }
1712
1713 return true;
1714}
1715
1716// 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 +00001717void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001718{
1719 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001720 Program *programObject = getCurrentProgram();
1721
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001722 Framebuffer *framebufferObject = getDrawFramebuffer();
1723
1724 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1725
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001726 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001727 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001728 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001730 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001731 GLint alwaysFront = !isTriangleMode(drawMode);
1732 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1733
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001734 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001736 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001738 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 }
1740 else
1741 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001742 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 }
1744
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001745 mCullStateDirty = false;
1746 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 if (mDepthStateDirty)
1749 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001750 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001752 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1753 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 }
1755 else
1756 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001759
1760 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761 }
1762
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001766 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1768
1769 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1770 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1771 {
1772 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1773 }
1774 else
1775 {
1776 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1777 unorm<8>(mState.blendColor.alpha),
1778 unorm<8>(mState.blendColor.alpha),
1779 unorm<8>(mState.blendColor.alpha)));
1780 }
1781
1782 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1783 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1784 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1785
1786 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1787 mState.destBlendRGB != mState.destBlendAlpha ||
1788 mState.blendEquationRGB != mState.blendEquationAlpha)
1789 {
1790 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1791
1792 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1793 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1794 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001795 }
1796 else
1797 {
1798 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1799 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001800 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001801 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001802 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001803 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001804 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001805
1806 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001807 }
1808
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001809 if (mStencilStateDirty || mFrontFaceDirty)
1810 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001811 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001812 {
1813 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1814 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1815
1816 // FIXME: Unsupported by D3D9
1817 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1818 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1819 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1820 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1821 mState.stencilRef != mState.stencilBackRef ||
1822 mState.stencilMask != mState.stencilBackMask)
1823 {
1824 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1825 return error(GL_INVALID_OPERATION);
1826 }
1827
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001828 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001829 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001830 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1831
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001832 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1833 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001834 es2dx::ConvertComparison(mState.stencilFunc));
1835
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001836 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1837 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001838
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001839 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001840 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001841 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001842 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001843 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001844 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1845
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001846 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1847 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848 es2dx::ConvertComparison(mState.stencilBackFunc));
1849
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001850 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1851 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001852
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001853 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001854 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001855 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001856 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001857 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001858 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1859 }
1860 else
1861 {
1862 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1863 }
1864
1865 mStencilStateDirty = false;
1866 }
1867
1868 if (mMaskStateDirty)
1869 {
1870 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1871 mState.colorMaskBlue, mState.colorMaskAlpha));
1872 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1873
1874 mMaskStateDirty = false;
1875 }
1876
1877 if (mPolygonOffsetStateDirty)
1878 {
1879 if (mState.polygonOffsetFill)
1880 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001881 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001882 if (depthbuffer)
1883 {
1884 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1885 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1886 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1887 }
1888 }
1889 else
1890 {
1891 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1892 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1893 }
1894
1895 mPolygonOffsetStateDirty = false;
1896 }
1897
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001898 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001899 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001900 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001901 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001902 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001903 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001904 FIXME("Sample alpha to coverage is unimplemented.");
1905 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001906
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001907 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1908 if (mState.sampleCoverage)
1909 {
1910 unsigned int mask = 0;
1911 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001912 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001913 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001914
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001915 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001916 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001917 mask <<= 1;
1918
1919 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1920 {
1921 threshold += 1.0f;
1922 mask |= 1;
1923 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001924 }
1925 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001926
1927 if (mState.sampleCoverageInvert)
1928 {
1929 mask = ~mask;
1930 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001931
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001932 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1933 }
1934 else
1935 {
1936 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1937 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001938 }
1939 else
1940 {
1941 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001942 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001943
1944 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945 }
1946
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001947 if (mDitherStateDirty)
1948 {
1949 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1950
1951 mDitherStateDirty = false;
1952 }
1953
1954 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955}
1956
daniel@transgaming.com83921382011-01-08 05:46:00 +00001957GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001958{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001959 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001960
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001961 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001962 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001963 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001964 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001965 }
1966
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00001967 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001968}
1969
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001970// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001971GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001972{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001973 IDirect3DDevice9 *device = getDevice();
1974 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001975
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001976 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001977 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001978 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001979 }
1980
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001981 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982}
1983
1984// Applies the shaders and shader constants to the Direct3D 9 device
1985void Context::applyShaders()
1986{
1987 IDirect3DDevice9 *device = getDevice();
1988 Program *programObject = getCurrentProgram();
1989 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1990 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1991
1992 device->SetVertexShader(vertexShader);
1993 device->SetPixelShader(pixelShader);
1994
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001995 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001996 {
1997 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001998 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001999 }
2000
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002001 programObject->applyUniforms();
2002}
2003
2004// Applies the textures and sampler states to the Direct3D 9 device
2005void Context::applyTextures()
2006{
2007 IDirect3DDevice9 *device = getDevice();
2008 Program *programObject = getCurrentProgram();
2009
2010 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2011 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002012 int textureUnit = programObject->getSamplerMapping(sampler);
2013 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002014 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002015 SamplerType textureType = programObject->getSamplerType(sampler);
2016
2017 Texture *texture = getSamplerTexture(textureUnit, textureType);
2018
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002019 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002020 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002021 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2022
2023 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002024 {
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002025 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter())
2026 {
2027 GLenum wrapS = texture->getWrapS();
2028 GLenum wrapT = texture->getWrapT();
2029 GLenum minFilter = texture->getMinFilter();
2030 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002032 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2033 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002035 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2036 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2037 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2038 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2039 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
2040 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002041
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002042 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyImage())
2043 {
2044 device->SetTexture(sampler, d3dTexture);
2045 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002046 }
2047 else
2048 {
2049 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2050 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002051
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002052 mAppliedTextureSerial[sampler] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002053 texture->resetDirty();
2054 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002056 else
2057 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002058 if (mAppliedTextureSerial[sampler] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002059 {
2060 device->SetTexture(sampler, NULL);
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002061 mAppliedTextureSerial[sampler] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002062 }
2063 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064 }
2065}
2066
2067void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2068{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002069 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002070
2071 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2072 {
2073 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2074 }
2075
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002076 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2077 {
2078 return error(GL_INVALID_OPERATION);
2079 }
2080
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002082
2083 if (!renderTarget)
2084 {
2085 return; // Context must be lost, return silently
2086 }
2087
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 IDirect3DDevice9 *device = getDevice();
2089
2090 D3DSURFACE_DESC desc;
2091 renderTarget->GetDesc(&desc);
2092
2093 IDirect3DSurface9 *systemSurface;
2094 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2095
2096 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2097 {
2098 return error(GL_OUT_OF_MEMORY);
2099 }
2100
2101 ASSERT(SUCCEEDED(result));
2102
2103 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2104 {
2105 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2106 }
2107
2108 result = device->GetRenderTargetData(renderTarget, systemSurface);
2109
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 if (FAILED(result))
2111 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 systemSurface->Release();
2113
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002114 switch (result)
2115 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002116 case D3DERR_DRIVERINTERNALERROR:
2117 case D3DERR_DEVICELOST:
2118 return error(GL_OUT_OF_MEMORY);
2119 default:
2120 UNREACHABLE();
2121 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002122 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 }
2124
2125 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002126 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2127 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2128 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2129 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2130 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131
2132 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2133
2134 if (FAILED(result))
2135 {
2136 UNREACHABLE();
2137 systemSurface->Release();
2138
2139 return; // No sensible error to generate
2140 }
2141
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002142 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002143 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002144 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002145 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002146 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002147
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 for (int j = 0; j < rect.bottom - rect.top; j++)
2149 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002150 if (desc.Format == D3DFMT_A8R8G8B8 &&
2151 format == GL_BGRA_EXT &&
2152 type == GL_UNSIGNED_BYTE)
2153 {
2154 // Fast path for EXT_read_format_bgra, given
2155 // an RGBA source buffer. Note that buffers with no
2156 // alpha go through the slow path below.
2157 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002158 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002159 (rect.right - rect.left) * 4);
2160 continue;
2161 }
2162
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 for (int i = 0; i < rect.right - rect.left; i++)
2164 {
2165 float r;
2166 float g;
2167 float b;
2168 float a;
2169
2170 switch (desc.Format)
2171 {
2172 case D3DFMT_R5G6B5:
2173 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002174 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175
2176 a = 1.0f;
2177 b = (rgb & 0x001F) * (1.0f / 0x001F);
2178 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2179 r = (rgb & 0xF800) * (1.0f / 0xF800);
2180 }
2181 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002182 case D3DFMT_A1R5G5B5:
2183 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002184 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185
2186 a = (argb & 0x8000) ? 1.0f : 0.0f;
2187 b = (argb & 0x001F) * (1.0f / 0x001F);
2188 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2189 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2190 }
2191 break;
2192 case D3DFMT_A8R8G8B8:
2193 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002194 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002195
2196 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2197 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2198 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2199 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2200 }
2201 break;
2202 case D3DFMT_X8R8G8B8:
2203 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002204 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205
2206 a = 1.0f;
2207 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2208 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2209 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2210 }
2211 break;
2212 case D3DFMT_A2R10G10B10:
2213 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002214 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002215
2216 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2217 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2218 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2219 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2220 }
2221 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002222 case D3DFMT_A32B32G32R32F:
2223 {
2224 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002225 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2226 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2227 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2228 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002229 }
2230 break;
2231 case D3DFMT_A16B16G16R16F:
2232 {
2233 // float formats in D3D are stored rgba, rather than the other way round
2234 float abgr[4];
2235
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002236 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002237
2238 a = abgr[3];
2239 b = abgr[2];
2240 g = abgr[1];
2241 r = abgr[0];
2242 }
2243 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 default:
2245 UNIMPLEMENTED(); // FIXME
2246 UNREACHABLE();
2247 }
2248
2249 switch (format)
2250 {
2251 case GL_RGBA:
2252 switch (type)
2253 {
2254 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002255 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2256 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2257 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2258 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 break;
2260 default: UNREACHABLE();
2261 }
2262 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002263 case GL_BGRA_EXT:
2264 switch (type)
2265 {
2266 case GL_UNSIGNED_BYTE:
2267 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2268 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2269 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2270 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2271 break;
2272 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2273 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2274 // this type is packed as follows:
2275 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2276 // --------------------------------------------------------------------------------
2277 // | 4th | 3rd | 2nd | 1st component |
2278 // --------------------------------------------------------------------------------
2279 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2280 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2281 ((unsigned short)(15 * a + 0.5f) << 12)|
2282 ((unsigned short)(15 * r + 0.5f) << 8) |
2283 ((unsigned short)(15 * g + 0.5f) << 4) |
2284 ((unsigned short)(15 * b + 0.5f) << 0);
2285 break;
2286 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2287 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2288 // this type is packed as follows:
2289 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2290 // --------------------------------------------------------------------------------
2291 // | 4th | 3rd | 2nd | 1st component |
2292 // --------------------------------------------------------------------------------
2293 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2294 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2295 ((unsigned short)( a + 0.5f) << 15) |
2296 ((unsigned short)(31 * r + 0.5f) << 10) |
2297 ((unsigned short)(31 * g + 0.5f) << 5) |
2298 ((unsigned short)(31 * b + 0.5f) << 0);
2299 break;
2300 default: UNREACHABLE();
2301 }
2302 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002303 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 switch (type)
2305 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002306 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002307 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2308 ((unsigned short)(31 * b + 0.5f) << 0) |
2309 ((unsigned short)(63 * g + 0.5f) << 5) |
2310 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 break;
2312 default: UNREACHABLE();
2313 }
2314 break;
2315 default: UNREACHABLE();
2316 }
2317 }
2318 }
2319
2320 systemSurface->UnlockRect();
2321
2322 systemSurface->Release();
2323}
2324
2325void Context::clear(GLbitfield mask)
2326{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002327 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002328
2329 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2330 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002331 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002332 }
2333
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002334 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335 IDirect3DDevice9 *device = getDevice();
2336 DWORD flags = 0;
2337
2338 if (mask & GL_COLOR_BUFFER_BIT)
2339 {
2340 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002341
2342 if (framebufferObject->getColorbufferType() != GL_NONE)
2343 {
2344 flags |= D3DCLEAR_TARGET;
2345 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002346 }
2347
2348 if (mask & GL_DEPTH_BUFFER_BIT)
2349 {
2350 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002351 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 {
2353 flags |= D3DCLEAR_ZBUFFER;
2354 }
2355 }
2356
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357 GLuint stencilUnmasked = 0x0;
2358
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002359 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002362 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002364 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002365 if (!depthStencil)
2366 {
2367 ERR("Depth stencil pointer unexpectedly null.");
2368 return;
2369 }
2370
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002371 D3DSURFACE_DESC desc;
2372 depthStencil->GetDesc(&desc);
2373
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002374 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002375 stencilUnmasked = (0x1 << stencilSize) - 1;
2376
2377 if (stencilUnmasked != 0x0)
2378 {
2379 flags |= D3DCLEAR_STENCIL;
2380 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 }
2382 }
2383
2384 if (mask != 0)
2385 {
2386 return error(GL_INVALID_VALUE);
2387 }
2388
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002389 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2390 {
2391 return;
2392 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002394 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002395 unorm<8>(mState.colorClearValue.red),
2396 unorm<8>(mState.colorClearValue.green),
2397 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002398 float depth = clamp01(mState.depthClearValue);
2399 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400
2401 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2402
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002403 if (!renderTarget)
2404 {
2405 return; // Context must be lost, return silently
2406 }
2407
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 D3DSURFACE_DESC desc;
2409 renderTarget->GetDesc(&desc);
2410
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002411 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002412
2413 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002414 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002416 !(mState.colorMaskRed && mState.colorMaskGreen &&
2417 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418
2419 if (needMaskedColorClear || needMaskedStencilClear)
2420 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002421 // State which is altered in all paths from this point to the clear call is saved.
2422 // State which is altered in only some paths will be flagged dirty in the case that
2423 // that path is taken.
2424 HRESULT hr;
2425 if (mMaskedClearSavedState == NULL)
2426 {
2427 hr = device->BeginStateBlock();
2428 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2429
2430 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2431 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2432 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2433 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2434 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2435 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2436 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2437 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2438 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2439 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2440 device->SetPixelShader(NULL);
2441 device->SetVertexShader(NULL);
2442 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002443 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2444 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2445 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2446 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2447 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2448 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2449 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002450
2451 hr = device->EndStateBlock(&mMaskedClearSavedState);
2452 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2453 }
2454
2455 ASSERT(mMaskedClearSavedState != NULL);
2456
2457 if (mMaskedClearSavedState != NULL)
2458 {
2459 hr = mMaskedClearSavedState->Capture();
2460 ASSERT(SUCCEEDED(hr));
2461 }
2462
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2464 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2465 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2466 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2467 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2468 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2469 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2470 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2471
2472 if (flags & D3DCLEAR_TARGET)
2473 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002474 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475 }
2476 else
2477 {
2478 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2479 }
2480
2481 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2482 {
2483 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2484 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2485 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2486 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002487 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002488 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2490 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002491 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492 }
2493 else
2494 {
2495 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2496 }
2497
2498 device->SetPixelShader(NULL);
2499 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002500 device->SetFVF(D3DFVF_XYZRHW);
2501 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2502 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2503 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2504 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2505 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2506 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2507 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002509 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2510 quad[0][0] = -0.5f;
2511 quad[0][1] = desc.Height - 0.5f;
2512 quad[0][2] = 0.0f;
2513 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002515 quad[1][0] = desc.Width - 0.5f;
2516 quad[1][1] = desc.Height - 0.5f;
2517 quad[1][2] = 0.0f;
2518 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002519
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002520 quad[2][0] = -0.5f;
2521 quad[2][1] = -0.5f;
2522 quad[2][2] = 0.0f;
2523 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002525 quad[3][0] = desc.Width - 0.5f;
2526 quad[3][1] = -0.5f;
2527 quad[3][2] = 0.0f;
2528 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002529
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002530 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002531 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532
2533 if (flags & D3DCLEAR_ZBUFFER)
2534 {
2535 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2536 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2537 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2538 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002539
2540 if (mMaskedClearSavedState != NULL)
2541 {
2542 mMaskedClearSavedState->Apply();
2543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002545 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546 {
2547 device->Clear(0, NULL, flags, color, depth, stencil);
2548 }
2549}
2550
2551void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2552{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002553 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002554 {
2555 return error(GL_INVALID_OPERATION);
2556 }
2557
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002558 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559 IDirect3DDevice9 *device = getDevice();
2560 D3DPRIMITIVETYPE primitiveType;
2561 int primitiveCount;
2562
2563 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2564 return error(GL_INVALID_ENUM);
2565
2566 if (primitiveCount <= 0)
2567 {
2568 return;
2569 }
2570
2571 if (!applyRenderTarget(false))
2572 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002573 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002574 }
2575
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002576 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002577
daniel@transgaming.com83921382011-01-08 05:46:00 +00002578 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002579 if (err != GL_NO_ERROR)
2580 {
2581 return error(err);
2582 }
2583
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584 applyShaders();
2585 applyTextures();
2586
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002587 if (!getCurrentProgram()->validateSamplers())
2588 {
2589 return error(GL_INVALID_OPERATION);
2590 }
2591
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002592 if (!cullSkipsDraw(mode))
2593 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002594 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002595
2596 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002597
2598 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2599 {
2600 drawClosingLine(first, first + count - 1);
2601 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002602 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603}
2604
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002605void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002606{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002607 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608 {
2609 return error(GL_INVALID_OPERATION);
2610 }
2611
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002612 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613 {
2614 return error(GL_INVALID_OPERATION);
2615 }
2616
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002617 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002618 IDirect3DDevice9 *device = getDevice();
2619 D3DPRIMITIVETYPE primitiveType;
2620 int primitiveCount;
2621
2622 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2623 return error(GL_INVALID_ENUM);
2624
2625 if (primitiveCount <= 0)
2626 {
2627 return;
2628 }
2629
2630 if (!applyRenderTarget(false))
2631 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002632 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633 }
2634
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002635 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002636
2637 TranslatedIndexData indexInfo;
2638 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2639 if (err != GL_NO_ERROR)
2640 {
2641 return error(err);
2642 }
2643
daniel@transgaming.com83921382011-01-08 05:46:00 +00002644 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2645 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002646 if (err != GL_NO_ERROR)
2647 {
2648 return error(err);
2649 }
2650
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651 applyShaders();
2652 applyTextures();
2653
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002654 if (!getCurrentProgram()->validateSamplers())
2655 {
2656 return error(GL_INVALID_OPERATION);
2657 }
2658
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002659 if (!cullSkipsDraw(mode))
2660 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002661 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002662
daniel@transgaming.com83921382011-01-08 05:46:00 +00002663 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002664
2665 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2666 {
2667 drawClosingLine(count, type, indices);
2668 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002669 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002670}
2671
2672void Context::finish()
2673{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002674 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002675 IDirect3DDevice9 *device = getDevice();
2676 IDirect3DQuery9 *occlusionQuery = NULL;
2677
2678 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2679
2680 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2681 {
2682 return error(GL_OUT_OF_MEMORY);
2683 }
2684
2685 ASSERT(SUCCEEDED(result));
2686
2687 if (occlusionQuery)
2688 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002689 IDirect3DStateBlock9 *savedState = NULL;
2690 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2691
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002692 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2693 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694
2695 // Render something outside the render target
2696 device->SetPixelShader(NULL);
2697 device->SetVertexShader(NULL);
2698 device->SetFVF(D3DFVF_XYZRHW);
2699 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002700 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002701 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002703 result = occlusionQuery->Issue(D3DISSUE_END);
2704 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705
2706 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2707 {
2708 // Keep polling, but allow other threads to do something useful first
2709 Sleep(0);
2710 }
2711
2712 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002713
2714 if (savedState)
2715 {
2716 savedState->Apply();
2717 savedState->Release();
2718 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719 }
2720}
2721
2722void Context::flush()
2723{
2724 IDirect3DDevice9 *device = getDevice();
2725 IDirect3DQuery9 *eventQuery = NULL;
2726
2727 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2728
2729 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2730 {
2731 return error(GL_OUT_OF_MEMORY);
2732 }
2733
2734 ASSERT(SUCCEEDED(result));
2735
2736 if (eventQuery)
2737 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002738 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2739 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002741 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002743
2744 if (result == D3DERR_DEVICELOST)
2745 {
2746 error(GL_OUT_OF_MEMORY);
2747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 }
2749}
2750
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002751void Context::drawClosingLine(unsigned int first, unsigned int last)
2752{
2753 IDirect3DDevice9 *device = getDevice();
2754 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002755 bool succeeded = false;
2756 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002757
2758 if (supports32bitIndices())
2759 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002760 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002761
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002762 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002763 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002764 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2765 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002766
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002767 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2768
2769 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2770 if (data)
2771 {
2772 data[0] = last;
2773 data[1] = first;
2774 mClosingIB->unmap();
2775 offset /= 4;
2776 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002777 }
2778 }
2779 else
2780 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002781 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002782
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002783 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002784 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002785 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2786 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002787
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002788 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2789
2790 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2791 if (data)
2792 {
2793 data[0] = last;
2794 data[1] = first;
2795 mClosingIB->unmap();
2796 offset /= 2;
2797 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002798 }
2799 }
2800
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002801 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002802 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002803 device->SetIndices(mClosingIB->getBuffer());
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002804
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002805 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002806 }
2807 else
2808 {
2809 ERR("Could not create an index buffer for closing a line loop.");
2810 error(GL_OUT_OF_MEMORY);
2811 }
2812}
2813
2814void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2815{
2816 unsigned int first = 0;
2817 unsigned int last = 0;
2818
2819 if (mState.elementArrayBuffer.get())
2820 {
2821 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2822 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2823 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2824 }
2825
2826 switch (type)
2827 {
2828 case GL_UNSIGNED_BYTE:
2829 first = static_cast<const GLubyte*>(indices)[0];
2830 last = static_cast<const GLubyte*>(indices)[count - 1];
2831 break;
2832 case GL_UNSIGNED_SHORT:
2833 first = static_cast<const GLushort*>(indices)[0];
2834 last = static_cast<const GLushort*>(indices)[count - 1];
2835 break;
2836 case GL_UNSIGNED_INT:
2837 first = static_cast<const GLuint*>(indices)[0];
2838 last = static_cast<const GLuint*>(indices)[count - 1];
2839 break;
2840 default: UNREACHABLE();
2841 }
2842
2843 drawClosingLine(first, last);
2844}
2845
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002846void Context::recordInvalidEnum()
2847{
2848 mInvalidEnum = true;
2849}
2850
2851void Context::recordInvalidValue()
2852{
2853 mInvalidValue = true;
2854}
2855
2856void Context::recordInvalidOperation()
2857{
2858 mInvalidOperation = true;
2859}
2860
2861void Context::recordOutOfMemory()
2862{
2863 mOutOfMemory = true;
2864}
2865
2866void Context::recordInvalidFramebufferOperation()
2867{
2868 mInvalidFramebufferOperation = true;
2869}
2870
2871// Get one of the recorded errors and clear its flag, if any.
2872// [OpenGL ES 2.0.24] section 2.5 page 13.
2873GLenum Context::getError()
2874{
2875 if (mInvalidEnum)
2876 {
2877 mInvalidEnum = false;
2878
2879 return GL_INVALID_ENUM;
2880 }
2881
2882 if (mInvalidValue)
2883 {
2884 mInvalidValue = false;
2885
2886 return GL_INVALID_VALUE;
2887 }
2888
2889 if (mInvalidOperation)
2890 {
2891 mInvalidOperation = false;
2892
2893 return GL_INVALID_OPERATION;
2894 }
2895
2896 if (mOutOfMemory)
2897 {
2898 mOutOfMemory = false;
2899
2900 return GL_OUT_OF_MEMORY;
2901 }
2902
2903 if (mInvalidFramebufferOperation)
2904 {
2905 mInvalidFramebufferOperation = false;
2906
2907 return GL_INVALID_FRAMEBUFFER_OPERATION;
2908 }
2909
2910 return GL_NO_ERROR;
2911}
2912
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002913bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002914{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002915 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002916}
2917
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002918int Context::getMaximumVaryingVectors() const
2919{
2920 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2921}
2922
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002923int Context::getMaximumVertexTextureImageUnits() const
2924{
2925 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
2926}
2927
2928int Context::getMaximumCombinedTextureImageUnits() const
2929{
2930 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
2931}
2932
daniel@transgaming.com458da142010-11-28 02:03:02 +00002933int Context::getMaximumFragmentUniformVectors() const
2934{
2935 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2936}
2937
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002938int Context::getMaxSupportedSamples() const
2939{
2940 return mMaxSupportedSamples;
2941}
2942
2943int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2944{
2945 if (requested == 0)
2946 {
2947 return requested;
2948 }
2949
2950 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2951 if (itr == mMultiSampleSupport.end())
2952 {
2953 return -1;
2954 }
2955
2956 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2957 {
2958 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2959 {
2960 return i;
2961 }
2962 }
2963
2964 return -1;
2965}
2966
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002967bool Context::supportsEventQueries() const
2968{
2969 return mSupportsEventQueries;
2970}
2971
daniel@transgaming.com01868132010-08-24 19:21:17 +00002972bool Context::supportsCompressedTextures() const
2973{
2974 return mSupportsCompressedTextures;
2975}
2976
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002977bool Context::supportsFloatTextures() const
2978{
2979 return mSupportsFloatTextures;
2980}
2981
2982bool Context::supportsFloatLinearFilter() const
2983{
2984 return mSupportsFloatLinearFilter;
2985}
2986
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002987bool Context::supportsFloatRenderableTextures() const
2988{
2989 return mSupportsFloatRenderableTextures;
2990}
2991
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002992bool Context::supportsHalfFloatTextures() const
2993{
2994 return mSupportsHalfFloatTextures;
2995}
2996
2997bool Context::supportsHalfFloatLinearFilter() const
2998{
2999 return mSupportsHalfFloatLinearFilter;
3000}
3001
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003002bool Context::supportsHalfFloatRenderableTextures() const
3003{
3004 return mSupportsHalfFloatRenderableTextures;
3005}
3006
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003007int Context::getMaximumRenderbufferDimension() const
3008{
3009 return mMaxRenderbufferDimension;
3010}
3011
3012int Context::getMaximumTextureDimension() const
3013{
3014 return mMaxTextureDimension;
3015}
3016
3017int Context::getMaximumCubeTextureDimension() const
3018{
3019 return mMaxCubeTextureDimension;
3020}
3021
3022int Context::getMaximumTextureLevel() const
3023{
3024 return mMaxTextureLevel;
3025}
3026
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003027bool Context::supportsLuminanceTextures() const
3028{
3029 return mSupportsLuminanceTextures;
3030}
3031
3032bool Context::supportsLuminanceAlphaTextures() const
3033{
3034 return mSupportsLuminanceAlphaTextures;
3035}
3036
daniel@transgaming.com83921382011-01-08 05:46:00 +00003037bool Context::supports32bitIndices() const
3038{
3039 return mSupports32bitIndices;
3040}
3041
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003042void Context::detachBuffer(GLuint buffer)
3043{
3044 // [OpenGL ES 2.0.24] section 2.9 page 22:
3045 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3046 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3047
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003048 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003049 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003050 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003051 }
3052
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003053 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003054 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003055 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003056 }
3057
3058 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3059 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003060 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003061 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003062 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003063 }
3064 }
3065}
3066
3067void Context::detachTexture(GLuint texture)
3068{
3069 // [OpenGL ES 2.0.24] section 3.8 page 84:
3070 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3071 // rebound to texture object zero
3072
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003073 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003074 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003075 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003076 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003077 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003078 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003079 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003080 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003081 }
3082 }
3083
3084 // [OpenGL ES 2.0.24] section 4.4 page 112:
3085 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3086 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3087 // image was attached in the currently bound framebuffer.
3088
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003089 Framebuffer *readFramebuffer = getReadFramebuffer();
3090 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003091
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003092 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003093 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003094 readFramebuffer->detachTexture(texture);
3095 }
3096
3097 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3098 {
3099 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003100 }
3101}
3102
3103void Context::detachFramebuffer(GLuint framebuffer)
3104{
3105 // [OpenGL ES 2.0.24] section 4.4 page 107:
3106 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3107 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3108
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003109 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003110 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003111 bindReadFramebuffer(0);
3112 }
3113
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003114 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003115 {
3116 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003117 }
3118}
3119
3120void Context::detachRenderbuffer(GLuint renderbuffer)
3121{
3122 // [OpenGL ES 2.0.24] section 4.4 page 109:
3123 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3124 // had been executed with the target RENDERBUFFER and name of zero.
3125
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003126 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003127 {
3128 bindRenderbuffer(0);
3129 }
3130
3131 // [OpenGL ES 2.0.24] section 4.4 page 111:
3132 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3133 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3134 // point to which this image was attached in the currently bound framebuffer.
3135
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003136 Framebuffer *readFramebuffer = getReadFramebuffer();
3137 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003138
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003139 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003140 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003141 readFramebuffer->detachRenderbuffer(renderbuffer);
3142 }
3143
3144 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3145 {
3146 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003147 }
3148}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003149
3150Texture *Context::getIncompleteTexture(SamplerType type)
3151{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003152 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003153
3154 if (t == NULL)
3155 {
3156 static const GLubyte color[] = { 0, 0, 0, 255 };
3157
3158 switch (type)
3159 {
3160 default:
3161 UNREACHABLE();
3162 // default falls through to SAMPLER_2D
3163
3164 case SAMPLER_2D:
3165 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003166 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003167 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003168 t = incomplete2d;
3169 }
3170 break;
3171
3172 case SAMPLER_CUBE:
3173 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003174 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003175
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003176 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3177 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3178 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3179 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3180 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3181 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003182
3183 t = incompleteCube;
3184 }
3185 break;
3186 }
3187
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003188 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003189 }
3190
3191 return t;
3192}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003193
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003194bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003195{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003196 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003197}
3198
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003199bool Context::isTriangleMode(GLenum drawMode)
3200{
3201 switch (drawMode)
3202 {
3203 case GL_TRIANGLES:
3204 case GL_TRIANGLE_FAN:
3205 case GL_TRIANGLE_STRIP:
3206 return true;
3207 case GL_POINTS:
3208 case GL_LINES:
3209 case GL_LINE_LOOP:
3210 case GL_LINE_STRIP:
3211 return false;
3212 default: UNREACHABLE();
3213 }
3214
3215 return false;
3216}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003217
3218void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3219{
3220 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3221
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003222 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3223 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3224 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3225 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003226
daniel@transgaming.com83921382011-01-08 05:46:00 +00003227 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003228}
3229
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003230void Context::initExtensionString()
3231{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003232 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003233 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3234 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003235 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003236 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003237 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003238
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003239 if (supportsEventQueries())
3240 {
3241 mExtensionString += "GL_NV_fence ";
3242 }
3243
daniel@transgaming.com01868132010-08-24 19:21:17 +00003244 if (supportsCompressedTextures())
3245 {
3246 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3247 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003248
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003249 if (supportsFloatTextures())
3250 {
3251 mExtensionString += "GL_OES_texture_float ";
3252 }
3253
3254 if (supportsHalfFloatTextures())
3255 {
3256 mExtensionString += "GL_OES_texture_half_float ";
3257 }
3258
3259 if (supportsFloatLinearFilter())
3260 {
3261 mExtensionString += "GL_OES_texture_float_linear ";
3262 }
3263
3264 if (supportsHalfFloatLinearFilter())
3265 {
3266 mExtensionString += "GL_OES_texture_half_float_linear ";
3267 }
3268
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003269 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003270 {
3271 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3272 }
3273
daniel@transgaming.com83921382011-01-08 05:46:00 +00003274 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003275 {
3276 mExtensionString += "GL_OES_element_index_uint ";
3277 }
3278
3279 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3280 if (end != std::string::npos)
3281 {
3282 mExtensionString.resize(end+1);
3283 }
3284}
3285
3286const char *Context::getExtensionString() const
3287{
3288 return mExtensionString.c_str();
3289}
3290
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003291void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3292 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3293 GLbitfield mask)
3294{
3295 IDirect3DDevice9 *device = getDevice();
3296
3297 Framebuffer *readFramebuffer = getReadFramebuffer();
3298 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3299
3300 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3301 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3302 {
3303 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3304 }
3305
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003306 if (drawFramebuffer->getSamples() != 0)
3307 {
3308 return error(GL_INVALID_OPERATION);
3309 }
3310
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003311 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3312 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3313 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3314 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3315
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003316 RECT sourceRect;
3317 RECT destRect;
3318
3319 if (srcX0 < srcX1)
3320 {
3321 sourceRect.left = srcX0;
3322 sourceRect.right = srcX1;
3323 destRect.left = dstX0;
3324 destRect.right = dstX1;
3325 }
3326 else
3327 {
3328 sourceRect.left = srcX1;
3329 destRect.left = dstX1;
3330 sourceRect.right = srcX0;
3331 destRect.right = dstX0;
3332 }
3333
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003334 if (srcY0 < srcY1)
3335 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003336 sourceRect.top = readBufferHeight - srcY1;
3337 destRect.top = drawBufferHeight - dstY1;
3338 sourceRect.bottom = readBufferHeight - srcY0;
3339 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003340 }
3341 else
3342 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003343 sourceRect.top = readBufferHeight - srcY0;
3344 destRect.top = drawBufferHeight - dstY0;
3345 sourceRect.bottom = readBufferHeight - srcY1;
3346 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003347 }
3348
3349 RECT sourceScissoredRect = sourceRect;
3350 RECT destScissoredRect = destRect;
3351
3352 if (mState.scissorTest)
3353 {
3354 // Only write to parts of the destination framebuffer which pass the scissor test
3355 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3356 // rect will be checked against scissorY, rather than the bottom.
3357 if (destRect.left < mState.scissorX)
3358 {
3359 int xDiff = mState.scissorX - destRect.left;
3360 destScissoredRect.left = mState.scissorX;
3361 sourceScissoredRect.left += xDiff;
3362 }
3363
3364 if (destRect.right > mState.scissorX + mState.scissorWidth)
3365 {
3366 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3367 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3368 sourceScissoredRect.right -= xDiff;
3369 }
3370
3371 if (destRect.top < mState.scissorY)
3372 {
3373 int yDiff = mState.scissorY - destRect.top;
3374 destScissoredRect.top = mState.scissorY;
3375 sourceScissoredRect.top += yDiff;
3376 }
3377
3378 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3379 {
3380 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3381 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3382 sourceScissoredRect.bottom -= yDiff;
3383 }
3384 }
3385
3386 bool blitRenderTarget = false;
3387 bool blitDepthStencil = false;
3388
3389 RECT sourceTrimmedRect = sourceScissoredRect;
3390 RECT destTrimmedRect = destScissoredRect;
3391
3392 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3393 // the actual draw and read surfaces.
3394 if (sourceTrimmedRect.left < 0)
3395 {
3396 int xDiff = 0 - sourceTrimmedRect.left;
3397 sourceTrimmedRect.left = 0;
3398 destTrimmedRect.left += xDiff;
3399 }
3400
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003401 if (sourceTrimmedRect.right > readBufferWidth)
3402 {
3403 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3404 sourceTrimmedRect.right = readBufferWidth;
3405 destTrimmedRect.right -= xDiff;
3406 }
3407
3408 if (sourceTrimmedRect.top < 0)
3409 {
3410 int yDiff = 0 - sourceTrimmedRect.top;
3411 sourceTrimmedRect.top = 0;
3412 destTrimmedRect.top += yDiff;
3413 }
3414
3415 if (sourceTrimmedRect.bottom > readBufferHeight)
3416 {
3417 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3418 sourceTrimmedRect.bottom = readBufferHeight;
3419 destTrimmedRect.bottom -= yDiff;
3420 }
3421
3422 if (destTrimmedRect.left < 0)
3423 {
3424 int xDiff = 0 - destTrimmedRect.left;
3425 destTrimmedRect.left = 0;
3426 sourceTrimmedRect.left += xDiff;
3427 }
3428
3429 if (destTrimmedRect.right > drawBufferWidth)
3430 {
3431 int xDiff = destTrimmedRect.right - drawBufferWidth;
3432 destTrimmedRect.right = drawBufferWidth;
3433 sourceTrimmedRect.right -= xDiff;
3434 }
3435
3436 if (destTrimmedRect.top < 0)
3437 {
3438 int yDiff = 0 - destTrimmedRect.top;
3439 destTrimmedRect.top = 0;
3440 sourceTrimmedRect.top += yDiff;
3441 }
3442
3443 if (destTrimmedRect.bottom > drawBufferHeight)
3444 {
3445 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3446 destTrimmedRect.bottom = drawBufferHeight;
3447 sourceTrimmedRect.bottom -= yDiff;
3448 }
3449
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003450 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003451 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3452 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3453 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3454 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003455 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3456 {
3457 partialBufferCopy = true;
3458 }
3459
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003460 if (mask & GL_COLOR_BUFFER_BIT)
3461 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003462 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3463 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3464 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3465 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3466 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003467 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3468 {
3469 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3470 return error(GL_INVALID_OPERATION);
3471 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003472
3473 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3474 {
3475 return error(GL_INVALID_OPERATION);
3476 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003477
3478 blitRenderTarget = true;
3479
3480 }
3481
3482 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3483 {
3484 DepthStencilbuffer *readDSBuffer = NULL;
3485 DepthStencilbuffer *drawDSBuffer = NULL;
3486
3487 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3488 // both a depth and stencil buffer, it will be the same buffer.
3489
3490 if (mask & GL_DEPTH_BUFFER_BIT)
3491 {
3492 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3493 {
3494 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3495 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3496 {
3497 return error(GL_INVALID_OPERATION);
3498 }
3499
3500 blitDepthStencil = true;
3501 readDSBuffer = readFramebuffer->getDepthbuffer();
3502 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3503 }
3504 }
3505
3506 if (mask & GL_STENCIL_BUFFER_BIT)
3507 {
3508 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3509 {
3510 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3511 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3512 {
3513 return error(GL_INVALID_OPERATION);
3514 }
3515
3516 blitDepthStencil = true;
3517 readDSBuffer = readFramebuffer->getStencilbuffer();
3518 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3519 }
3520 }
3521
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003522 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003523 {
3524 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3525 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3526 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003527
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003528 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3529 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003530 {
3531 return error(GL_INVALID_OPERATION);
3532 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003533 }
3534
3535 if (blitRenderTarget || blitDepthStencil)
3536 {
3537 egl::Display *display = getDisplay();
3538 display->endScene();
3539
3540 if (blitRenderTarget)
3541 {
3542 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3543 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3544
3545 if (FAILED(result))
3546 {
3547 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3548 return;
3549 }
3550 }
3551
3552 if (blitDepthStencil)
3553 {
3554 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3555
3556 if (FAILED(result))
3557 {
3558 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3559 return;
3560 }
3561 }
3562 }
3563}
3564
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003565VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3566{
3567 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3568 {
3569 mVertexDeclCache[i].vertexDeclaration = NULL;
3570 mVertexDeclCache[i].lruCount = 0;
3571 }
3572}
3573
3574VertexDeclarationCache::~VertexDeclarationCache()
3575{
3576 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3577 {
3578 if (mVertexDeclCache[i].vertexDeclaration)
3579 {
3580 mVertexDeclCache[i].vertexDeclaration->Release();
3581 }
3582 }
3583}
3584
3585GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3586{
3587 IDirect3DDevice9 *device = getDevice();
3588
3589 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3590 D3DVERTEXELEMENT9 *element = &elements[0];
3591
3592 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3593 {
3594 if (attributes[i].active)
3595 {
3596 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3597
3598 element->Stream = i;
3599 element->Offset = 0;
3600 element->Type = attributes[i].type;
3601 element->Method = D3DDECLMETHOD_DEFAULT;
3602 element->Usage = D3DDECLUSAGE_TEXCOORD;
3603 element->UsageIndex = program->getSemanticIndex(i);
3604 element++;
3605 }
3606 }
3607
3608 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3609 *(element++) = end;
3610
3611 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3612 {
3613 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3614 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3615 {
3616 entry->lruCount = ++mMaxLru;
3617 device->SetVertexDeclaration(entry->vertexDeclaration);
3618
3619 return GL_NO_ERROR;
3620 }
3621 }
3622
3623 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3624
3625 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3626 {
3627 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3628 {
3629 lastCache = &mVertexDeclCache[i];
3630 }
3631 }
3632
3633 if (lastCache->vertexDeclaration != NULL)
3634 {
3635 lastCache->vertexDeclaration->Release();
3636 lastCache->vertexDeclaration = NULL;
3637 }
3638
3639 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3640 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3641 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3642 lastCache->lruCount = ++mMaxLru;
3643
3644 return GL_NO_ERROR;
3645}
3646
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003647}
3648
3649extern "C"
3650{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003651gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003652{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003653 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003654}
3655
3656void glDestroyContext(gl::Context *context)
3657{
3658 delete context;
3659
3660 if (context == gl::getContext())
3661 {
3662 gl::makeCurrent(NULL, NULL, NULL);
3663 }
3664}
3665
3666void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3667{
3668 gl::makeCurrent(context, display, surface);
3669}
3670
3671gl::Context *glGetCurrentContext()
3672{
3673 return gl::getContext();
3674}
3675}