blob: 268a47a152183af1fa62f1895cb2d16c2000d935 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// 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
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034namespace gl
35{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +000036Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037{
38 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000039
daniel@transgaming.com428d1582010-05-04 03:35:25 +000040 mState.depthClearValue = 1.0f;
41 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042
daniel@transgaming.com428d1582010-05-04 03:35:25 +000043 mState.cullFace = false;
44 mState.cullMode = GL_BACK;
45 mState.frontFace = GL_CCW;
46 mState.depthTest = false;
47 mState.depthFunc = GL_LESS;
48 mState.blend = false;
49 mState.sourceBlendRGB = GL_ONE;
50 mState.sourceBlendAlpha = GL_ONE;
51 mState.destBlendRGB = GL_ZERO;
52 mState.destBlendAlpha = GL_ZERO;
53 mState.blendEquationRGB = GL_FUNC_ADD;
54 mState.blendEquationAlpha = GL_FUNC_ADD;
55 mState.blendColor.red = 0;
56 mState.blendColor.green = 0;
57 mState.blendColor.blue = 0;
58 mState.blendColor.alpha = 0;
59 mState.stencilTest = false;
60 mState.stencilFunc = GL_ALWAYS;
61 mState.stencilRef = 0;
62 mState.stencilMask = -1;
63 mState.stencilWritemask = -1;
64 mState.stencilBackFunc = GL_ALWAYS;
65 mState.stencilBackRef = 0;
66 mState.stencilBackMask = - 1;
67 mState.stencilBackWritemask = -1;
68 mState.stencilFail = GL_KEEP;
69 mState.stencilPassDepthFail = GL_KEEP;
70 mState.stencilPassDepthPass = GL_KEEP;
71 mState.stencilBackFail = GL_KEEP;
72 mState.stencilBackPassDepthFail = GL_KEEP;
73 mState.stencilBackPassDepthPass = GL_KEEP;
74 mState.polygonOffsetFill = false;
75 mState.polygonOffsetFactor = 0.0f;
76 mState.polygonOffsetUnits = 0.0f;
77 mState.sampleAlphaToCoverage = false;
78 mState.sampleCoverage = false;
79 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000080 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000081 mState.scissorTest = false;
82 mState.dither = true;
83 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000084 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085
daniel@transgaming.com428d1582010-05-04 03:35:25 +000086 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000087
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.viewportX = 0;
89 mState.viewportY = 0;
90 mState.viewportWidth = config->mDisplayMode.Width;
91 mState.viewportHeight = config->mDisplayMode.Height;
92 mState.zNear = 0.0f;
93 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094
daniel@transgaming.com428d1582010-05-04 03:35:25 +000095 mState.scissorX = 0;
96 mState.scissorY = 0;
97 mState.scissorWidth = config->mDisplayMode.Width;
98 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000100 mState.colorMaskRed = true;
101 mState.colorMaskGreen = true;
102 mState.colorMaskBlue = true;
103 mState.colorMaskAlpha = true;
104 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000106 if (shareContext != NULL)
107 {
108 mResourceManager = shareContext->mResourceManager;
109 mResourceManager->addRef();
110 }
111 else
112 {
113 mResourceManager = new ResourceManager();
114 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000115
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000116 // [OpenGL ES 2.0.24] section 3.7 page 83:
117 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
118 // and cube map texture state vectors respectively associated with them.
119 // In order that access to these initial textures not be lost, they are treated as texture
120 // objects all of whose names are 0.
121
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000122 mTexture2DZero.set(new Texture2D(0));
123 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000125 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000126 bindArrayBuffer(0);
127 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128 bindTextureCubeMap(0);
129 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000130 bindReadFramebuffer(0);
131 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132 bindRenderbuffer(0);
133
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000134 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000136 mState.packAlignment = 4;
137 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000138
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000139 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000140 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000141 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000142
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143 mInvalidEnum = false;
144 mInvalidValue = false;
145 mInvalidOperation = false;
146 mOutOfMemory = false;
147 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000148
149 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000150
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000151 mSupportsCompressedTextures = false;
152 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000153 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000154 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000155 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156}
157
158Context::~Context()
159{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000160 if (mState.currentProgram != 0)
161 {
162 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
163 if (programObject)
164 {
165 programObject->release();
166 }
167 mState.currentProgram = 0;
168 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000170 while (!mFramebufferMap.empty())
171 {
172 deleteFramebuffer(mFramebufferMap.begin()->first);
173 }
174
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000175 while (!mFenceMap.empty())
176 {
177 deleteFence(mFenceMap.begin()->first);
178 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000179
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000180 while (!mMultiSampleSupport.empty())
181 {
182 delete [] mMultiSampleSupport.begin()->second;
183 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
184 }
185
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000186 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
187 {
188 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
189 {
190 mState.samplerTexture[type][sampler].set(NULL);
191 }
192 }
193
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000194 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
195 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000196 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000197 }
198
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000199 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
200 {
201 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
202 }
203
204 mState.arrayBuffer.set(NULL);
205 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000206 mState.renderbuffer.set(NULL);
207
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000208 mTexture2DZero.set(NULL);
209 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000210
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000211 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000212 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000213 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000214
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000215 if (mMaskedClearSavedState)
216 {
217 mMaskedClearSavedState->Release();
218 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000219
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000220 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221}
222
223void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
224{
225 IDirect3DDevice9 *device = display->getDevice();
226
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000227 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000228 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000229 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000230
daniel@transgaming.com83921382011-01-08 05:46:00 +0000231 mVertexDataManager = new VertexDataManager(this, device);
232 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000233 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000234
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000235 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
236
237 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
238 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
239 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
240 mMaxRenderbufferDimension = mMaxTextureDimension;
241 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
242 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
243 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
244
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000245 const D3DFORMAT renderBufferFormats[] =
246 {
247 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000248 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000249 D3DFMT_R5G6B5,
250 D3DFMT_D24S8
251 };
252
253 int max = 0;
254 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
255 {
256 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
257 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
258 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
259
260 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
261 {
262 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
263 {
264 max = j;
265 }
266 }
267 }
268
269 mMaxSupportedSamples = max;
270
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000271 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000272 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000273 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
274 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000275 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
276 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000277
daniel@transgaming.com83921382011-01-08 05:46:00 +0000278 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
279
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000280 initExtensionString();
281
282 mState.viewportX = 0;
283 mState.viewportY = 0;
284 mState.viewportWidth = surface->getWidth();
285 mState.viewportHeight = surface->getHeight();
286
287 mState.scissorX = 0;
288 mState.scissorY = 0;
289 mState.scissorWidth = surface->getWidth();
290 mState.scissorHeight = surface->getHeight();
291
292 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000293 }
294
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
296 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000297 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000300 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000301 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
303 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000305 if (defaultRenderTarget)
306 {
307 defaultRenderTarget->Release();
308 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000310 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000312 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000314
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000315 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000316}
317
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000318// 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 +0000319void Context::markAllStateDirty()
320{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000321 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
322 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000323 mAppliedTextureSerial[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000324 }
325
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000326 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000327 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000328 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000329 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000330 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000331
332 mClearStateDirty = true;
333 mCullStateDirty = true;
334 mDepthStateDirty = true;
335 mMaskStateDirty = true;
336 mBlendStateDirty = true;
337 mStencilStateDirty = true;
338 mPolygonOffsetStateDirty = true;
339 mScissorStateDirty = true;
340 mSampleStateDirty = true;
341 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000342 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000343}
344
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345void Context::setClearColor(float red, float green, float blue, float alpha)
346{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000347 mState.colorClearValue.red = red;
348 mState.colorClearValue.green = green;
349 mState.colorClearValue.blue = blue;
350 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351}
352
353void Context::setClearDepth(float depth)
354{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000355 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356}
357
358void Context::setClearStencil(int stencil)
359{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000360 mState.stencilClearValue = stencil;
361}
362
363void Context::setCullFace(bool enabled)
364{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000365 if (mState.cullFace != enabled)
366 {
367 mState.cullFace = enabled;
368 mCullStateDirty = true;
369 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000370}
371
372bool Context::isCullFaceEnabled() const
373{
374 return mState.cullFace;
375}
376
377void Context::setCullMode(GLenum mode)
378{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000379 if (mState.cullMode != mode)
380 {
381 mState.cullMode = mode;
382 mCullStateDirty = true;
383 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000384}
385
386void Context::setFrontFace(GLenum front)
387{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000388 if (mState.frontFace != front)
389 {
390 mState.frontFace = front;
391 mFrontFaceDirty = true;
392 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000393}
394
395void Context::setDepthTest(bool enabled)
396{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000397 if (mState.depthTest != enabled)
398 {
399 mState.depthTest = enabled;
400 mDepthStateDirty = true;
401 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000402}
403
404bool Context::isDepthTestEnabled() const
405{
406 return mState.depthTest;
407}
408
409void Context::setDepthFunc(GLenum depthFunc)
410{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000411 if (mState.depthFunc != depthFunc)
412 {
413 mState.depthFunc = depthFunc;
414 mDepthStateDirty = true;
415 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000416}
417
418void Context::setDepthRange(float zNear, float zFar)
419{
420 mState.zNear = zNear;
421 mState.zFar = zFar;
422}
423
424void Context::setBlend(bool enabled)
425{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000426 if (mState.blend != enabled)
427 {
428 mState.blend = enabled;
429 mBlendStateDirty = true;
430 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000431}
432
433bool Context::isBlendEnabled() const
434{
435 return mState.blend;
436}
437
438void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
439{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000440 if (mState.sourceBlendRGB != sourceRGB ||
441 mState.sourceBlendAlpha != sourceAlpha ||
442 mState.destBlendRGB != destRGB ||
443 mState.destBlendAlpha != destAlpha)
444 {
445 mState.sourceBlendRGB = sourceRGB;
446 mState.destBlendRGB = destRGB;
447 mState.sourceBlendAlpha = sourceAlpha;
448 mState.destBlendAlpha = destAlpha;
449 mBlendStateDirty = true;
450 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000451}
452
453void Context::setBlendColor(float red, float green, float blue, float alpha)
454{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000455 if (mState.blendColor.red != red ||
456 mState.blendColor.green != green ||
457 mState.blendColor.blue != blue ||
458 mState.blendColor.alpha != alpha)
459 {
460 mState.blendColor.red = red;
461 mState.blendColor.green = green;
462 mState.blendColor.blue = blue;
463 mState.blendColor.alpha = alpha;
464 mBlendStateDirty = true;
465 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000466}
467
468void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
469{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000470 if (mState.blendEquationRGB != rgbEquation ||
471 mState.blendEquationAlpha != alphaEquation)
472 {
473 mState.blendEquationRGB = rgbEquation;
474 mState.blendEquationAlpha = alphaEquation;
475 mBlendStateDirty = true;
476 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000477}
478
479void Context::setStencilTest(bool enabled)
480{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000481 if (mState.stencilTest != enabled)
482 {
483 mState.stencilTest = enabled;
484 mStencilStateDirty = true;
485 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000486}
487
488bool Context::isStencilTestEnabled() const
489{
490 return mState.stencilTest;
491}
492
493void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
494{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000495 if (mState.stencilFunc != stencilFunc ||
496 mState.stencilRef != stencilRef ||
497 mState.stencilMask != stencilMask)
498 {
499 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000500 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000501 mState.stencilMask = stencilMask;
502 mStencilStateDirty = true;
503 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000504}
505
506void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
507{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000508 if (mState.stencilBackFunc != stencilBackFunc ||
509 mState.stencilBackRef != stencilBackRef ||
510 mState.stencilBackMask != stencilBackMask)
511 {
512 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000513 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000514 mState.stencilBackMask = stencilBackMask;
515 mStencilStateDirty = true;
516 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000517}
518
519void Context::setStencilWritemask(GLuint stencilWritemask)
520{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000521 if (mState.stencilWritemask != stencilWritemask)
522 {
523 mState.stencilWritemask = stencilWritemask;
524 mStencilStateDirty = true;
525 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000526}
527
528void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
529{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000530 if (mState.stencilBackWritemask != stencilBackWritemask)
531 {
532 mState.stencilBackWritemask = stencilBackWritemask;
533 mStencilStateDirty = true;
534 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000535}
536
537void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
538{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000539 if (mState.stencilFail != stencilFail ||
540 mState.stencilPassDepthFail != stencilPassDepthFail ||
541 mState.stencilPassDepthPass != stencilPassDepthPass)
542 {
543 mState.stencilFail = stencilFail;
544 mState.stencilPassDepthFail = stencilPassDepthFail;
545 mState.stencilPassDepthPass = stencilPassDepthPass;
546 mStencilStateDirty = true;
547 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000548}
549
550void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
551{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000552 if (mState.stencilBackFail != stencilBackFail ||
553 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
554 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
555 {
556 mState.stencilBackFail = stencilBackFail;
557 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
558 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
559 mStencilStateDirty = true;
560 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000561}
562
563void Context::setPolygonOffsetFill(bool enabled)
564{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000565 if (mState.polygonOffsetFill != enabled)
566 {
567 mState.polygonOffsetFill = enabled;
568 mPolygonOffsetStateDirty = true;
569 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000570}
571
572bool Context::isPolygonOffsetFillEnabled() const
573{
574 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000575
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000576}
577
578void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
579{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000580 if (mState.polygonOffsetFactor != factor ||
581 mState.polygonOffsetUnits != units)
582 {
583 mState.polygonOffsetFactor = factor;
584 mState.polygonOffsetUnits = units;
585 mPolygonOffsetStateDirty = true;
586 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000587}
588
589void Context::setSampleAlphaToCoverage(bool enabled)
590{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000591 if (mState.sampleAlphaToCoverage != enabled)
592 {
593 mState.sampleAlphaToCoverage = enabled;
594 mSampleStateDirty = true;
595 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000596}
597
598bool Context::isSampleAlphaToCoverageEnabled() const
599{
600 return mState.sampleAlphaToCoverage;
601}
602
603void Context::setSampleCoverage(bool enabled)
604{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000605 if (mState.sampleCoverage != enabled)
606 {
607 mState.sampleCoverage = enabled;
608 mSampleStateDirty = true;
609 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000610}
611
612bool Context::isSampleCoverageEnabled() const
613{
614 return mState.sampleCoverage;
615}
616
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000617void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000618{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000619 if (mState.sampleCoverageValue != value ||
620 mState.sampleCoverageInvert != invert)
621 {
622 mState.sampleCoverageValue = value;
623 mState.sampleCoverageInvert = invert;
624 mSampleStateDirty = true;
625 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000626}
627
628void Context::setScissorTest(bool enabled)
629{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000630 if (mState.scissorTest != enabled)
631 {
632 mState.scissorTest = enabled;
633 mScissorStateDirty = true;
634 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000635}
636
637bool Context::isScissorTestEnabled() const
638{
639 return mState.scissorTest;
640}
641
642void Context::setDither(bool enabled)
643{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000644 if (mState.dither != enabled)
645 {
646 mState.dither = enabled;
647 mDitherStateDirty = true;
648 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000649}
650
651bool Context::isDitherEnabled() const
652{
653 return mState.dither;
654}
655
656void Context::setLineWidth(GLfloat width)
657{
658 mState.lineWidth = width;
659}
660
661void Context::setGenerateMipmapHint(GLenum hint)
662{
663 mState.generateMipmapHint = hint;
664}
665
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000666void Context::setFragmentShaderDerivativeHint(GLenum hint)
667{
668 mState.fragmentShaderDerivativeHint = hint;
669 // TODO: Propagate the hint to shader translator so we can write
670 // ddx, ddx_coarse, or ddx_fine depending on the hint.
671 // Ignore for now. It is valid for implementations to ignore hint.
672}
673
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000674void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
675{
676 mState.viewportX = x;
677 mState.viewportY = y;
678 mState.viewportWidth = width;
679 mState.viewportHeight = height;
680}
681
682void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
683{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000684 if (mState.scissorX != x || mState.scissorY != y ||
685 mState.scissorWidth != width || mState.scissorHeight != height)
686 {
687 mState.scissorX = x;
688 mState.scissorY = y;
689 mState.scissorWidth = width;
690 mState.scissorHeight = height;
691 mScissorStateDirty = true;
692 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000693}
694
695void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
696{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000697 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
698 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
699 {
700 mState.colorMaskRed = red;
701 mState.colorMaskGreen = green;
702 mState.colorMaskBlue = blue;
703 mState.colorMaskAlpha = alpha;
704 mMaskStateDirty = true;
705 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000706}
707
708void Context::setDepthMask(bool mask)
709{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000710 if (mState.depthMask != mask)
711 {
712 mState.depthMask = mask;
713 mMaskStateDirty = true;
714 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000715}
716
717void Context::setActiveSampler(int active)
718{
719 mState.activeSampler = active;
720}
721
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000722GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000723{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000724 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000725}
726
727GLuint Context::getDrawFramebufferHandle() const
728{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000729 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000730}
731
732GLuint Context::getRenderbufferHandle() const
733{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000734 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000735}
736
737GLuint Context::getArrayBufferHandle() const
738{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000739 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000740}
741
daniel@transgaming.com83921382011-01-08 05:46:00 +0000742void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000743{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000744 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000745}
746
daniel@transgaming.com83921382011-01-08 05:46:00 +0000747const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000748{
749 return mState.vertexAttribute[attribNum];
750}
751
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000752void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000753 GLsizei stride, const void *pointer)
754{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000755 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000756 mState.vertexAttribute[attribNum].mSize = size;
757 mState.vertexAttribute[attribNum].mType = type;
758 mState.vertexAttribute[attribNum].mNormalized = normalized;
759 mState.vertexAttribute[attribNum].mStride = stride;
760 mState.vertexAttribute[attribNum].mPointer = pointer;
761}
762
763const void *Context::getVertexAttribPointer(unsigned int attribNum) const
764{
765 return mState.vertexAttribute[attribNum].mPointer;
766}
767
daniel@transgaming.com83921382011-01-08 05:46:00 +0000768const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000769{
770 return mState.vertexAttribute;
771}
772
773void Context::setPackAlignment(GLint alignment)
774{
775 mState.packAlignment = alignment;
776}
777
778GLint Context::getPackAlignment() const
779{
780 return mState.packAlignment;
781}
782
783void Context::setUnpackAlignment(GLint alignment)
784{
785 mState.unpackAlignment = alignment;
786}
787
788GLint Context::getUnpackAlignment() const
789{
790 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791}
792
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793GLuint Context::createBuffer()
794{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000795 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798GLuint Context::createProgram()
799{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000800 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801}
802
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000803GLuint Context::createShader(GLenum type)
804{
805 return mResourceManager->createShader(type);
806}
807
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808GLuint Context::createTexture()
809{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000810 return mResourceManager->createTexture();
811}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000813GLuint Context::createRenderbuffer()
814{
815 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816}
817
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000818// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819GLuint Context::createFramebuffer()
820{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000821 unsigned int handle = 1;
822
823 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
824 {
825 handle++;
826 }
827
828 mFramebufferMap[handle] = NULL;
829
830 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831}
832
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000833GLuint Context::createFence()
834{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000835 unsigned int handle = 0;
836
837 while (mFenceMap.find(handle) != mFenceMap.end())
838 {
839 handle++;
840 }
841
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
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000895 delete framebufferObject->second;
896 mFramebufferMap.erase(framebufferObject);
897 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000899
900void Context::deleteFence(GLuint fence)
901{
902 FenceMap::iterator fenceObject = mFenceMap.find(fence);
903
904 if (fenceObject != mFenceMap.end())
905 {
906 delete fenceObject->second;
907 mFenceMap.erase(fenceObject);
908 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000909}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000911Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000913 return mResourceManager->getBuffer(handle);
914}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000916Shader *Context::getShader(GLuint handle)
917{
918 return mResourceManager->getShader(handle);
919}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000921Program *Context::getProgram(GLuint handle)
922{
923 return mResourceManager->getProgram(handle);
924}
925
926Texture *Context::getTexture(GLuint handle)
927{
928 return mResourceManager->getTexture(handle);
929}
930
931Renderbuffer *Context::getRenderbuffer(GLuint handle)
932{
933 return mResourceManager->getRenderbuffer(handle);
934}
935
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000936Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000937{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000938 return getFramebuffer(mState.readFramebuffer);
939}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000940
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000941Framebuffer *Context::getDrawFramebuffer()
942{
943 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944}
945
946void Context::bindArrayBuffer(unsigned int buffer)
947{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000948 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000950 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951}
952
953void Context::bindElementArrayBuffer(unsigned int buffer)
954{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000955 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000957 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958}
959
960void Context::bindTexture2D(GLuint texture)
961{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000962 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000964 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965}
966
967void Context::bindTextureCubeMap(GLuint texture)
968{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000969 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000971 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000974void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000976 if (!getFramebuffer(framebuffer))
977 {
978 mFramebufferMap[framebuffer] = new Framebuffer();
979 }
980
981 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000982}
983
984void Context::bindDrawFramebuffer(GLuint framebuffer)
985{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000986 if (!getFramebuffer(framebuffer))
987 {
988 mFramebufferMap[framebuffer] = new Framebuffer();
989 }
990
991 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992}
993
994void Context::bindRenderbuffer(GLuint renderbuffer)
995{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000996 mResourceManager->checkRenderbufferAllocation(renderbuffer);
997
998 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999}
1000
1001void Context::useProgram(GLuint program)
1002{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001003 GLuint priorProgram = mState.currentProgram;
1004 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001005
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001006 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001008 Program *newProgram = mResourceManager->getProgram(program);
1009 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1010
1011 if (newProgram)
1012 {
1013 newProgram->addRef();
1014 }
1015
1016 if (oldProgram)
1017 {
1018 oldProgram->release();
1019 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001020 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021}
1022
1023void Context::setFramebufferZero(Framebuffer *buffer)
1024{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001025 delete mFramebufferMap[0];
1026 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027}
1028
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001029void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001031 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1032 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033}
1034
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001035Framebuffer *Context::getFramebuffer(unsigned int handle)
1036{
1037 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1038
1039 if (framebuffer == mFramebufferMap.end())
1040 {
1041 return NULL;
1042 }
1043 else
1044 {
1045 return framebuffer->second;
1046 }
1047}
1048
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001049Fence *Context::getFence(unsigned int handle)
1050{
1051 FenceMap::iterator fence = mFenceMap.find(handle);
1052
1053 if (fence == mFenceMap.end())
1054 {
1055 return NULL;
1056 }
1057 else
1058 {
1059 return fence->second;
1060 }
1061}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001062
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063Buffer *Context::getArrayBuffer()
1064{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001065 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066}
1067
1068Buffer *Context::getElementArrayBuffer()
1069{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001070 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071}
1072
1073Program *Context::getCurrentProgram()
1074{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001075 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076}
1077
1078Texture2D *Context::getTexture2D()
1079{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001080 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, SAMPLER_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081}
1082
1083TextureCubeMap *Context::getTextureCubeMap()
1084{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001085 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, SAMPLER_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001088Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001090 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001091
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001092 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001093 {
1094 switch (type)
1095 {
1096 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001097 case SAMPLER_2D: return mTexture2DZero.get();
1098 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001099 }
1100 }
1101
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001102 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103}
1104
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001105bool Context::getBooleanv(GLenum pname, GLboolean *params)
1106{
1107 switch (pname)
1108 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001109 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1110 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1111 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001112 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001113 params[0] = mState.colorMaskRed;
1114 params[1] = mState.colorMaskGreen;
1115 params[2] = mState.colorMaskBlue;
1116 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001117 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001118 case GL_CULL_FACE: *params = mState.cullFace; break;
1119 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1120 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1121 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1122 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1123 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1124 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1125 case GL_BLEND: *params = mState.blend; break;
1126 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001127 default:
1128 return false;
1129 }
1130
1131 return true;
1132}
1133
1134bool Context::getFloatv(GLenum pname, GLfloat *params)
1135{
1136 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1137 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1138 // GetIntegerv as its native query function. As it would require conversion in any
1139 // case, this should make no difference to the calling application.
1140 switch (pname)
1141 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001142 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1143 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1144 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1145 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1146 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001147 case GL_ALIASED_LINE_WIDTH_RANGE:
1148 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1149 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1150 break;
1151 case GL_ALIASED_POINT_SIZE_RANGE:
1152 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001153 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 +00001154 break;
1155 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001156 params[0] = mState.zNear;
1157 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001158 break;
1159 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001160 params[0] = mState.colorClearValue.red;
1161 params[1] = mState.colorClearValue.green;
1162 params[2] = mState.colorClearValue.blue;
1163 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001164 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001165 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001166 params[0] = mState.blendColor.red;
1167 params[1] = mState.blendColor.green;
1168 params[2] = mState.blendColor.blue;
1169 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001170 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001171 default:
1172 return false;
1173 }
1174
1175 return true;
1176}
1177
1178bool Context::getIntegerv(GLenum pname, GLint *params)
1179{
1180 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1181 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1182 // GetIntegerv as its native query function. As it would require conversion in any
1183 // case, this should make no difference to the calling application. You may find it in
1184 // Context::getFloatv.
1185 switch (pname)
1186 {
1187 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1188 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001189 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001190 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1191 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1192 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001193 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001194 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001195 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001196 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001197 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1198 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001199 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1200 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1201 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001202 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001203 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1204 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1205 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1206 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001207 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001208 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1209 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1210 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1211 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1212 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1213 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1214 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1215 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1216 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1217 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1218 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1219 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1220 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1221 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1222 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1223 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1224 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1225 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1226 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1227 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1228 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1229 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1230 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1231 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001232 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1233 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001234 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1235 {
1236 if (supportsCompressedTextures())
1237 {
1238 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1239 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1240 *params = 2;
1241 }
1242 else
1243 {
1244 *params = 0;
1245 }
1246 }
1247 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001248 case GL_MAX_SAMPLES_ANGLE:
1249 {
1250 GLsizei maxSamples = getMaxSupportedSamples();
1251 if (maxSamples != 0)
1252 {
1253 *params = maxSamples;
1254 }
1255 else
1256 {
1257 return false;
1258 }
1259
1260 break;
1261 }
1262 case GL_SAMPLE_BUFFERS:
1263 case GL_SAMPLES:
1264 {
1265 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1266 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1267 {
1268 switch (pname)
1269 {
1270 case GL_SAMPLE_BUFFERS:
1271 if (framebuffer->getSamples() != 0)
1272 {
1273 *params = 1;
1274 }
1275 else
1276 {
1277 *params = 0;
1278 }
1279 break;
1280 case GL_SAMPLES:
1281 *params = framebuffer->getSamples();
1282 break;
1283 }
1284 }
1285 else
1286 {
1287 *params = 0;
1288 }
1289 }
1290 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001291 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1292 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1293 case GL_MAX_VIEWPORT_DIMS:
1294 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001295 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001296 params[0] = maxDimension;
1297 params[1] = maxDimension;
1298 }
1299 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001300 case GL_COMPRESSED_TEXTURE_FORMATS:
1301 {
1302 if (supportsCompressedTextures())
1303 {
1304 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1305 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1306 }
1307 }
1308 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001309 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001310 params[0] = mState.viewportX;
1311 params[1] = mState.viewportY;
1312 params[2] = mState.viewportWidth;
1313 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001314 break;
1315 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001316 params[0] = mState.scissorX;
1317 params[1] = mState.scissorY;
1318 params[2] = mState.scissorWidth;
1319 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001320 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001321 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1322 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001323 case GL_RED_BITS:
1324 case GL_GREEN_BITS:
1325 case GL_BLUE_BITS:
1326 case GL_ALPHA_BITS:
1327 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001328 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001329 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1330
1331 if (colorbuffer)
1332 {
1333 switch (pname)
1334 {
1335 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1336 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1337 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1338 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1339 }
1340 }
1341 else
1342 {
1343 *params = 0;
1344 }
1345 }
1346 break;
1347 case GL_DEPTH_BITS:
1348 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001349 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001350 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001351
1352 if (depthbuffer)
1353 {
1354 *params = depthbuffer->getDepthSize();
1355 }
1356 else
1357 {
1358 *params = 0;
1359 }
1360 }
1361 break;
1362 case GL_STENCIL_BITS:
1363 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001364 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001365 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001366
1367 if (stencilbuffer)
1368 {
1369 *params = stencilbuffer->getStencilSize();
1370 }
1371 else
1372 {
1373 *params = 0;
1374 }
1375 }
1376 break;
1377 case GL_TEXTURE_BINDING_2D:
1378 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001379 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001380 {
1381 error(GL_INVALID_OPERATION);
1382 return false;
1383 }
1384
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001385 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001386 }
1387 break;
1388 case GL_TEXTURE_BINDING_CUBE_MAP:
1389 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001390 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 {
1392 error(GL_INVALID_OPERATION);
1393 return false;
1394 }
1395
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001396 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001397 }
1398 break;
1399 default:
1400 return false;
1401 }
1402
1403 return true;
1404}
1405
1406bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1407{
1408 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1409 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1410 // to the fact that it is stored internally as a float, and so would require conversion
1411 // if returned from Context::getIntegerv. Since this conversion is already implemented
1412 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1413 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1414 // application.
1415 switch (pname)
1416 {
1417 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1418 case GL_SHADER_BINARY_FORMATS:
1419 {
1420 *type = GL_INT;
1421 *numParams = 0;
1422 }
1423 break;
1424 case GL_MAX_VERTEX_ATTRIBS:
1425 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1426 case GL_MAX_VARYING_VECTORS:
1427 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1428 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1429 case GL_MAX_TEXTURE_IMAGE_UNITS:
1430 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1431 case GL_MAX_RENDERBUFFER_SIZE:
1432 case GL_NUM_SHADER_BINARY_FORMATS:
1433 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1434 case GL_ARRAY_BUFFER_BINDING:
1435 case GL_FRAMEBUFFER_BINDING:
1436 case GL_RENDERBUFFER_BINDING:
1437 case GL_CURRENT_PROGRAM:
1438 case GL_PACK_ALIGNMENT:
1439 case GL_UNPACK_ALIGNMENT:
1440 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001441 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001442 case GL_RED_BITS:
1443 case GL_GREEN_BITS:
1444 case GL_BLUE_BITS:
1445 case GL_ALPHA_BITS:
1446 case GL_DEPTH_BITS:
1447 case GL_STENCIL_BITS:
1448 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1449 case GL_CULL_FACE_MODE:
1450 case GL_FRONT_FACE:
1451 case GL_ACTIVE_TEXTURE:
1452 case GL_STENCIL_FUNC:
1453 case GL_STENCIL_VALUE_MASK:
1454 case GL_STENCIL_REF:
1455 case GL_STENCIL_FAIL:
1456 case GL_STENCIL_PASS_DEPTH_FAIL:
1457 case GL_STENCIL_PASS_DEPTH_PASS:
1458 case GL_STENCIL_BACK_FUNC:
1459 case GL_STENCIL_BACK_VALUE_MASK:
1460 case GL_STENCIL_BACK_REF:
1461 case GL_STENCIL_BACK_FAIL:
1462 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1463 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1464 case GL_DEPTH_FUNC:
1465 case GL_BLEND_SRC_RGB:
1466 case GL_BLEND_SRC_ALPHA:
1467 case GL_BLEND_DST_RGB:
1468 case GL_BLEND_DST_ALPHA:
1469 case GL_BLEND_EQUATION_RGB:
1470 case GL_BLEND_EQUATION_ALPHA:
1471 case GL_STENCIL_WRITEMASK:
1472 case GL_STENCIL_BACK_WRITEMASK:
1473 case GL_STENCIL_CLEAR_VALUE:
1474 case GL_SUBPIXEL_BITS:
1475 case GL_MAX_TEXTURE_SIZE:
1476 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1477 case GL_SAMPLE_BUFFERS:
1478 case GL_SAMPLES:
1479 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1480 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1481 case GL_TEXTURE_BINDING_2D:
1482 case GL_TEXTURE_BINDING_CUBE_MAP:
1483 {
1484 *type = GL_INT;
1485 *numParams = 1;
1486 }
1487 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001488 case GL_MAX_SAMPLES_ANGLE:
1489 {
1490 if (getMaxSupportedSamples() != 0)
1491 {
1492 *type = GL_INT;
1493 *numParams = 1;
1494 }
1495 else
1496 {
1497 return false;
1498 }
1499 }
1500 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001501 case GL_MAX_VIEWPORT_DIMS:
1502 {
1503 *type = GL_INT;
1504 *numParams = 2;
1505 }
1506 break;
1507 case GL_VIEWPORT:
1508 case GL_SCISSOR_BOX:
1509 {
1510 *type = GL_INT;
1511 *numParams = 4;
1512 }
1513 break;
1514 case GL_SHADER_COMPILER:
1515 case GL_SAMPLE_COVERAGE_INVERT:
1516 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001517 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1518 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1519 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1520 case GL_SAMPLE_COVERAGE:
1521 case GL_SCISSOR_TEST:
1522 case GL_STENCIL_TEST:
1523 case GL_DEPTH_TEST:
1524 case GL_BLEND:
1525 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001526 {
1527 *type = GL_BOOL;
1528 *numParams = 1;
1529 }
1530 break;
1531 case GL_COLOR_WRITEMASK:
1532 {
1533 *type = GL_BOOL;
1534 *numParams = 4;
1535 }
1536 break;
1537 case GL_POLYGON_OFFSET_FACTOR:
1538 case GL_POLYGON_OFFSET_UNITS:
1539 case GL_SAMPLE_COVERAGE_VALUE:
1540 case GL_DEPTH_CLEAR_VALUE:
1541 case GL_LINE_WIDTH:
1542 {
1543 *type = GL_FLOAT;
1544 *numParams = 1;
1545 }
1546 break;
1547 case GL_ALIASED_LINE_WIDTH_RANGE:
1548 case GL_ALIASED_POINT_SIZE_RANGE:
1549 case GL_DEPTH_RANGE:
1550 {
1551 *type = GL_FLOAT;
1552 *numParams = 2;
1553 }
1554 break;
1555 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001556 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001557 {
1558 *type = GL_FLOAT;
1559 *numParams = 4;
1560 }
1561 break;
1562 default:
1563 return false;
1564 }
1565
1566 return true;
1567}
1568
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001569// Applies the render target surface, depth stencil surface, viewport rectangle and
1570// scissor rectangle to the Direct3D 9 device
1571bool Context::applyRenderTarget(bool ignoreViewport)
1572{
1573 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001574
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001575 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001576
1577 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1578 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001579 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001580 }
1581
1582 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001583
1584 if (!renderTarget)
1585 {
1586 return false; // Context must be lost
1587 }
1588
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001589 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001590
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001591 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1592 if (renderTargetSerial != mAppliedRenderTargetSerial)
1593 {
1594 device->SetRenderTarget(0, renderTarget);
1595 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001596 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 +00001597 }
1598
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001599 unsigned int depthbufferSerial = 0;
1600 unsigned int stencilbufferSerial = 0;
1601 if (framebufferObject->getDepthbufferType() != GL_NONE)
1602 {
1603 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001604 if (!depthStencil)
1605 {
1606 ERR("Depth stencil pointer unexpectedly null.");
1607 return false;
1608 }
1609
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001610 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1611 }
1612 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1613 {
1614 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001615 if (!depthStencil)
1616 {
1617 ERR("Depth stencil pointer unexpectedly null.");
1618 return false;
1619 }
1620
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001621 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1622 }
1623
1624 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001625 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001626 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001627 {
1628 device->SetDepthStencilSurface(depthStencil);
1629 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001630 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001631 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001632 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001633
1634 D3DVIEWPORT9 viewport;
1635 D3DSURFACE_DESC desc;
1636 renderTarget->GetDesc(&desc);
1637
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001638 float zNear = clamp01(mState.zNear);
1639 float zFar = clamp01(mState.zFar);
1640
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641 if (ignoreViewport)
1642 {
1643 viewport.X = 0;
1644 viewport.Y = 0;
1645 viewport.Width = desc.Width;
1646 viewport.Height = desc.Height;
1647 viewport.MinZ = 0.0f;
1648 viewport.MaxZ = 1.0f;
1649 }
1650 else
1651 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001652 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1653 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1654 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1655 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1656 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 +00001657 viewport.MinZ = zNear;
1658 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001659 }
1660
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001661 if (viewport.Width <= 0 || viewport.Height <= 0)
1662 {
1663 return false; // Nothing to render
1664 }
1665
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666 device->SetViewport(&viewport);
1667
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001668 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001670 if (mState.scissorTest)
1671 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001672 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1673 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1674 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1675 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1676 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001677 device->SetScissorRect(&rect);
1678 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1679 }
1680 else
1681 {
1682 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1683 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001684
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001685 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001686 }
1687
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001688 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690 Program *programObject = getCurrentProgram();
1691
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001692 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001693 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001694 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001695
daniel@transgaming.com31754962010-11-28 02:02:52 +00001696 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001697 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1698 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1699 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001700 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001701
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001702 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001703 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001704 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001705
daniel@transgaming.com31754962010-11-28 02:02:52 +00001706 GLint depthRange = programObject->getDxDepthRangeLocation();
1707 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1708 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709 }
1710
1711 return true;
1712}
1713
1714// 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 +00001715void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716{
1717 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001718 Program *programObject = getCurrentProgram();
1719
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001720 Framebuffer *framebufferObject = getDrawFramebuffer();
1721
1722 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1723
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001724 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001725 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001726 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001728 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001729 GLint alwaysFront = !isTriangleMode(drawMode);
1730 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1731
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001732 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001734 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001736 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737 }
1738 else
1739 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001740 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001741 }
1742
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001743 mCullStateDirty = false;
1744 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001746 if (mDepthStateDirty)
1747 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001748 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001750 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1751 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 }
1753 else
1754 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001755 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757
1758 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 }
1760
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001761 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001764 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1766
1767 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1768 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1769 {
1770 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1771 }
1772 else
1773 {
1774 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1775 unorm<8>(mState.blendColor.alpha),
1776 unorm<8>(mState.blendColor.alpha),
1777 unorm<8>(mState.blendColor.alpha)));
1778 }
1779
1780 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1781 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1782 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1783
1784 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1785 mState.destBlendRGB != mState.destBlendAlpha ||
1786 mState.blendEquationRGB != mState.blendEquationAlpha)
1787 {
1788 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1789
1790 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1791 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1792 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001793 }
1794 else
1795 {
1796 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1797 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001798 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001799 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001800 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001801 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001802 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001803
1804 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805 }
1806
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001807 if (mStencilStateDirty || mFrontFaceDirty)
1808 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001809 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001810 {
1811 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1812 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1813
1814 // FIXME: Unsupported by D3D9
1815 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1816 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1817 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1818 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1819 mState.stencilRef != mState.stencilBackRef ||
1820 mState.stencilMask != mState.stencilBackMask)
1821 {
1822 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1823 return error(GL_INVALID_OPERATION);
1824 }
1825
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001826 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001827 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001828 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1829
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001830 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1831 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001832 es2dx::ConvertComparison(mState.stencilFunc));
1833
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001834 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1835 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001836
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001837 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001838 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001839 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001840 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001841 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001842 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1843
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001844 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1845 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001846 es2dx::ConvertComparison(mState.stencilBackFunc));
1847
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001848 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1849 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001850
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001851 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001852 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001853 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001854 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001855 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001856 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1857 }
1858 else
1859 {
1860 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1861 }
1862
1863 mStencilStateDirty = false;
1864 }
1865
1866 if (mMaskStateDirty)
1867 {
1868 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1869 mState.colorMaskBlue, mState.colorMaskAlpha));
1870 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1871
1872 mMaskStateDirty = false;
1873 }
1874
1875 if (mPolygonOffsetStateDirty)
1876 {
1877 if (mState.polygonOffsetFill)
1878 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001879 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001880 if (depthbuffer)
1881 {
1882 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1883 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1884 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1885 }
1886 }
1887 else
1888 {
1889 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1890 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1891 }
1892
1893 mPolygonOffsetStateDirty = false;
1894 }
1895
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001896 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001898 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001899 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001900 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001901 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001902 FIXME("Sample alpha to coverage is unimplemented.");
1903 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001904
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001905 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1906 if (mState.sampleCoverage)
1907 {
1908 unsigned int mask = 0;
1909 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001910 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001911 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001912
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001913 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001914 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001915 mask <<= 1;
1916
1917 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1918 {
1919 threshold += 1.0f;
1920 mask |= 1;
1921 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001922 }
1923 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001924
1925 if (mState.sampleCoverageInvert)
1926 {
1927 mask = ~mask;
1928 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001929
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001930 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1931 }
1932 else
1933 {
1934 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1935 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001936 }
1937 else
1938 {
1939 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001940 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001941
1942 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943 }
1944
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001945 if (mDitherStateDirty)
1946 {
1947 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1948
1949 mDitherStateDirty = false;
1950 }
1951
1952 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953}
1954
daniel@transgaming.com83921382011-01-08 05:46:00 +00001955GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001956{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001957 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001958
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001959 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001960 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001961 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001962 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001963 }
1964
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00001965 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001966}
1967
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001968// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001969GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001970{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001971 IDirect3DDevice9 *device = getDevice();
1972 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001973
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001974 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001975 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001976 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001977 }
1978
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001979 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980}
1981
1982// Applies the shaders and shader constants to the Direct3D 9 device
1983void Context::applyShaders()
1984{
1985 IDirect3DDevice9 *device = getDevice();
1986 Program *programObject = getCurrentProgram();
1987 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1988 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1989
1990 device->SetVertexShader(vertexShader);
1991 device->SetPixelShader(pixelShader);
1992
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001993 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001994 {
1995 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001996 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001997 }
1998
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 programObject->applyUniforms();
2000}
2001
2002// Applies the textures and sampler states to the Direct3D 9 device
2003void Context::applyTextures()
2004{
2005 IDirect3DDevice9 *device = getDevice();
2006 Program *programObject = getCurrentProgram();
2007
2008 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2009 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002010 int textureUnit = programObject->getSamplerMapping(sampler);
2011 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002012 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002013 SamplerType textureType = programObject->getSamplerType(sampler);
2014
2015 Texture *texture = getSamplerTexture(textureUnit, textureType);
2016
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002017 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002018 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002019 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2020
2021 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002022 {
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002023 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter())
2024 {
2025 GLenum wrapS = texture->getWrapS();
2026 GLenum wrapT = texture->getWrapT();
2027 GLenum minFilter = texture->getMinFilter();
2028 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002030 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2031 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002033 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2034 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2035 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2036 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2037 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
2038 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002039
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002040 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyImage())
2041 {
2042 device->SetTexture(sampler, d3dTexture);
2043 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002044 }
2045 else
2046 {
2047 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2048 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002049
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002050 mAppliedTextureSerial[sampler] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002051 texture->resetDirty();
2052 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002054 else
2055 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002056 if (mAppliedTextureSerial[sampler] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002057 {
2058 device->SetTexture(sampler, NULL);
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002059 mAppliedTextureSerial[sampler] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002060 }
2061 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 }
2063}
2064
2065void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2066{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002067 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002068
2069 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2070 {
2071 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2072 }
2073
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002074 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2075 {
2076 return error(GL_INVALID_OPERATION);
2077 }
2078
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002080
2081 if (!renderTarget)
2082 {
2083 return; // Context must be lost, return silently
2084 }
2085
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 IDirect3DDevice9 *device = getDevice();
2087
2088 D3DSURFACE_DESC desc;
2089 renderTarget->GetDesc(&desc);
2090
2091 IDirect3DSurface9 *systemSurface;
2092 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2093
2094 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2095 {
2096 return error(GL_OUT_OF_MEMORY);
2097 }
2098
2099 ASSERT(SUCCEEDED(result));
2100
2101 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2102 {
2103 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2104 }
2105
2106 result = device->GetRenderTargetData(renderTarget, systemSurface);
2107
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108 if (FAILED(result))
2109 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 systemSurface->Release();
2111
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002112 switch (result)
2113 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002114 case D3DERR_DRIVERINTERNALERROR:
2115 case D3DERR_DEVICELOST:
2116 return error(GL_OUT_OF_MEMORY);
2117 default:
2118 UNREACHABLE();
2119 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002120 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002121 }
2122
2123 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002124 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2125 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2126 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2127 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2128 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002129
2130 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2131
2132 if (FAILED(result))
2133 {
2134 UNREACHABLE();
2135 systemSurface->Release();
2136
2137 return; // No sensible error to generate
2138 }
2139
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002140 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002142 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002143 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002144 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002145
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146 for (int j = 0; j < rect.bottom - rect.top; j++)
2147 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002148 if (desc.Format == D3DFMT_A8R8G8B8 &&
2149 format == GL_BGRA_EXT &&
2150 type == GL_UNSIGNED_BYTE)
2151 {
2152 // Fast path for EXT_read_format_bgra, given
2153 // an RGBA source buffer. Note that buffers with no
2154 // alpha go through the slow path below.
2155 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002156 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002157 (rect.right - rect.left) * 4);
2158 continue;
2159 }
2160
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161 for (int i = 0; i < rect.right - rect.left; i++)
2162 {
2163 float r;
2164 float g;
2165 float b;
2166 float a;
2167
2168 switch (desc.Format)
2169 {
2170 case D3DFMT_R5G6B5:
2171 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002172 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173
2174 a = 1.0f;
2175 b = (rgb & 0x001F) * (1.0f / 0x001F);
2176 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2177 r = (rgb & 0xF800) * (1.0f / 0xF800);
2178 }
2179 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180 case D3DFMT_A1R5G5B5:
2181 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002182 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002183
2184 a = (argb & 0x8000) ? 1.0f : 0.0f;
2185 b = (argb & 0x001F) * (1.0f / 0x001F);
2186 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2187 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2188 }
2189 break;
2190 case D3DFMT_A8R8G8B8:
2191 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002192 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002193
2194 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2195 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2196 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2197 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2198 }
2199 break;
2200 case D3DFMT_X8R8G8B8:
2201 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002202 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203
2204 a = 1.0f;
2205 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2206 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2207 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2208 }
2209 break;
2210 case D3DFMT_A2R10G10B10:
2211 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002212 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213
2214 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2215 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2216 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2217 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2218 }
2219 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002220 case D3DFMT_A32B32G32R32F:
2221 {
2222 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002223 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2224 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2225 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2226 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002227 }
2228 break;
2229 case D3DFMT_A16B16G16R16F:
2230 {
2231 // float formats in D3D are stored rgba, rather than the other way round
2232 float abgr[4];
2233
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002234 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002235
2236 a = abgr[3];
2237 b = abgr[2];
2238 g = abgr[1];
2239 r = abgr[0];
2240 }
2241 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242 default:
2243 UNIMPLEMENTED(); // FIXME
2244 UNREACHABLE();
2245 }
2246
2247 switch (format)
2248 {
2249 case GL_RGBA:
2250 switch (type)
2251 {
2252 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002253 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2254 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2255 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2256 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 break;
2258 default: UNREACHABLE();
2259 }
2260 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002261 case GL_BGRA_EXT:
2262 switch (type)
2263 {
2264 case GL_UNSIGNED_BYTE:
2265 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2266 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2267 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2268 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2269 break;
2270 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2271 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2272 // this type is packed as follows:
2273 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2274 // --------------------------------------------------------------------------------
2275 // | 4th | 3rd | 2nd | 1st component |
2276 // --------------------------------------------------------------------------------
2277 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2278 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2279 ((unsigned short)(15 * a + 0.5f) << 12)|
2280 ((unsigned short)(15 * r + 0.5f) << 8) |
2281 ((unsigned short)(15 * g + 0.5f) << 4) |
2282 ((unsigned short)(15 * b + 0.5f) << 0);
2283 break;
2284 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2285 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2286 // this type is packed as follows:
2287 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2288 // --------------------------------------------------------------------------------
2289 // | 4th | 3rd | 2nd | 1st component |
2290 // --------------------------------------------------------------------------------
2291 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2292 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2293 ((unsigned short)( a + 0.5f) << 15) |
2294 ((unsigned short)(31 * r + 0.5f) << 10) |
2295 ((unsigned short)(31 * g + 0.5f) << 5) |
2296 ((unsigned short)(31 * b + 0.5f) << 0);
2297 break;
2298 default: UNREACHABLE();
2299 }
2300 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002301 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302 switch (type)
2303 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002304 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002305 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2306 ((unsigned short)(31 * b + 0.5f) << 0) |
2307 ((unsigned short)(63 * g + 0.5f) << 5) |
2308 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002309 break;
2310 default: UNREACHABLE();
2311 }
2312 break;
2313 default: UNREACHABLE();
2314 }
2315 }
2316 }
2317
2318 systemSurface->UnlockRect();
2319
2320 systemSurface->Release();
2321}
2322
2323void Context::clear(GLbitfield mask)
2324{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002325 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002326
2327 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2328 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002329 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002330 }
2331
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002332 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333 IDirect3DDevice9 *device = getDevice();
2334 DWORD flags = 0;
2335
2336 if (mask & GL_COLOR_BUFFER_BIT)
2337 {
2338 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002339
2340 if (framebufferObject->getColorbufferType() != GL_NONE)
2341 {
2342 flags |= D3DCLEAR_TARGET;
2343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344 }
2345
2346 if (mask & GL_DEPTH_BUFFER_BIT)
2347 {
2348 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002349 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350 {
2351 flags |= D3DCLEAR_ZBUFFER;
2352 }
2353 }
2354
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 GLuint stencilUnmasked = 0x0;
2356
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002357 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002360 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002362 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002363 if (!depthStencil)
2364 {
2365 ERR("Depth stencil pointer unexpectedly null.");
2366 return;
2367 }
2368
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002369 D3DSURFACE_DESC desc;
2370 depthStencil->GetDesc(&desc);
2371
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002372 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002373 stencilUnmasked = (0x1 << stencilSize) - 1;
2374
2375 if (stencilUnmasked != 0x0)
2376 {
2377 flags |= D3DCLEAR_STENCIL;
2378 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379 }
2380 }
2381
2382 if (mask != 0)
2383 {
2384 return error(GL_INVALID_VALUE);
2385 }
2386
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002387 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2388 {
2389 return;
2390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002392 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002393 unorm<8>(mState.colorClearValue.red),
2394 unorm<8>(mState.colorClearValue.green),
2395 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002396 float depth = clamp01(mState.depthClearValue);
2397 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398
2399 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2400
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002401 if (!renderTarget)
2402 {
2403 return; // Context must be lost, return silently
2404 }
2405
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 D3DSURFACE_DESC desc;
2407 renderTarget->GetDesc(&desc);
2408
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002409 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410
2411 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002412 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002414 !(mState.colorMaskRed && mState.colorMaskGreen &&
2415 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416
2417 if (needMaskedColorClear || needMaskedStencilClear)
2418 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002419 // State which is altered in all paths from this point to the clear call is saved.
2420 // State which is altered in only some paths will be flagged dirty in the case that
2421 // that path is taken.
2422 HRESULT hr;
2423 if (mMaskedClearSavedState == NULL)
2424 {
2425 hr = device->BeginStateBlock();
2426 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2427
2428 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2429 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2430 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2431 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2432 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2433 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2434 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2435 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2436 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2437 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2438 device->SetPixelShader(NULL);
2439 device->SetVertexShader(NULL);
2440 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002441 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2442 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2443 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2444 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2445 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2446 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2447 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002448
2449 hr = device->EndStateBlock(&mMaskedClearSavedState);
2450 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2451 }
2452
2453 ASSERT(mMaskedClearSavedState != NULL);
2454
2455 if (mMaskedClearSavedState != NULL)
2456 {
2457 hr = mMaskedClearSavedState->Capture();
2458 ASSERT(SUCCEEDED(hr));
2459 }
2460
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2462 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2463 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2464 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2465 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2466 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2467 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2468 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2469
2470 if (flags & D3DCLEAR_TARGET)
2471 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002472 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 }
2474 else
2475 {
2476 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2477 }
2478
2479 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2480 {
2481 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2482 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2483 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2484 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002485 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002486 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2488 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002489 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490 }
2491 else
2492 {
2493 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2494 }
2495
2496 device->SetPixelShader(NULL);
2497 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002498 device->SetFVF(D3DFVF_XYZRHW);
2499 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2500 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2501 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2502 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2503 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2504 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2505 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002507 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2508 quad[0][0] = -0.5f;
2509 quad[0][1] = desc.Height - 0.5f;
2510 quad[0][2] = 0.0f;
2511 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002513 quad[1][0] = desc.Width - 0.5f;
2514 quad[1][1] = desc.Height - 0.5f;
2515 quad[1][2] = 0.0f;
2516 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002517
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002518 quad[2][0] = -0.5f;
2519 quad[2][1] = -0.5f;
2520 quad[2][2] = 0.0f;
2521 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002522
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002523 quad[3][0] = desc.Width - 0.5f;
2524 quad[3][1] = -0.5f;
2525 quad[3][2] = 0.0f;
2526 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002528 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002529 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530
2531 if (flags & D3DCLEAR_ZBUFFER)
2532 {
2533 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2534 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2535 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2536 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002537
2538 if (mMaskedClearSavedState != NULL)
2539 {
2540 mMaskedClearSavedState->Apply();
2541 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002543 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 {
2545 device->Clear(0, NULL, flags, color, depth, stencil);
2546 }
2547}
2548
2549void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2550{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002551 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552 {
2553 return error(GL_INVALID_OPERATION);
2554 }
2555
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002556 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557 IDirect3DDevice9 *device = getDevice();
2558 D3DPRIMITIVETYPE primitiveType;
2559 int primitiveCount;
2560
2561 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2562 return error(GL_INVALID_ENUM);
2563
2564 if (primitiveCount <= 0)
2565 {
2566 return;
2567 }
2568
2569 if (!applyRenderTarget(false))
2570 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002571 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002572 }
2573
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002574 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002575
daniel@transgaming.com83921382011-01-08 05:46:00 +00002576 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002577 if (err != GL_NO_ERROR)
2578 {
2579 return error(err);
2580 }
2581
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002582 applyShaders();
2583 applyTextures();
2584
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002585 if (!getCurrentProgram()->validateSamplers())
2586 {
2587 return error(GL_INVALID_OPERATION);
2588 }
2589
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002590 if (!cullSkipsDraw(mode))
2591 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002592 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002593
2594 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002595
2596 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2597 {
2598 drawClosingLine(first, first + count - 1);
2599 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002600 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002601}
2602
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002603void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002605 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002606 {
2607 return error(GL_INVALID_OPERATION);
2608 }
2609
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002610 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611 {
2612 return error(GL_INVALID_OPERATION);
2613 }
2614
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002615 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 IDirect3DDevice9 *device = getDevice();
2617 D3DPRIMITIVETYPE primitiveType;
2618 int primitiveCount;
2619
2620 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2621 return error(GL_INVALID_ENUM);
2622
2623 if (primitiveCount <= 0)
2624 {
2625 return;
2626 }
2627
2628 if (!applyRenderTarget(false))
2629 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002630 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631 }
2632
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002633 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002634
2635 TranslatedIndexData indexInfo;
2636 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2637 if (err != GL_NO_ERROR)
2638 {
2639 return error(err);
2640 }
2641
daniel@transgaming.com83921382011-01-08 05:46:00 +00002642 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2643 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002644 if (err != GL_NO_ERROR)
2645 {
2646 return error(err);
2647 }
2648
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649 applyShaders();
2650 applyTextures();
2651
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002652 if (!getCurrentProgram()->validateSamplers())
2653 {
2654 return error(GL_INVALID_OPERATION);
2655 }
2656
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002657 if (!cullSkipsDraw(mode))
2658 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002659 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002660
daniel@transgaming.com83921382011-01-08 05:46:00 +00002661 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002662
2663 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2664 {
2665 drawClosingLine(count, type, indices);
2666 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002667 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002668}
2669
2670void Context::finish()
2671{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002672 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673 IDirect3DDevice9 *device = getDevice();
2674 IDirect3DQuery9 *occlusionQuery = NULL;
2675
2676 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2677
2678 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2679 {
2680 return error(GL_OUT_OF_MEMORY);
2681 }
2682
2683 ASSERT(SUCCEEDED(result));
2684
2685 if (occlusionQuery)
2686 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002687 IDirect3DStateBlock9 *savedState = NULL;
2688 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2689
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002690 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2691 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692
2693 // Render something outside the render target
2694 device->SetPixelShader(NULL);
2695 device->SetVertexShader(NULL);
2696 device->SetFVF(D3DFVF_XYZRHW);
2697 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002698 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002701 result = occlusionQuery->Issue(D3DISSUE_END);
2702 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703
2704 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2705 {
2706 // Keep polling, but allow other threads to do something useful first
2707 Sleep(0);
2708 }
2709
2710 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002711
2712 if (savedState)
2713 {
2714 savedState->Apply();
2715 savedState->Release();
2716 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002717 }
2718}
2719
2720void Context::flush()
2721{
2722 IDirect3DDevice9 *device = getDevice();
2723 IDirect3DQuery9 *eventQuery = NULL;
2724
2725 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2726
2727 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2728 {
2729 return error(GL_OUT_OF_MEMORY);
2730 }
2731
2732 ASSERT(SUCCEEDED(result));
2733
2734 if (eventQuery)
2735 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002736 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2737 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002738
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002739 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002741
2742 if (result == D3DERR_DEVICELOST)
2743 {
2744 error(GL_OUT_OF_MEMORY);
2745 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002746 }
2747}
2748
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002749void Context::drawClosingLine(unsigned int first, unsigned int last)
2750{
2751 IDirect3DDevice9 *device = getDevice();
2752 IDirect3DIndexBuffer9 *indexBuffer = NULL;
2753 HRESULT result = D3DERR_INVALIDCALL;
2754
2755 if (supports32bitIndices())
2756 {
2757 result = device->CreateIndexBuffer(8, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &indexBuffer, 0);
2758
2759 if (SUCCEEDED(result))
2760 {
2761 unsigned int *data;
2762 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2763
2764 if (SUCCEEDED(result))
2765 {
2766 data[0] = last;
2767 data[1] = first;
2768 }
2769 }
2770 }
2771 else
2772 {
2773 result = device->CreateIndexBuffer(4, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexBuffer, 0);
2774
2775 if (SUCCEEDED(result))
2776 {
2777 unsigned short *data;
2778 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2779
2780 if (SUCCEEDED(result))
2781 {
2782 data[0] = last;
2783 data[1] = first;
2784 }
2785 }
2786 }
2787
2788 if (SUCCEEDED(result))
2789 {
2790 indexBuffer->Unlock();
2791 device->SetIndices(indexBuffer);
2792
2793 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, 0, 1);
2794
2795 indexBuffer->Release();
2796 }
2797 else
2798 {
2799 ERR("Could not create an index buffer for closing a line loop.");
2800 error(GL_OUT_OF_MEMORY);
2801 }
2802}
2803
2804void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2805{
2806 unsigned int first = 0;
2807 unsigned int last = 0;
2808
2809 if (mState.elementArrayBuffer.get())
2810 {
2811 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2812 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2813 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2814 }
2815
2816 switch (type)
2817 {
2818 case GL_UNSIGNED_BYTE:
2819 first = static_cast<const GLubyte*>(indices)[0];
2820 last = static_cast<const GLubyte*>(indices)[count - 1];
2821 break;
2822 case GL_UNSIGNED_SHORT:
2823 first = static_cast<const GLushort*>(indices)[0];
2824 last = static_cast<const GLushort*>(indices)[count - 1];
2825 break;
2826 case GL_UNSIGNED_INT:
2827 first = static_cast<const GLuint*>(indices)[0];
2828 last = static_cast<const GLuint*>(indices)[count - 1];
2829 break;
2830 default: UNREACHABLE();
2831 }
2832
2833 drawClosingLine(first, last);
2834}
2835
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002836void Context::recordInvalidEnum()
2837{
2838 mInvalidEnum = true;
2839}
2840
2841void Context::recordInvalidValue()
2842{
2843 mInvalidValue = true;
2844}
2845
2846void Context::recordInvalidOperation()
2847{
2848 mInvalidOperation = true;
2849}
2850
2851void Context::recordOutOfMemory()
2852{
2853 mOutOfMemory = true;
2854}
2855
2856void Context::recordInvalidFramebufferOperation()
2857{
2858 mInvalidFramebufferOperation = true;
2859}
2860
2861// Get one of the recorded errors and clear its flag, if any.
2862// [OpenGL ES 2.0.24] section 2.5 page 13.
2863GLenum Context::getError()
2864{
2865 if (mInvalidEnum)
2866 {
2867 mInvalidEnum = false;
2868
2869 return GL_INVALID_ENUM;
2870 }
2871
2872 if (mInvalidValue)
2873 {
2874 mInvalidValue = false;
2875
2876 return GL_INVALID_VALUE;
2877 }
2878
2879 if (mInvalidOperation)
2880 {
2881 mInvalidOperation = false;
2882
2883 return GL_INVALID_OPERATION;
2884 }
2885
2886 if (mOutOfMemory)
2887 {
2888 mOutOfMemory = false;
2889
2890 return GL_OUT_OF_MEMORY;
2891 }
2892
2893 if (mInvalidFramebufferOperation)
2894 {
2895 mInvalidFramebufferOperation = false;
2896
2897 return GL_INVALID_FRAMEBUFFER_OPERATION;
2898 }
2899
2900 return GL_NO_ERROR;
2901}
2902
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002903bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002904{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002905 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002906}
2907
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002908int Context::getMaximumVaryingVectors() const
2909{
2910 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2911}
2912
daniel@transgaming.com458da142010-11-28 02:03:02 +00002913int Context::getMaximumFragmentUniformVectors() const
2914{
2915 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2916}
2917
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002918int Context::getMaxSupportedSamples() const
2919{
2920 return mMaxSupportedSamples;
2921}
2922
2923int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2924{
2925 if (requested == 0)
2926 {
2927 return requested;
2928 }
2929
2930 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2931 if (itr == mMultiSampleSupport.end())
2932 {
2933 return -1;
2934 }
2935
2936 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2937 {
2938 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2939 {
2940 return i;
2941 }
2942 }
2943
2944 return -1;
2945}
2946
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002947bool Context::supportsEventQueries() const
2948{
2949 return mSupportsEventQueries;
2950}
2951
daniel@transgaming.com01868132010-08-24 19:21:17 +00002952bool Context::supportsCompressedTextures() const
2953{
2954 return mSupportsCompressedTextures;
2955}
2956
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002957bool Context::supportsFloatTextures() const
2958{
2959 return mSupportsFloatTextures;
2960}
2961
2962bool Context::supportsFloatLinearFilter() const
2963{
2964 return mSupportsFloatLinearFilter;
2965}
2966
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002967bool Context::supportsFloatRenderableTextures() const
2968{
2969 return mSupportsFloatRenderableTextures;
2970}
2971
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002972bool Context::supportsHalfFloatTextures() const
2973{
2974 return mSupportsHalfFloatTextures;
2975}
2976
2977bool Context::supportsHalfFloatLinearFilter() const
2978{
2979 return mSupportsHalfFloatLinearFilter;
2980}
2981
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002982bool Context::supportsHalfFloatRenderableTextures() const
2983{
2984 return mSupportsHalfFloatRenderableTextures;
2985}
2986
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00002987int Context::getMaximumRenderbufferDimension() const
2988{
2989 return mMaxRenderbufferDimension;
2990}
2991
2992int Context::getMaximumTextureDimension() const
2993{
2994 return mMaxTextureDimension;
2995}
2996
2997int Context::getMaximumCubeTextureDimension() const
2998{
2999 return mMaxCubeTextureDimension;
3000}
3001
3002int Context::getMaximumTextureLevel() const
3003{
3004 return mMaxTextureLevel;
3005}
3006
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003007bool Context::supportsLuminanceTextures() const
3008{
3009 return mSupportsLuminanceTextures;
3010}
3011
3012bool Context::supportsLuminanceAlphaTextures() const
3013{
3014 return mSupportsLuminanceAlphaTextures;
3015}
3016
daniel@transgaming.com83921382011-01-08 05:46:00 +00003017bool Context::supports32bitIndices() const
3018{
3019 return mSupports32bitIndices;
3020}
3021
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003022void Context::detachBuffer(GLuint buffer)
3023{
3024 // [OpenGL ES 2.0.24] section 2.9 page 22:
3025 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3026 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3027
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003028 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003029 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003030 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003031 }
3032
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003033 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003034 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003035 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003036 }
3037
3038 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3039 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003040 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003041 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003042 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003043 }
3044 }
3045}
3046
3047void Context::detachTexture(GLuint texture)
3048{
3049 // [OpenGL ES 2.0.24] section 3.8 page 84:
3050 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3051 // rebound to texture object zero
3052
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003053 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003054 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003055 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003056 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003057 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003058 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003059 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003060 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003061 }
3062 }
3063
3064 // [OpenGL ES 2.0.24] section 4.4 page 112:
3065 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3066 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3067 // image was attached in the currently bound framebuffer.
3068
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003069 Framebuffer *readFramebuffer = getReadFramebuffer();
3070 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003071
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003072 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003073 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003074 readFramebuffer->detachTexture(texture);
3075 }
3076
3077 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3078 {
3079 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003080 }
3081}
3082
3083void Context::detachFramebuffer(GLuint framebuffer)
3084{
3085 // [OpenGL ES 2.0.24] section 4.4 page 107:
3086 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3087 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3088
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003089 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003090 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003091 bindReadFramebuffer(0);
3092 }
3093
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003094 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003095 {
3096 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003097 }
3098}
3099
3100void Context::detachRenderbuffer(GLuint renderbuffer)
3101{
3102 // [OpenGL ES 2.0.24] section 4.4 page 109:
3103 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3104 // had been executed with the target RENDERBUFFER and name of zero.
3105
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003106 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003107 {
3108 bindRenderbuffer(0);
3109 }
3110
3111 // [OpenGL ES 2.0.24] section 4.4 page 111:
3112 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3113 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3114 // point to which this image was attached in the currently bound framebuffer.
3115
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003116 Framebuffer *readFramebuffer = getReadFramebuffer();
3117 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003118
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003119 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003120 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003121 readFramebuffer->detachRenderbuffer(renderbuffer);
3122 }
3123
3124 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3125 {
3126 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003127 }
3128}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003129
3130Texture *Context::getIncompleteTexture(SamplerType type)
3131{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003132 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003133
3134 if (t == NULL)
3135 {
3136 static const GLubyte color[] = { 0, 0, 0, 255 };
3137
3138 switch (type)
3139 {
3140 default:
3141 UNREACHABLE();
3142 // default falls through to SAMPLER_2D
3143
3144 case SAMPLER_2D:
3145 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003146 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003147 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003148 t = incomplete2d;
3149 }
3150 break;
3151
3152 case SAMPLER_CUBE:
3153 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003154 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003155
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003156 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3157 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3158 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3159 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3160 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3161 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003162
3163 t = incompleteCube;
3164 }
3165 break;
3166 }
3167
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003168 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003169 }
3170
3171 return t;
3172}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003173
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003174bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003175{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003176 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003177}
3178
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003179bool Context::isTriangleMode(GLenum drawMode)
3180{
3181 switch (drawMode)
3182 {
3183 case GL_TRIANGLES:
3184 case GL_TRIANGLE_FAN:
3185 case GL_TRIANGLE_STRIP:
3186 return true;
3187 case GL_POINTS:
3188 case GL_LINES:
3189 case GL_LINE_LOOP:
3190 case GL_LINE_STRIP:
3191 return false;
3192 default: UNREACHABLE();
3193 }
3194
3195 return false;
3196}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003197
3198void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3199{
3200 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3201
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003202 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3203 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3204 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3205 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003206
daniel@transgaming.com83921382011-01-08 05:46:00 +00003207 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003208}
3209
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003210void Context::initExtensionString()
3211{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003212 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003213 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3214 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003215 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003216 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003217 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003218
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003219 if (supportsEventQueries())
3220 {
3221 mExtensionString += "GL_NV_fence ";
3222 }
3223
daniel@transgaming.com01868132010-08-24 19:21:17 +00003224 if (supportsCompressedTextures())
3225 {
3226 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3227 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003228
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003229 if (supportsFloatTextures())
3230 {
3231 mExtensionString += "GL_OES_texture_float ";
3232 }
3233
3234 if (supportsHalfFloatTextures())
3235 {
3236 mExtensionString += "GL_OES_texture_half_float ";
3237 }
3238
3239 if (supportsFloatLinearFilter())
3240 {
3241 mExtensionString += "GL_OES_texture_float_linear ";
3242 }
3243
3244 if (supportsHalfFloatLinearFilter())
3245 {
3246 mExtensionString += "GL_OES_texture_half_float_linear ";
3247 }
3248
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003249 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003250 {
3251 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3252 }
3253
daniel@transgaming.com83921382011-01-08 05:46:00 +00003254 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003255 {
3256 mExtensionString += "GL_OES_element_index_uint ";
3257 }
3258
3259 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3260 if (end != std::string::npos)
3261 {
3262 mExtensionString.resize(end+1);
3263 }
3264}
3265
3266const char *Context::getExtensionString() const
3267{
3268 return mExtensionString.c_str();
3269}
3270
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003271void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3272 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3273 GLbitfield mask)
3274{
3275 IDirect3DDevice9 *device = getDevice();
3276
3277 Framebuffer *readFramebuffer = getReadFramebuffer();
3278 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3279
3280 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3281 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3282 {
3283 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3284 }
3285
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003286 if (drawFramebuffer->getSamples() != 0)
3287 {
3288 return error(GL_INVALID_OPERATION);
3289 }
3290
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003291 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3292 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3293 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3294 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3295
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003296 RECT sourceRect;
3297 RECT destRect;
3298
3299 if (srcX0 < srcX1)
3300 {
3301 sourceRect.left = srcX0;
3302 sourceRect.right = srcX1;
3303 destRect.left = dstX0;
3304 destRect.right = dstX1;
3305 }
3306 else
3307 {
3308 sourceRect.left = srcX1;
3309 destRect.left = dstX1;
3310 sourceRect.right = srcX0;
3311 destRect.right = dstX0;
3312 }
3313
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003314 if (srcY0 < srcY1)
3315 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003316 sourceRect.top = readBufferHeight - srcY1;
3317 destRect.top = drawBufferHeight - dstY1;
3318 sourceRect.bottom = readBufferHeight - srcY0;
3319 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003320 }
3321 else
3322 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003323 sourceRect.top = readBufferHeight - srcY0;
3324 destRect.top = drawBufferHeight - dstY0;
3325 sourceRect.bottom = readBufferHeight - srcY1;
3326 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003327 }
3328
3329 RECT sourceScissoredRect = sourceRect;
3330 RECT destScissoredRect = destRect;
3331
3332 if (mState.scissorTest)
3333 {
3334 // Only write to parts of the destination framebuffer which pass the scissor test
3335 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3336 // rect will be checked against scissorY, rather than the bottom.
3337 if (destRect.left < mState.scissorX)
3338 {
3339 int xDiff = mState.scissorX - destRect.left;
3340 destScissoredRect.left = mState.scissorX;
3341 sourceScissoredRect.left += xDiff;
3342 }
3343
3344 if (destRect.right > mState.scissorX + mState.scissorWidth)
3345 {
3346 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3347 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3348 sourceScissoredRect.right -= xDiff;
3349 }
3350
3351 if (destRect.top < mState.scissorY)
3352 {
3353 int yDiff = mState.scissorY - destRect.top;
3354 destScissoredRect.top = mState.scissorY;
3355 sourceScissoredRect.top += yDiff;
3356 }
3357
3358 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3359 {
3360 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3361 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3362 sourceScissoredRect.bottom -= yDiff;
3363 }
3364 }
3365
3366 bool blitRenderTarget = false;
3367 bool blitDepthStencil = false;
3368
3369 RECT sourceTrimmedRect = sourceScissoredRect;
3370 RECT destTrimmedRect = destScissoredRect;
3371
3372 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3373 // the actual draw and read surfaces.
3374 if (sourceTrimmedRect.left < 0)
3375 {
3376 int xDiff = 0 - sourceTrimmedRect.left;
3377 sourceTrimmedRect.left = 0;
3378 destTrimmedRect.left += xDiff;
3379 }
3380
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003381 if (sourceTrimmedRect.right > readBufferWidth)
3382 {
3383 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3384 sourceTrimmedRect.right = readBufferWidth;
3385 destTrimmedRect.right -= xDiff;
3386 }
3387
3388 if (sourceTrimmedRect.top < 0)
3389 {
3390 int yDiff = 0 - sourceTrimmedRect.top;
3391 sourceTrimmedRect.top = 0;
3392 destTrimmedRect.top += yDiff;
3393 }
3394
3395 if (sourceTrimmedRect.bottom > readBufferHeight)
3396 {
3397 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3398 sourceTrimmedRect.bottom = readBufferHeight;
3399 destTrimmedRect.bottom -= yDiff;
3400 }
3401
3402 if (destTrimmedRect.left < 0)
3403 {
3404 int xDiff = 0 - destTrimmedRect.left;
3405 destTrimmedRect.left = 0;
3406 sourceTrimmedRect.left += xDiff;
3407 }
3408
3409 if (destTrimmedRect.right > drawBufferWidth)
3410 {
3411 int xDiff = destTrimmedRect.right - drawBufferWidth;
3412 destTrimmedRect.right = drawBufferWidth;
3413 sourceTrimmedRect.right -= xDiff;
3414 }
3415
3416 if (destTrimmedRect.top < 0)
3417 {
3418 int yDiff = 0 - destTrimmedRect.top;
3419 destTrimmedRect.top = 0;
3420 sourceTrimmedRect.top += yDiff;
3421 }
3422
3423 if (destTrimmedRect.bottom > drawBufferHeight)
3424 {
3425 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3426 destTrimmedRect.bottom = drawBufferHeight;
3427 sourceTrimmedRect.bottom -= yDiff;
3428 }
3429
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003430 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003431 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3432 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3433 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3434 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003435 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3436 {
3437 partialBufferCopy = true;
3438 }
3439
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003440 if (mask & GL_COLOR_BUFFER_BIT)
3441 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003442 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3443 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3444 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3445 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3446 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003447 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3448 {
3449 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3450 return error(GL_INVALID_OPERATION);
3451 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003452
3453 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3454 {
3455 return error(GL_INVALID_OPERATION);
3456 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003457
3458 blitRenderTarget = true;
3459
3460 }
3461
3462 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3463 {
3464 DepthStencilbuffer *readDSBuffer = NULL;
3465 DepthStencilbuffer *drawDSBuffer = NULL;
3466
3467 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3468 // both a depth and stencil buffer, it will be the same buffer.
3469
3470 if (mask & GL_DEPTH_BUFFER_BIT)
3471 {
3472 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3473 {
3474 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3475 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3476 {
3477 return error(GL_INVALID_OPERATION);
3478 }
3479
3480 blitDepthStencil = true;
3481 readDSBuffer = readFramebuffer->getDepthbuffer();
3482 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3483 }
3484 }
3485
3486 if (mask & GL_STENCIL_BUFFER_BIT)
3487 {
3488 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3489 {
3490 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3491 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3492 {
3493 return error(GL_INVALID_OPERATION);
3494 }
3495
3496 blitDepthStencil = true;
3497 readDSBuffer = readFramebuffer->getStencilbuffer();
3498 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3499 }
3500 }
3501
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003502 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003503 {
3504 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3505 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3506 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003507
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003508 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3509 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003510 {
3511 return error(GL_INVALID_OPERATION);
3512 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003513 }
3514
3515 if (blitRenderTarget || blitDepthStencil)
3516 {
3517 egl::Display *display = getDisplay();
3518 display->endScene();
3519
3520 if (blitRenderTarget)
3521 {
3522 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3523 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3524
3525 if (FAILED(result))
3526 {
3527 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3528 return;
3529 }
3530 }
3531
3532 if (blitDepthStencil)
3533 {
3534 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3535
3536 if (FAILED(result))
3537 {
3538 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3539 return;
3540 }
3541 }
3542 }
3543}
3544
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003545VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3546{
3547 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3548 {
3549 mVertexDeclCache[i].vertexDeclaration = NULL;
3550 mVertexDeclCache[i].lruCount = 0;
3551 }
3552}
3553
3554VertexDeclarationCache::~VertexDeclarationCache()
3555{
3556 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3557 {
3558 if (mVertexDeclCache[i].vertexDeclaration)
3559 {
3560 mVertexDeclCache[i].vertexDeclaration->Release();
3561 }
3562 }
3563}
3564
3565GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3566{
3567 IDirect3DDevice9 *device = getDevice();
3568
3569 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3570 D3DVERTEXELEMENT9 *element = &elements[0];
3571
3572 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3573 {
3574 if (attributes[i].active)
3575 {
3576 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3577
3578 element->Stream = i;
3579 element->Offset = 0;
3580 element->Type = attributes[i].type;
3581 element->Method = D3DDECLMETHOD_DEFAULT;
3582 element->Usage = D3DDECLUSAGE_TEXCOORD;
3583 element->UsageIndex = program->getSemanticIndex(i);
3584 element++;
3585 }
3586 }
3587
3588 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3589 *(element++) = end;
3590
3591 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3592 {
3593 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3594 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3595 {
3596 entry->lruCount = ++mMaxLru;
3597 device->SetVertexDeclaration(entry->vertexDeclaration);
3598
3599 return GL_NO_ERROR;
3600 }
3601 }
3602
3603 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3604
3605 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3606 {
3607 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3608 {
3609 lastCache = &mVertexDeclCache[i];
3610 }
3611 }
3612
3613 if (lastCache->vertexDeclaration != NULL)
3614 {
3615 lastCache->vertexDeclaration->Release();
3616 lastCache->vertexDeclaration = NULL;
3617 }
3618
3619 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3620 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3621 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3622 lastCache->lruCount = ++mMaxLru;
3623
3624 return GL_NO_ERROR;
3625}
3626
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003627}
3628
3629extern "C"
3630{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003631gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003632{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003633 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003634}
3635
3636void glDestroyContext(gl::Context *context)
3637{
3638 delete context;
3639
3640 if (context == gl::getContext())
3641 {
3642 gl::makeCurrent(NULL, NULL, NULL);
3643 }
3644}
3645
3646void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3647{
3648 gl::makeCurrent(context, display, surface);
3649}
3650
3651gl::Context *glGetCurrentContext()
3652{
3653 return gl::getContext();
3654}
3655}