blob: 5f77c82bb59c27ed0ee0660f822ceaf1fe86532b [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.com0d25b002010-07-28 19:21:07 +000036Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000037 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038{
39 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000040
daniel@transgaming.com428d1582010-05-04 03:35:25 +000041 mState.depthClearValue = 1.0f;
42 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043
daniel@transgaming.com428d1582010-05-04 03:35:25 +000044 mState.cullFace = false;
45 mState.cullMode = GL_BACK;
46 mState.frontFace = GL_CCW;
47 mState.depthTest = false;
48 mState.depthFunc = GL_LESS;
49 mState.blend = false;
50 mState.sourceBlendRGB = GL_ONE;
51 mState.sourceBlendAlpha = GL_ONE;
52 mState.destBlendRGB = GL_ZERO;
53 mState.destBlendAlpha = GL_ZERO;
54 mState.blendEquationRGB = GL_FUNC_ADD;
55 mState.blendEquationAlpha = GL_FUNC_ADD;
56 mState.blendColor.red = 0;
57 mState.blendColor.green = 0;
58 mState.blendColor.blue = 0;
59 mState.blendColor.alpha = 0;
60 mState.stencilTest = false;
61 mState.stencilFunc = GL_ALWAYS;
62 mState.stencilRef = 0;
63 mState.stencilMask = -1;
64 mState.stencilWritemask = -1;
65 mState.stencilBackFunc = GL_ALWAYS;
66 mState.stencilBackRef = 0;
67 mState.stencilBackMask = - 1;
68 mState.stencilBackWritemask = -1;
69 mState.stencilFail = GL_KEEP;
70 mState.stencilPassDepthFail = GL_KEEP;
71 mState.stencilPassDepthPass = GL_KEEP;
72 mState.stencilBackFail = GL_KEEP;
73 mState.stencilBackPassDepthFail = GL_KEEP;
74 mState.stencilBackPassDepthPass = GL_KEEP;
75 mState.polygonOffsetFill = false;
76 mState.polygonOffsetFactor = 0.0f;
77 mState.polygonOffsetUnits = 0.0f;
78 mState.sampleAlphaToCoverage = false;
79 mState.sampleCoverage = false;
80 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000081 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000082 mState.scissorTest = false;
83 mState.dither = true;
84 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000085 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086
daniel@transgaming.com428d1582010-05-04 03:35:25 +000087 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.viewportX = 0;
90 mState.viewportY = 0;
91 mState.viewportWidth = config->mDisplayMode.Width;
92 mState.viewportHeight = config->mDisplayMode.Height;
93 mState.zNear = 0.0f;
94 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.scissorX = 0;
97 mState.scissorY = 0;
98 mState.scissorWidth = config->mDisplayMode.Width;
99 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000101 mState.colorMaskRed = true;
102 mState.colorMaskGreen = true;
103 mState.colorMaskBlue = true;
104 mState.colorMaskAlpha = true;
105 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000107 if (shareContext != NULL)
108 {
109 mResourceManager = shareContext->mResourceManager;
110 mResourceManager->addRef();
111 }
112 else
113 {
114 mResourceManager = new ResourceManager();
115 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000116
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117 // [OpenGL ES 2.0.24] section 3.7 page 83:
118 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
119 // and cube map texture state vectors respectively associated with them.
120 // In order that access to these initial textures not be lost, they are treated as texture
121 // objects all of whose names are 0.
122
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000123 mTexture2DZero.set(new Texture2D(0));
124 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000126 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000127 bindArrayBuffer(0);
128 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129 bindTextureCubeMap(0);
130 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000131 bindReadFramebuffer(0);
132 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133 bindRenderbuffer(0);
134
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000135 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000137 mState.packAlignment = 4;
138 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000139
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000140 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000141 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000142 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000143
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144 mInvalidEnum = false;
145 mInvalidValue = false;
146 mInvalidOperation = false;
147 mOutOfMemory = false;
148 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000149
150 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000151
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000152 mSupportsCompressedTextures = false;
153 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000154 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000155 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000156 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157}
158
159Context::~Context()
160{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000161 if (mState.currentProgram != 0)
162 {
163 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
164 if (programObject)
165 {
166 programObject->release();
167 }
168 mState.currentProgram = 0;
169 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000171 while (!mFramebufferMap.empty())
172 {
173 deleteFramebuffer(mFramebufferMap.begin()->first);
174 }
175
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000176 while (!mFenceMap.empty())
177 {
178 deleteFence(mFenceMap.begin()->first);
179 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000180
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000181 while (!mMultiSampleSupport.empty())
182 {
183 delete [] mMultiSampleSupport.begin()->second;
184 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
185 }
186
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000187 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
188 {
189 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
190 {
191 mState.samplerTexture[type][sampler].set(NULL);
192 }
193 }
194
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000195 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
196 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000197 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000198 }
199
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000200 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
201 {
202 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
203 }
204
205 mState.arrayBuffer.set(NULL);
206 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000207 mState.renderbuffer.set(NULL);
208
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000209 mTexture2DZero.set(NULL);
210 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000212 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000213 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000214 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000215
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000216 if (mMaskedClearSavedState)
217 {
218 mMaskedClearSavedState->Release();
219 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000220
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000221 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222}
223
224void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
225{
226 IDirect3DDevice9 *device = display->getDevice();
227
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000228 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000229 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000230 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000231
daniel@transgaming.com83921382011-01-08 05:46:00 +0000232 mVertexDataManager = new VertexDataManager(this, device);
233 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000234 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000235
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000236 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
237
238 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
239 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
240 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
241 mMaxRenderbufferDimension = mMaxTextureDimension;
242 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
243 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
244 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
245
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000246 const D3DFORMAT renderBufferFormats[] =
247 {
248 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000249 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000250 D3DFMT_R5G6B5,
251 D3DFMT_D24S8
252 };
253
254 int max = 0;
255 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
256 {
257 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
258 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
259 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
260
261 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
262 {
263 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
264 {
265 max = j;
266 }
267 }
268 }
269
270 mMaxSupportedSamples = max;
271
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000272 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000273 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000274 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
275 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000276 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
277 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000278
daniel@transgaming.com83921382011-01-08 05:46:00 +0000279 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
280
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000281 initExtensionString();
282
283 mState.viewportX = 0;
284 mState.viewportY = 0;
285 mState.viewportWidth = surface->getWidth();
286 mState.viewportHeight = surface->getHeight();
287
288 mState.scissorX = 0;
289 mState.scissorY = 0;
290 mState.scissorWidth = surface->getWidth();
291 mState.scissorHeight = surface->getHeight();
292
293 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000294 }
295
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
297 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000298 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000301 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000302 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
304 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000306 if (defaultRenderTarget)
307 {
308 defaultRenderTarget->Release();
309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000311 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000313 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000315
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000316 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317}
318
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000319// 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 +0000320void Context::markAllStateDirty()
321{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000322 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
323 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000324 mAppliedTextureSerial[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000325 }
326
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000327 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000328 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000329 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000330 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000331 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000332
333 mClearStateDirty = true;
334 mCullStateDirty = true;
335 mDepthStateDirty = true;
336 mMaskStateDirty = true;
337 mBlendStateDirty = true;
338 mStencilStateDirty = true;
339 mPolygonOffsetStateDirty = true;
340 mScissorStateDirty = true;
341 mSampleStateDirty = true;
342 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000343 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000344}
345
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346void Context::setClearColor(float red, float green, float blue, float alpha)
347{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000348 mState.colorClearValue.red = red;
349 mState.colorClearValue.green = green;
350 mState.colorClearValue.blue = blue;
351 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352}
353
354void Context::setClearDepth(float depth)
355{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000356 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357}
358
359void Context::setClearStencil(int stencil)
360{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000361 mState.stencilClearValue = stencil;
362}
363
364void Context::setCullFace(bool enabled)
365{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000366 if (mState.cullFace != enabled)
367 {
368 mState.cullFace = enabled;
369 mCullStateDirty = true;
370 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000371}
372
373bool Context::isCullFaceEnabled() const
374{
375 return mState.cullFace;
376}
377
378void Context::setCullMode(GLenum mode)
379{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000380 if (mState.cullMode != mode)
381 {
382 mState.cullMode = mode;
383 mCullStateDirty = true;
384 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000385}
386
387void Context::setFrontFace(GLenum front)
388{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000389 if (mState.frontFace != front)
390 {
391 mState.frontFace = front;
392 mFrontFaceDirty = true;
393 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000394}
395
396void Context::setDepthTest(bool enabled)
397{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000398 if (mState.depthTest != enabled)
399 {
400 mState.depthTest = enabled;
401 mDepthStateDirty = true;
402 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000403}
404
405bool Context::isDepthTestEnabled() const
406{
407 return mState.depthTest;
408}
409
410void Context::setDepthFunc(GLenum depthFunc)
411{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000412 if (mState.depthFunc != depthFunc)
413 {
414 mState.depthFunc = depthFunc;
415 mDepthStateDirty = true;
416 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000417}
418
419void Context::setDepthRange(float zNear, float zFar)
420{
421 mState.zNear = zNear;
422 mState.zFar = zFar;
423}
424
425void Context::setBlend(bool enabled)
426{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000427 if (mState.blend != enabled)
428 {
429 mState.blend = enabled;
430 mBlendStateDirty = true;
431 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000432}
433
434bool Context::isBlendEnabled() const
435{
436 return mState.blend;
437}
438
439void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
440{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000441 if (mState.sourceBlendRGB != sourceRGB ||
442 mState.sourceBlendAlpha != sourceAlpha ||
443 mState.destBlendRGB != destRGB ||
444 mState.destBlendAlpha != destAlpha)
445 {
446 mState.sourceBlendRGB = sourceRGB;
447 mState.destBlendRGB = destRGB;
448 mState.sourceBlendAlpha = sourceAlpha;
449 mState.destBlendAlpha = destAlpha;
450 mBlendStateDirty = true;
451 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000452}
453
454void Context::setBlendColor(float red, float green, float blue, float alpha)
455{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000456 if (mState.blendColor.red != red ||
457 mState.blendColor.green != green ||
458 mState.blendColor.blue != blue ||
459 mState.blendColor.alpha != alpha)
460 {
461 mState.blendColor.red = red;
462 mState.blendColor.green = green;
463 mState.blendColor.blue = blue;
464 mState.blendColor.alpha = alpha;
465 mBlendStateDirty = true;
466 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000467}
468
469void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
470{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000471 if (mState.blendEquationRGB != rgbEquation ||
472 mState.blendEquationAlpha != alphaEquation)
473 {
474 mState.blendEquationRGB = rgbEquation;
475 mState.blendEquationAlpha = alphaEquation;
476 mBlendStateDirty = true;
477 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000478}
479
480void Context::setStencilTest(bool enabled)
481{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000482 if (mState.stencilTest != enabled)
483 {
484 mState.stencilTest = enabled;
485 mStencilStateDirty = true;
486 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000487}
488
489bool Context::isStencilTestEnabled() const
490{
491 return mState.stencilTest;
492}
493
494void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
495{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000496 if (mState.stencilFunc != stencilFunc ||
497 mState.stencilRef != stencilRef ||
498 mState.stencilMask != stencilMask)
499 {
500 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000501 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000502 mState.stencilMask = stencilMask;
503 mStencilStateDirty = true;
504 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000505}
506
507void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
508{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000509 if (mState.stencilBackFunc != stencilBackFunc ||
510 mState.stencilBackRef != stencilBackRef ||
511 mState.stencilBackMask != stencilBackMask)
512 {
513 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000514 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000515 mState.stencilBackMask = stencilBackMask;
516 mStencilStateDirty = true;
517 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000518}
519
520void Context::setStencilWritemask(GLuint stencilWritemask)
521{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000522 if (mState.stencilWritemask != stencilWritemask)
523 {
524 mState.stencilWritemask = stencilWritemask;
525 mStencilStateDirty = true;
526 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000527}
528
529void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
530{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000531 if (mState.stencilBackWritemask != stencilBackWritemask)
532 {
533 mState.stencilBackWritemask = stencilBackWritemask;
534 mStencilStateDirty = true;
535 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000536}
537
538void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
539{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000540 if (mState.stencilFail != stencilFail ||
541 mState.stencilPassDepthFail != stencilPassDepthFail ||
542 mState.stencilPassDepthPass != stencilPassDepthPass)
543 {
544 mState.stencilFail = stencilFail;
545 mState.stencilPassDepthFail = stencilPassDepthFail;
546 mState.stencilPassDepthPass = stencilPassDepthPass;
547 mStencilStateDirty = true;
548 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000549}
550
551void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
552{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000553 if (mState.stencilBackFail != stencilBackFail ||
554 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
555 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
556 {
557 mState.stencilBackFail = stencilBackFail;
558 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
559 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
560 mStencilStateDirty = true;
561 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000562}
563
564void Context::setPolygonOffsetFill(bool enabled)
565{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000566 if (mState.polygonOffsetFill != enabled)
567 {
568 mState.polygonOffsetFill = enabled;
569 mPolygonOffsetStateDirty = true;
570 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000571}
572
573bool Context::isPolygonOffsetFillEnabled() const
574{
575 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000577}
578
579void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
580{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000581 if (mState.polygonOffsetFactor != factor ||
582 mState.polygonOffsetUnits != units)
583 {
584 mState.polygonOffsetFactor = factor;
585 mState.polygonOffsetUnits = units;
586 mPolygonOffsetStateDirty = true;
587 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000588}
589
590void Context::setSampleAlphaToCoverage(bool enabled)
591{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000592 if (mState.sampleAlphaToCoverage != enabled)
593 {
594 mState.sampleAlphaToCoverage = enabled;
595 mSampleStateDirty = true;
596 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000597}
598
599bool Context::isSampleAlphaToCoverageEnabled() const
600{
601 return mState.sampleAlphaToCoverage;
602}
603
604void Context::setSampleCoverage(bool enabled)
605{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000606 if (mState.sampleCoverage != enabled)
607 {
608 mState.sampleCoverage = enabled;
609 mSampleStateDirty = true;
610 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000611}
612
613bool Context::isSampleCoverageEnabled() const
614{
615 return mState.sampleCoverage;
616}
617
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000618void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000619{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000620 if (mState.sampleCoverageValue != value ||
621 mState.sampleCoverageInvert != invert)
622 {
623 mState.sampleCoverageValue = value;
624 mState.sampleCoverageInvert = invert;
625 mSampleStateDirty = true;
626 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000627}
628
629void Context::setScissorTest(bool enabled)
630{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000631 if (mState.scissorTest != enabled)
632 {
633 mState.scissorTest = enabled;
634 mScissorStateDirty = true;
635 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000636}
637
638bool Context::isScissorTestEnabled() const
639{
640 return mState.scissorTest;
641}
642
643void Context::setDither(bool enabled)
644{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000645 if (mState.dither != enabled)
646 {
647 mState.dither = enabled;
648 mDitherStateDirty = true;
649 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000650}
651
652bool Context::isDitherEnabled() const
653{
654 return mState.dither;
655}
656
657void Context::setLineWidth(GLfloat width)
658{
659 mState.lineWidth = width;
660}
661
662void Context::setGenerateMipmapHint(GLenum hint)
663{
664 mState.generateMipmapHint = hint;
665}
666
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000667void Context::setFragmentShaderDerivativeHint(GLenum hint)
668{
669 mState.fragmentShaderDerivativeHint = hint;
670 // TODO: Propagate the hint to shader translator so we can write
671 // ddx, ddx_coarse, or ddx_fine depending on the hint.
672 // Ignore for now. It is valid for implementations to ignore hint.
673}
674
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000675void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
676{
677 mState.viewportX = x;
678 mState.viewportY = y;
679 mState.viewportWidth = width;
680 mState.viewportHeight = height;
681}
682
683void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
684{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000685 if (mState.scissorX != x || mState.scissorY != y ||
686 mState.scissorWidth != width || mState.scissorHeight != height)
687 {
688 mState.scissorX = x;
689 mState.scissorY = y;
690 mState.scissorWidth = width;
691 mState.scissorHeight = height;
692 mScissorStateDirty = true;
693 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000694}
695
696void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
697{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000698 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
699 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
700 {
701 mState.colorMaskRed = red;
702 mState.colorMaskGreen = green;
703 mState.colorMaskBlue = blue;
704 mState.colorMaskAlpha = alpha;
705 mMaskStateDirty = true;
706 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000707}
708
709void Context::setDepthMask(bool mask)
710{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000711 if (mState.depthMask != mask)
712 {
713 mState.depthMask = mask;
714 mMaskStateDirty = true;
715 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000716}
717
718void Context::setActiveSampler(int active)
719{
720 mState.activeSampler = active;
721}
722
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000723GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000724{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000725 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000726}
727
728GLuint Context::getDrawFramebufferHandle() const
729{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000730 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000731}
732
733GLuint Context::getRenderbufferHandle() const
734{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000735 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000736}
737
738GLuint Context::getArrayBufferHandle() const
739{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000740 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000741}
742
daniel@transgaming.com83921382011-01-08 05:46:00 +0000743void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000744{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000745 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000746}
747
daniel@transgaming.com83921382011-01-08 05:46:00 +0000748const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749{
750 return mState.vertexAttribute[attribNum];
751}
752
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000753void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000754 GLsizei stride, const void *pointer)
755{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000756 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000757 mState.vertexAttribute[attribNum].mSize = size;
758 mState.vertexAttribute[attribNum].mType = type;
759 mState.vertexAttribute[attribNum].mNormalized = normalized;
760 mState.vertexAttribute[attribNum].mStride = stride;
761 mState.vertexAttribute[attribNum].mPointer = pointer;
762}
763
764const void *Context::getVertexAttribPointer(unsigned int attribNum) const
765{
766 return mState.vertexAttribute[attribNum].mPointer;
767}
768
daniel@transgaming.com83921382011-01-08 05:46:00 +0000769const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000770{
771 return mState.vertexAttribute;
772}
773
774void Context::setPackAlignment(GLint alignment)
775{
776 mState.packAlignment = alignment;
777}
778
779GLint Context::getPackAlignment() const
780{
781 return mState.packAlignment;
782}
783
784void Context::setUnpackAlignment(GLint alignment)
785{
786 mState.unpackAlignment = alignment;
787}
788
789GLint Context::getUnpackAlignment() const
790{
791 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792}
793
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794GLuint Context::createBuffer()
795{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000796 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797}
798
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799GLuint Context::createProgram()
800{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000801 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802}
803
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000804GLuint Context::createShader(GLenum type)
805{
806 return mResourceManager->createShader(type);
807}
808
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809GLuint Context::createTexture()
810{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000811 return mResourceManager->createTexture();
812}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000814GLuint Context::createRenderbuffer()
815{
816 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817}
818
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000819// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820GLuint Context::createFramebuffer()
821{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000822 unsigned int handle = 1;
823
824 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
825 {
826 handle++;
827 }
828
829 mFramebufferMap[handle] = NULL;
830
831 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000834GLuint Context::createFence()
835{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000836 unsigned int handle = 0;
837
838 while (mFenceMap.find(handle) != mFenceMap.end())
839 {
840 handle++;
841 }
842
843 mFenceMap[handle] = new Fence;
844
845 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000846}
847
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848void Context::deleteBuffer(GLuint buffer)
849{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000850 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851 {
852 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000854
855 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
858void Context::deleteShader(GLuint shader)
859{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000860 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
863void Context::deleteProgram(GLuint program)
864{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000865 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866}
867
868void Context::deleteTexture(GLuint texture)
869{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000870 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871 {
872 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000874
875 mResourceManager->deleteTexture(texture);
876}
877
878void Context::deleteRenderbuffer(GLuint renderbuffer)
879{
880 if (mResourceManager->getRenderbuffer(renderbuffer))
881 {
882 detachRenderbuffer(renderbuffer);
883 }
884
885 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886}
887
888void Context::deleteFramebuffer(GLuint framebuffer)
889{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000890 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
891
892 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893 {
894 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000895
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000896 delete framebufferObject->second;
897 mFramebufferMap.erase(framebufferObject);
898 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000900
901void Context::deleteFence(GLuint fence)
902{
903 FenceMap::iterator fenceObject = mFenceMap.find(fence);
904
905 if (fenceObject != mFenceMap.end())
906 {
907 delete fenceObject->second;
908 mFenceMap.erase(fenceObject);
909 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000910}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000912Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000914 return mResourceManager->getBuffer(handle);
915}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000917Shader *Context::getShader(GLuint handle)
918{
919 return mResourceManager->getShader(handle);
920}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000922Program *Context::getProgram(GLuint handle)
923{
924 return mResourceManager->getProgram(handle);
925}
926
927Texture *Context::getTexture(GLuint handle)
928{
929 return mResourceManager->getTexture(handle);
930}
931
932Renderbuffer *Context::getRenderbuffer(GLuint handle)
933{
934 return mResourceManager->getRenderbuffer(handle);
935}
936
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000937Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000938{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000939 return getFramebuffer(mState.readFramebuffer);
940}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000941
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000942Framebuffer *Context::getDrawFramebuffer()
943{
944 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945}
946
947void Context::bindArrayBuffer(unsigned int buffer)
948{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000949 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000951 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
954void Context::bindElementArrayBuffer(unsigned int buffer)
955{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000956 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000958 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959}
960
961void Context::bindTexture2D(GLuint texture)
962{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000963 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000965 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
968void Context::bindTextureCubeMap(GLuint texture)
969{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000970 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000972 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000975void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000977 if (!getFramebuffer(framebuffer))
978 {
979 mFramebufferMap[framebuffer] = new Framebuffer();
980 }
981
982 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000983}
984
985void Context::bindDrawFramebuffer(GLuint framebuffer)
986{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000987 if (!getFramebuffer(framebuffer))
988 {
989 mFramebufferMap[framebuffer] = new Framebuffer();
990 }
991
992 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
995void Context::bindRenderbuffer(GLuint renderbuffer)
996{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000997 mResourceManager->checkRenderbufferAllocation(renderbuffer);
998
999 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000}
1001
1002void Context::useProgram(GLuint program)
1003{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001004 GLuint priorProgram = mState.currentProgram;
1005 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001006
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001007 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001009 Program *newProgram = mResourceManager->getProgram(program);
1010 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1011
1012 if (newProgram)
1013 {
1014 newProgram->addRef();
1015 }
1016
1017 if (oldProgram)
1018 {
1019 oldProgram->release();
1020 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022}
1023
1024void Context::setFramebufferZero(Framebuffer *buffer)
1025{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001026 delete mFramebufferMap[0];
1027 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001030void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001032 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1033 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034}
1035
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001036Framebuffer *Context::getFramebuffer(unsigned int handle)
1037{
1038 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1039
1040 if (framebuffer == mFramebufferMap.end())
1041 {
1042 return NULL;
1043 }
1044 else
1045 {
1046 return framebuffer->second;
1047 }
1048}
1049
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001050Fence *Context::getFence(unsigned int handle)
1051{
1052 FenceMap::iterator fence = mFenceMap.find(handle);
1053
1054 if (fence == mFenceMap.end())
1055 {
1056 return NULL;
1057 }
1058 else
1059 {
1060 return fence->second;
1061 }
1062}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001063
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064Buffer *Context::getArrayBuffer()
1065{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001066 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067}
1068
1069Buffer *Context::getElementArrayBuffer()
1070{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001071 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072}
1073
1074Program *Context::getCurrentProgram()
1075{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001076 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
1079Texture2D *Context::getTexture2D()
1080{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001081 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, SAMPLER_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082}
1083
1084TextureCubeMap *Context::getTextureCubeMap()
1085{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001086 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, SAMPLER_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087}
1088
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001089Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001091 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001092
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001093 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001094 {
1095 switch (type)
1096 {
1097 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001098 case SAMPLER_2D: return mTexture2DZero.get();
1099 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001100 }
1101 }
1102
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001103 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104}
1105
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001106bool Context::getBooleanv(GLenum pname, GLboolean *params)
1107{
1108 switch (pname)
1109 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001110 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1111 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1112 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001113 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001114 params[0] = mState.colorMaskRed;
1115 params[1] = mState.colorMaskGreen;
1116 params[2] = mState.colorMaskBlue;
1117 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001118 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001119 case GL_CULL_FACE: *params = mState.cullFace; break;
1120 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1121 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1122 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1123 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1124 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1125 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1126 case GL_BLEND: *params = mState.blend; break;
1127 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001128 default:
1129 return false;
1130 }
1131
1132 return true;
1133}
1134
1135bool Context::getFloatv(GLenum pname, GLfloat *params)
1136{
1137 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1138 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1139 // GetIntegerv as its native query function. As it would require conversion in any
1140 // case, this should make no difference to the calling application.
1141 switch (pname)
1142 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001143 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1144 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1145 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1146 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1147 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001148 case GL_ALIASED_LINE_WIDTH_RANGE:
1149 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1150 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1151 break;
1152 case GL_ALIASED_POINT_SIZE_RANGE:
1153 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001154 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 +00001155 break;
1156 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001157 params[0] = mState.zNear;
1158 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001159 break;
1160 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 params[0] = mState.colorClearValue.red;
1162 params[1] = mState.colorClearValue.green;
1163 params[2] = mState.colorClearValue.blue;
1164 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001165 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001166 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001167 params[0] = mState.blendColor.red;
1168 params[1] = mState.blendColor.green;
1169 params[2] = mState.blendColor.blue;
1170 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001171 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001172 default:
1173 return false;
1174 }
1175
1176 return true;
1177}
1178
1179bool Context::getIntegerv(GLenum pname, GLint *params)
1180{
1181 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1182 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1183 // GetIntegerv as its native query function. As it would require conversion in any
1184 // case, this should make no difference to the calling application. You may find it in
1185 // Context::getFloatv.
1186 switch (pname)
1187 {
1188 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1189 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001190 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001191 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1192 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1193 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001194 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001195 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001196 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001197 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001198 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1199 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001200 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1201 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1202 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001203 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001204 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1205 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1206 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1207 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001208 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001209 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1210 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1211 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1212 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1213 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1214 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1215 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1216 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1217 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1218 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1219 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1220 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1221 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1222 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1223 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1224 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1225 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1226 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1227 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1228 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1229 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1230 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1231 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1232 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001233 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1234 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001235 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1236 {
1237 if (supportsCompressedTextures())
1238 {
1239 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1240 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1241 *params = 2;
1242 }
1243 else
1244 {
1245 *params = 0;
1246 }
1247 }
1248 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001249 case GL_MAX_SAMPLES_ANGLE:
1250 {
1251 GLsizei maxSamples = getMaxSupportedSamples();
1252 if (maxSamples != 0)
1253 {
1254 *params = maxSamples;
1255 }
1256 else
1257 {
1258 return false;
1259 }
1260
1261 break;
1262 }
1263 case GL_SAMPLE_BUFFERS:
1264 case GL_SAMPLES:
1265 {
1266 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1267 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1268 {
1269 switch (pname)
1270 {
1271 case GL_SAMPLE_BUFFERS:
1272 if (framebuffer->getSamples() != 0)
1273 {
1274 *params = 1;
1275 }
1276 else
1277 {
1278 *params = 0;
1279 }
1280 break;
1281 case GL_SAMPLES:
1282 *params = framebuffer->getSamples();
1283 break;
1284 }
1285 }
1286 else
1287 {
1288 *params = 0;
1289 }
1290 }
1291 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001292 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1293 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1294 case GL_MAX_VIEWPORT_DIMS:
1295 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001296 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001297 params[0] = maxDimension;
1298 params[1] = maxDimension;
1299 }
1300 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001301 case GL_COMPRESSED_TEXTURE_FORMATS:
1302 {
1303 if (supportsCompressedTextures())
1304 {
1305 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1306 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1307 }
1308 }
1309 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001310 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001311 params[0] = mState.viewportX;
1312 params[1] = mState.viewportY;
1313 params[2] = mState.viewportWidth;
1314 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001315 break;
1316 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001317 params[0] = mState.scissorX;
1318 params[1] = mState.scissorY;
1319 params[2] = mState.scissorWidth;
1320 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001321 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001322 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1323 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001324 case GL_RED_BITS:
1325 case GL_GREEN_BITS:
1326 case GL_BLUE_BITS:
1327 case GL_ALPHA_BITS:
1328 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001329 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001330 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1331
1332 if (colorbuffer)
1333 {
1334 switch (pname)
1335 {
1336 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1337 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1338 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1339 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1340 }
1341 }
1342 else
1343 {
1344 *params = 0;
1345 }
1346 }
1347 break;
1348 case GL_DEPTH_BITS:
1349 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001350 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001351 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001352
1353 if (depthbuffer)
1354 {
1355 *params = depthbuffer->getDepthSize();
1356 }
1357 else
1358 {
1359 *params = 0;
1360 }
1361 }
1362 break;
1363 case GL_STENCIL_BITS:
1364 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001365 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001366 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001367
1368 if (stencilbuffer)
1369 {
1370 *params = stencilbuffer->getStencilSize();
1371 }
1372 else
1373 {
1374 *params = 0;
1375 }
1376 }
1377 break;
1378 case GL_TEXTURE_BINDING_2D:
1379 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001380 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001381 {
1382 error(GL_INVALID_OPERATION);
1383 return false;
1384 }
1385
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001386 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001387 }
1388 break;
1389 case GL_TEXTURE_BINDING_CUBE_MAP:
1390 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001391 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001392 {
1393 error(GL_INVALID_OPERATION);
1394 return false;
1395 }
1396
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001397 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001398 }
1399 break;
1400 default:
1401 return false;
1402 }
1403
1404 return true;
1405}
1406
1407bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1408{
1409 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1410 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1411 // to the fact that it is stored internally as a float, and so would require conversion
1412 // if returned from Context::getIntegerv. Since this conversion is already implemented
1413 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1414 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1415 // application.
1416 switch (pname)
1417 {
1418 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1419 case GL_SHADER_BINARY_FORMATS:
1420 {
1421 *type = GL_INT;
1422 *numParams = 0;
1423 }
1424 break;
1425 case GL_MAX_VERTEX_ATTRIBS:
1426 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1427 case GL_MAX_VARYING_VECTORS:
1428 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1429 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1430 case GL_MAX_TEXTURE_IMAGE_UNITS:
1431 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1432 case GL_MAX_RENDERBUFFER_SIZE:
1433 case GL_NUM_SHADER_BINARY_FORMATS:
1434 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1435 case GL_ARRAY_BUFFER_BINDING:
1436 case GL_FRAMEBUFFER_BINDING:
1437 case GL_RENDERBUFFER_BINDING:
1438 case GL_CURRENT_PROGRAM:
1439 case GL_PACK_ALIGNMENT:
1440 case GL_UNPACK_ALIGNMENT:
1441 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001442 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001443 case GL_RED_BITS:
1444 case GL_GREEN_BITS:
1445 case GL_BLUE_BITS:
1446 case GL_ALPHA_BITS:
1447 case GL_DEPTH_BITS:
1448 case GL_STENCIL_BITS:
1449 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1450 case GL_CULL_FACE_MODE:
1451 case GL_FRONT_FACE:
1452 case GL_ACTIVE_TEXTURE:
1453 case GL_STENCIL_FUNC:
1454 case GL_STENCIL_VALUE_MASK:
1455 case GL_STENCIL_REF:
1456 case GL_STENCIL_FAIL:
1457 case GL_STENCIL_PASS_DEPTH_FAIL:
1458 case GL_STENCIL_PASS_DEPTH_PASS:
1459 case GL_STENCIL_BACK_FUNC:
1460 case GL_STENCIL_BACK_VALUE_MASK:
1461 case GL_STENCIL_BACK_REF:
1462 case GL_STENCIL_BACK_FAIL:
1463 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1464 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1465 case GL_DEPTH_FUNC:
1466 case GL_BLEND_SRC_RGB:
1467 case GL_BLEND_SRC_ALPHA:
1468 case GL_BLEND_DST_RGB:
1469 case GL_BLEND_DST_ALPHA:
1470 case GL_BLEND_EQUATION_RGB:
1471 case GL_BLEND_EQUATION_ALPHA:
1472 case GL_STENCIL_WRITEMASK:
1473 case GL_STENCIL_BACK_WRITEMASK:
1474 case GL_STENCIL_CLEAR_VALUE:
1475 case GL_SUBPIXEL_BITS:
1476 case GL_MAX_TEXTURE_SIZE:
1477 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1478 case GL_SAMPLE_BUFFERS:
1479 case GL_SAMPLES:
1480 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1481 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1482 case GL_TEXTURE_BINDING_2D:
1483 case GL_TEXTURE_BINDING_CUBE_MAP:
1484 {
1485 *type = GL_INT;
1486 *numParams = 1;
1487 }
1488 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001489 case GL_MAX_SAMPLES_ANGLE:
1490 {
1491 if (getMaxSupportedSamples() != 0)
1492 {
1493 *type = GL_INT;
1494 *numParams = 1;
1495 }
1496 else
1497 {
1498 return false;
1499 }
1500 }
1501 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001502 case GL_MAX_VIEWPORT_DIMS:
1503 {
1504 *type = GL_INT;
1505 *numParams = 2;
1506 }
1507 break;
1508 case GL_VIEWPORT:
1509 case GL_SCISSOR_BOX:
1510 {
1511 *type = GL_INT;
1512 *numParams = 4;
1513 }
1514 break;
1515 case GL_SHADER_COMPILER:
1516 case GL_SAMPLE_COVERAGE_INVERT:
1517 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001518 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1519 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1520 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1521 case GL_SAMPLE_COVERAGE:
1522 case GL_SCISSOR_TEST:
1523 case GL_STENCIL_TEST:
1524 case GL_DEPTH_TEST:
1525 case GL_BLEND:
1526 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001527 {
1528 *type = GL_BOOL;
1529 *numParams = 1;
1530 }
1531 break;
1532 case GL_COLOR_WRITEMASK:
1533 {
1534 *type = GL_BOOL;
1535 *numParams = 4;
1536 }
1537 break;
1538 case GL_POLYGON_OFFSET_FACTOR:
1539 case GL_POLYGON_OFFSET_UNITS:
1540 case GL_SAMPLE_COVERAGE_VALUE:
1541 case GL_DEPTH_CLEAR_VALUE:
1542 case GL_LINE_WIDTH:
1543 {
1544 *type = GL_FLOAT;
1545 *numParams = 1;
1546 }
1547 break;
1548 case GL_ALIASED_LINE_WIDTH_RANGE:
1549 case GL_ALIASED_POINT_SIZE_RANGE:
1550 case GL_DEPTH_RANGE:
1551 {
1552 *type = GL_FLOAT;
1553 *numParams = 2;
1554 }
1555 break;
1556 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001557 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001558 {
1559 *type = GL_FLOAT;
1560 *numParams = 4;
1561 }
1562 break;
1563 default:
1564 return false;
1565 }
1566
1567 return true;
1568}
1569
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001570// Applies the render target surface, depth stencil surface, viewport rectangle and
1571// scissor rectangle to the Direct3D 9 device
1572bool Context::applyRenderTarget(bool ignoreViewport)
1573{
1574 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001575
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001576 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577
1578 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1579 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001580 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001581 }
1582
1583 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001584
1585 if (!renderTarget)
1586 {
1587 return false; // Context must be lost
1588 }
1589
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001590 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001591
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001592 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1593 if (renderTargetSerial != mAppliedRenderTargetSerial)
1594 {
1595 device->SetRenderTarget(0, renderTarget);
1596 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001597 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 +00001598 }
1599
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001600 unsigned int depthbufferSerial = 0;
1601 unsigned int stencilbufferSerial = 0;
1602 if (framebufferObject->getDepthbufferType() != GL_NONE)
1603 {
1604 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001605 if (!depthStencil)
1606 {
1607 ERR("Depth stencil pointer unexpectedly null.");
1608 return false;
1609 }
1610
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001611 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1612 }
1613 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1614 {
1615 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001616 if (!depthStencil)
1617 {
1618 ERR("Depth stencil pointer unexpectedly null.");
1619 return false;
1620 }
1621
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001622 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1623 }
1624
1625 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001626 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001627 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001628 {
1629 device->SetDepthStencilSurface(depthStencil);
1630 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001631 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001632 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634
1635 D3DVIEWPORT9 viewport;
1636 D3DSURFACE_DESC desc;
1637 renderTarget->GetDesc(&desc);
1638
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001639 float zNear = clamp01(mState.zNear);
1640 float zFar = clamp01(mState.zFar);
1641
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642 if (ignoreViewport)
1643 {
1644 viewport.X = 0;
1645 viewport.Y = 0;
1646 viewport.Width = desc.Width;
1647 viewport.Height = desc.Height;
1648 viewport.MinZ = 0.0f;
1649 viewport.MaxZ = 1.0f;
1650 }
1651 else
1652 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001653 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1654 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1655 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1656 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1657 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 +00001658 viewport.MinZ = zNear;
1659 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001660 }
1661
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001662 if (viewport.Width <= 0 || viewport.Height <= 0)
1663 {
1664 return false; // Nothing to render
1665 }
1666
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 device->SetViewport(&viewport);
1668
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001669 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001671 if (mState.scissorTest)
1672 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001673 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1674 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1675 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1676 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1677 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001678 device->SetScissorRect(&rect);
1679 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1680 }
1681 else
1682 {
1683 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1684 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001685
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001686 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687 }
1688
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001689 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691 Program *programObject = getCurrentProgram();
1692
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001693 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001694 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001695 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001696
daniel@transgaming.com31754962010-11-28 02:02:52 +00001697 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001698 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1699 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1700 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001701 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001702
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001703 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001704 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001705 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001706
daniel@transgaming.com31754962010-11-28 02:02:52 +00001707 GLint depthRange = programObject->getDxDepthRangeLocation();
1708 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1709 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001710 }
1711
1712 return true;
1713}
1714
1715// 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 +00001716void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717{
1718 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001719 Program *programObject = getCurrentProgram();
1720
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001721 Framebuffer *framebufferObject = getDrawFramebuffer();
1722
1723 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1724
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001725 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001726 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001727 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001729 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001730 GLint alwaysFront = !isTriangleMode(drawMode);
1731 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1732
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001733 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001735 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001737 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 }
1739 else
1740 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 }
1743
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001744 mCullStateDirty = false;
1745 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 if (mDepthStateDirty)
1748 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001749 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1752 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753 }
1754 else
1755 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001758
1759 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 }
1761
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001762 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001764 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001765 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001766 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1767
1768 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1769 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1770 {
1771 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1772 }
1773 else
1774 {
1775 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1776 unorm<8>(mState.blendColor.alpha),
1777 unorm<8>(mState.blendColor.alpha),
1778 unorm<8>(mState.blendColor.alpha)));
1779 }
1780
1781 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1782 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1783 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1784
1785 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1786 mState.destBlendRGB != mState.destBlendAlpha ||
1787 mState.blendEquationRGB != mState.blendEquationAlpha)
1788 {
1789 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1790
1791 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1792 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1793 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001794 }
1795 else
1796 {
1797 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1798 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001799 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001800 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001801 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001802 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001803 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001804
1805 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806 }
1807
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001808 if (mStencilStateDirty || mFrontFaceDirty)
1809 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001810 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001811 {
1812 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1813 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1814
1815 // FIXME: Unsupported by D3D9
1816 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1817 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1818 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1819 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1820 mState.stencilRef != mState.stencilBackRef ||
1821 mState.stencilMask != mState.stencilBackMask)
1822 {
1823 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1824 return error(GL_INVALID_OPERATION);
1825 }
1826
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001827 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001828 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001829 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1830
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001831 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1832 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001833 es2dx::ConvertComparison(mState.stencilFunc));
1834
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001835 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1836 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001837
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001838 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001839 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001840 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001841 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001842 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001843 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1844
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001845 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1846 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001847 es2dx::ConvertComparison(mState.stencilBackFunc));
1848
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001849 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1850 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001851
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001852 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001853 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001854 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001855 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001856 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001857 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1858 }
1859 else
1860 {
1861 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1862 }
1863
1864 mStencilStateDirty = false;
1865 }
1866
1867 if (mMaskStateDirty)
1868 {
1869 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1870 mState.colorMaskBlue, mState.colorMaskAlpha));
1871 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1872
1873 mMaskStateDirty = false;
1874 }
1875
1876 if (mPolygonOffsetStateDirty)
1877 {
1878 if (mState.polygonOffsetFill)
1879 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001880 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001881 if (depthbuffer)
1882 {
1883 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1884 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1885 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1886 }
1887 }
1888 else
1889 {
1890 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1891 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1892 }
1893
1894 mPolygonOffsetStateDirty = false;
1895 }
1896
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001897 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001899 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001900 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001901 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001902 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001903 FIXME("Sample alpha to coverage is unimplemented.");
1904 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001905
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001906 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1907 if (mState.sampleCoverage)
1908 {
1909 unsigned int mask = 0;
1910 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001911 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001912 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001913
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001914 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001915 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001916 mask <<= 1;
1917
1918 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1919 {
1920 threshold += 1.0f;
1921 mask |= 1;
1922 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001923 }
1924 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001925
1926 if (mState.sampleCoverageInvert)
1927 {
1928 mask = ~mask;
1929 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001930
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001931 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1932 }
1933 else
1934 {
1935 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1936 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001937 }
1938 else
1939 {
1940 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001941 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001942
1943 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 }
1945
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001946 if (mDitherStateDirty)
1947 {
1948 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1949
1950 mDitherStateDirty = false;
1951 }
1952
1953 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954}
1955
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001956// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001957void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001958{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001959 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001961 if (attributes[i].active)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001963 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964 }
1965 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001966}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967
daniel@transgaming.com83921382011-01-08 05:46:00 +00001968GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001969{
1970 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1971
daniel@transgaming.com83921382011-01-08 05:46:00 +00001972 GLenum err = mVertexDataManager->prepareVertexData(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001973 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001974 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001975 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001976 }
1977
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001978 lookupAttributeMapping(translated);
1979
daniel@transgaming.com83921382011-01-08 05:46:00 +00001980 mVertexDataManager->setupAttributes(translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001981
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001982 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001983}
1984
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001985// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001986GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001987{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001988 IDirect3DDevice9 *device = getDevice();
1989 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001990
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001991 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001992 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001993 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001994 }
1995
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001996 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997}
1998
1999// Applies the shaders and shader constants to the Direct3D 9 device
2000void Context::applyShaders()
2001{
2002 IDirect3DDevice9 *device = getDevice();
2003 Program *programObject = getCurrentProgram();
2004 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2005 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2006
2007 device->SetVertexShader(vertexShader);
2008 device->SetPixelShader(pixelShader);
2009
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002010 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002011 {
2012 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002013 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002014 }
2015
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016 programObject->applyUniforms();
2017}
2018
2019// Applies the textures and sampler states to the Direct3D 9 device
2020void Context::applyTextures()
2021{
2022 IDirect3DDevice9 *device = getDevice();
2023 Program *programObject = getCurrentProgram();
2024
2025 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2026 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002027 int textureUnit = programObject->getSamplerMapping(sampler);
2028 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002030 SamplerType textureType = programObject->getSamplerType(sampler);
2031
2032 Texture *texture = getSamplerTexture(textureUnit, textureType);
2033
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002034 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002035 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002036 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2037
2038 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002039 {
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002040 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter())
2041 {
2042 GLenum wrapS = texture->getWrapS();
2043 GLenum wrapT = texture->getWrapT();
2044 GLenum minFilter = texture->getMinFilter();
2045 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002047 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2048 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002050 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2051 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2052 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2053 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2054 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
2055 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002056
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002057 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyImage())
2058 {
2059 device->SetTexture(sampler, d3dTexture);
2060 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002061 }
2062 else
2063 {
2064 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2065 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002066
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002067 mAppliedTextureSerial[sampler] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002068 texture->resetDirty();
2069 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002071 else
2072 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002073 if (mAppliedTextureSerial[sampler] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002074 {
2075 device->SetTexture(sampler, NULL);
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002076 mAppliedTextureSerial[sampler] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002077 }
2078 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 }
2080}
2081
2082void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2083{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002084 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002085
2086 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2087 {
2088 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2089 }
2090
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002091 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2092 {
2093 return error(GL_INVALID_OPERATION);
2094 }
2095
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002097
2098 if (!renderTarget)
2099 {
2100 return; // Context must be lost, return silently
2101 }
2102
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 IDirect3DDevice9 *device = getDevice();
2104
2105 D3DSURFACE_DESC desc;
2106 renderTarget->GetDesc(&desc);
2107
2108 IDirect3DSurface9 *systemSurface;
2109 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2110
2111 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2112 {
2113 return error(GL_OUT_OF_MEMORY);
2114 }
2115
2116 ASSERT(SUCCEEDED(result));
2117
2118 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2119 {
2120 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2121 }
2122
2123 result = device->GetRenderTargetData(renderTarget, systemSurface);
2124
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 if (FAILED(result))
2126 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 systemSurface->Release();
2128
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002129 switch (result)
2130 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002131 case D3DERR_DRIVERINTERNALERROR:
2132 case D3DERR_DEVICELOST:
2133 return error(GL_OUT_OF_MEMORY);
2134 default:
2135 UNREACHABLE();
2136 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002137 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002138 }
2139
2140 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002141 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2142 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2143 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2144 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2145 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146
2147 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2148
2149 if (FAILED(result))
2150 {
2151 UNREACHABLE();
2152 systemSurface->Release();
2153
2154 return; // No sensible error to generate
2155 }
2156
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002157 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002159 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002160 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002161 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002162
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 for (int j = 0; j < rect.bottom - rect.top; j++)
2164 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002165 if (desc.Format == D3DFMT_A8R8G8B8 &&
2166 format == GL_BGRA_EXT &&
2167 type == GL_UNSIGNED_BYTE)
2168 {
2169 // Fast path for EXT_read_format_bgra, given
2170 // an RGBA source buffer. Note that buffers with no
2171 // alpha go through the slow path below.
2172 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002173 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002174 (rect.right - rect.left) * 4);
2175 continue;
2176 }
2177
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178 for (int i = 0; i < rect.right - rect.left; i++)
2179 {
2180 float r;
2181 float g;
2182 float b;
2183 float a;
2184
2185 switch (desc.Format)
2186 {
2187 case D3DFMT_R5G6B5:
2188 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002189 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190
2191 a = 1.0f;
2192 b = (rgb & 0x001F) * (1.0f / 0x001F);
2193 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2194 r = (rgb & 0xF800) * (1.0f / 0xF800);
2195 }
2196 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197 case D3DFMT_A1R5G5B5:
2198 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002199 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002200
2201 a = (argb & 0x8000) ? 1.0f : 0.0f;
2202 b = (argb & 0x001F) * (1.0f / 0x001F);
2203 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2204 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2205 }
2206 break;
2207 case D3DFMT_A8R8G8B8:
2208 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002209 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002210
2211 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2212 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2213 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2214 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2215 }
2216 break;
2217 case D3DFMT_X8R8G8B8:
2218 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002219 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220
2221 a = 1.0f;
2222 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2223 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2224 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2225 }
2226 break;
2227 case D3DFMT_A2R10G10B10:
2228 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002229 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002230
2231 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2232 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2233 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2234 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2235 }
2236 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002237 case D3DFMT_A32B32G32R32F:
2238 {
2239 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002240 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2241 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2242 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2243 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002244 }
2245 break;
2246 case D3DFMT_A16B16G16R16F:
2247 {
2248 // float formats in D3D are stored rgba, rather than the other way round
2249 float abgr[4];
2250
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002251 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002252
2253 a = abgr[3];
2254 b = abgr[2];
2255 g = abgr[1];
2256 r = abgr[0];
2257 }
2258 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 default:
2260 UNIMPLEMENTED(); // FIXME
2261 UNREACHABLE();
2262 }
2263
2264 switch (format)
2265 {
2266 case GL_RGBA:
2267 switch (type)
2268 {
2269 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002270 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2271 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2272 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2273 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 break;
2275 default: UNREACHABLE();
2276 }
2277 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002278 case GL_BGRA_EXT:
2279 switch (type)
2280 {
2281 case GL_UNSIGNED_BYTE:
2282 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2283 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2284 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2285 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2286 break;
2287 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2288 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2289 // this type is packed as follows:
2290 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2291 // --------------------------------------------------------------------------------
2292 // | 4th | 3rd | 2nd | 1st component |
2293 // --------------------------------------------------------------------------------
2294 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2295 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2296 ((unsigned short)(15 * a + 0.5f) << 12)|
2297 ((unsigned short)(15 * r + 0.5f) << 8) |
2298 ((unsigned short)(15 * g + 0.5f) << 4) |
2299 ((unsigned short)(15 * b + 0.5f) << 0);
2300 break;
2301 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2302 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2303 // this type is packed as follows:
2304 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2305 // --------------------------------------------------------------------------------
2306 // | 4th | 3rd | 2nd | 1st component |
2307 // --------------------------------------------------------------------------------
2308 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2309 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2310 ((unsigned short)( a + 0.5f) << 15) |
2311 ((unsigned short)(31 * r + 0.5f) << 10) |
2312 ((unsigned short)(31 * g + 0.5f) << 5) |
2313 ((unsigned short)(31 * b + 0.5f) << 0);
2314 break;
2315 default: UNREACHABLE();
2316 }
2317 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002318 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 switch (type)
2320 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002321 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002322 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2323 ((unsigned short)(31 * b + 0.5f) << 0) |
2324 ((unsigned short)(63 * g + 0.5f) << 5) |
2325 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326 break;
2327 default: UNREACHABLE();
2328 }
2329 break;
2330 default: UNREACHABLE();
2331 }
2332 }
2333 }
2334
2335 systemSurface->UnlockRect();
2336
2337 systemSurface->Release();
2338}
2339
2340void Context::clear(GLbitfield mask)
2341{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002342 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002343
2344 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2345 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002346 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002347 }
2348
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002349 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350 IDirect3DDevice9 *device = getDevice();
2351 DWORD flags = 0;
2352
2353 if (mask & GL_COLOR_BUFFER_BIT)
2354 {
2355 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002356
2357 if (framebufferObject->getColorbufferType() != GL_NONE)
2358 {
2359 flags |= D3DCLEAR_TARGET;
2360 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361 }
2362
2363 if (mask & GL_DEPTH_BUFFER_BIT)
2364 {
2365 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002366 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002367 {
2368 flags |= D3DCLEAR_ZBUFFER;
2369 }
2370 }
2371
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372 GLuint stencilUnmasked = 0x0;
2373
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002374 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002377 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002379 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002380 if (!depthStencil)
2381 {
2382 ERR("Depth stencil pointer unexpectedly null.");
2383 return;
2384 }
2385
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002386 D3DSURFACE_DESC desc;
2387 depthStencil->GetDesc(&desc);
2388
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002389 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002390 stencilUnmasked = (0x1 << stencilSize) - 1;
2391
2392 if (stencilUnmasked != 0x0)
2393 {
2394 flags |= D3DCLEAR_STENCIL;
2395 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396 }
2397 }
2398
2399 if (mask != 0)
2400 {
2401 return error(GL_INVALID_VALUE);
2402 }
2403
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002404 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2405 {
2406 return;
2407 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002409 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002410 unorm<8>(mState.colorClearValue.red),
2411 unorm<8>(mState.colorClearValue.green),
2412 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002413 float depth = clamp01(mState.depthClearValue);
2414 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415
2416 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2417
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002418 if (!renderTarget)
2419 {
2420 return; // Context must be lost, return silently
2421 }
2422
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423 D3DSURFACE_DESC desc;
2424 renderTarget->GetDesc(&desc);
2425
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002426 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427
2428 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002429 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002431 !(mState.colorMaskRed && mState.colorMaskGreen &&
2432 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433
2434 if (needMaskedColorClear || needMaskedStencilClear)
2435 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002436 // State which is altered in all paths from this point to the clear call is saved.
2437 // State which is altered in only some paths will be flagged dirty in the case that
2438 // that path is taken.
2439 HRESULT hr;
2440 if (mMaskedClearSavedState == NULL)
2441 {
2442 hr = device->BeginStateBlock();
2443 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2444
2445 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2446 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2447 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2448 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2449 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2450 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2451 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2452 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2453 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2454 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2455 device->SetPixelShader(NULL);
2456 device->SetVertexShader(NULL);
2457 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002458 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2459 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2460 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2461 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2462 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2463 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2464 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002465
2466 hr = device->EndStateBlock(&mMaskedClearSavedState);
2467 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2468 }
2469
2470 ASSERT(mMaskedClearSavedState != NULL);
2471
2472 if (mMaskedClearSavedState != NULL)
2473 {
2474 hr = mMaskedClearSavedState->Capture();
2475 ASSERT(SUCCEEDED(hr));
2476 }
2477
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2479 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2480 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2481 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2482 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2483 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2484 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2485 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2486
2487 if (flags & D3DCLEAR_TARGET)
2488 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002489 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490 }
2491 else
2492 {
2493 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2494 }
2495
2496 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2497 {
2498 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2499 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2500 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2501 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002502 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002503 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2505 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002506 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507 }
2508 else
2509 {
2510 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2511 }
2512
2513 device->SetPixelShader(NULL);
2514 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002515 device->SetFVF(D3DFVF_XYZRHW);
2516 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2517 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2518 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2519 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2520 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2521 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2522 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002524 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2525 quad[0][0] = -0.5f;
2526 quad[0][1] = desc.Height - 0.5f;
2527 quad[0][2] = 0.0f;
2528 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002529
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002530 quad[1][0] = desc.Width - 0.5f;
2531 quad[1][1] = desc.Height - 0.5f;
2532 quad[1][2] = 0.0f;
2533 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002535 quad[2][0] = -0.5f;
2536 quad[2][1] = -0.5f;
2537 quad[2][2] = 0.0f;
2538 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002540 quad[3][0] = desc.Width - 0.5f;
2541 quad[3][1] = -0.5f;
2542 quad[3][2] = 0.0f;
2543 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002545 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002546 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002547
2548 if (flags & D3DCLEAR_ZBUFFER)
2549 {
2550 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2551 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2552 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2553 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002554
2555 if (mMaskedClearSavedState != NULL)
2556 {
2557 mMaskedClearSavedState->Apply();
2558 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002560 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561 {
2562 device->Clear(0, NULL, flags, color, depth, stencil);
2563 }
2564}
2565
2566void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2567{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002568 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569 {
2570 return error(GL_INVALID_OPERATION);
2571 }
2572
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002573 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002574 IDirect3DDevice9 *device = getDevice();
2575 D3DPRIMITIVETYPE primitiveType;
2576 int primitiveCount;
2577
2578 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2579 return error(GL_INVALID_ENUM);
2580
2581 if (primitiveCount <= 0)
2582 {
2583 return;
2584 }
2585
2586 if (!applyRenderTarget(false))
2587 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002588 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589 }
2590
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002591 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002592
daniel@transgaming.com83921382011-01-08 05:46:00 +00002593 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002594 if (err != GL_NO_ERROR)
2595 {
2596 return error(err);
2597 }
2598
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599 applyShaders();
2600 applyTextures();
2601
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002602 if (!getCurrentProgram()->validateSamplers())
2603 {
2604 return error(GL_INVALID_OPERATION);
2605 }
2606
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002607 if (!cullSkipsDraw(mode))
2608 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002609 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002610
2611 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002612
2613 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2614 {
2615 drawClosingLine(first, first + count - 1);
2616 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002617 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002618}
2619
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002620void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002622 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002623 {
2624 return error(GL_INVALID_OPERATION);
2625 }
2626
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002627 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002628 {
2629 return error(GL_INVALID_OPERATION);
2630 }
2631
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002632 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633 IDirect3DDevice9 *device = getDevice();
2634 D3DPRIMITIVETYPE primitiveType;
2635 int primitiveCount;
2636
2637 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2638 return error(GL_INVALID_ENUM);
2639
2640 if (primitiveCount <= 0)
2641 {
2642 return;
2643 }
2644
2645 if (!applyRenderTarget(false))
2646 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002647 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002648 }
2649
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002650 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002651
2652 TranslatedIndexData indexInfo;
2653 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2654 if (err != GL_NO_ERROR)
2655 {
2656 return error(err);
2657 }
2658
daniel@transgaming.com83921382011-01-08 05:46:00 +00002659 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2660 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002661 if (err != GL_NO_ERROR)
2662 {
2663 return error(err);
2664 }
2665
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 applyShaders();
2667 applyTextures();
2668
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002669 if (!getCurrentProgram()->validateSamplers())
2670 {
2671 return error(GL_INVALID_OPERATION);
2672 }
2673
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002674 if (!cullSkipsDraw(mode))
2675 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002676 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002677
daniel@transgaming.com83921382011-01-08 05:46:00 +00002678 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002679
2680 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2681 {
2682 drawClosingLine(count, type, indices);
2683 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002684 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002685}
2686
2687void Context::finish()
2688{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002689 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690 IDirect3DDevice9 *device = getDevice();
2691 IDirect3DQuery9 *occlusionQuery = NULL;
2692
2693 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2694
2695 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2696 {
2697 return error(GL_OUT_OF_MEMORY);
2698 }
2699
2700 ASSERT(SUCCEEDED(result));
2701
2702 if (occlusionQuery)
2703 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002704 IDirect3DStateBlock9 *savedState = NULL;
2705 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2706
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002707 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2708 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002709
2710 // Render something outside the render target
2711 device->SetPixelShader(NULL);
2712 device->SetVertexShader(NULL);
2713 device->SetFVF(D3DFVF_XYZRHW);
2714 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002715 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002716 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002717
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002718 result = occlusionQuery->Issue(D3DISSUE_END);
2719 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720
2721 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2722 {
2723 // Keep polling, but allow other threads to do something useful first
2724 Sleep(0);
2725 }
2726
2727 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002728
2729 if (savedState)
2730 {
2731 savedState->Apply();
2732 savedState->Release();
2733 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734 }
2735}
2736
2737void Context::flush()
2738{
2739 IDirect3DDevice9 *device = getDevice();
2740 IDirect3DQuery9 *eventQuery = NULL;
2741
2742 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2743
2744 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2745 {
2746 return error(GL_OUT_OF_MEMORY);
2747 }
2748
2749 ASSERT(SUCCEEDED(result));
2750
2751 if (eventQuery)
2752 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002753 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2754 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002755
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002756 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002757 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002758
2759 if (result == D3DERR_DEVICELOST)
2760 {
2761 error(GL_OUT_OF_MEMORY);
2762 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002763 }
2764}
2765
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002766void Context::drawClosingLine(unsigned int first, unsigned int last)
2767{
2768 IDirect3DDevice9 *device = getDevice();
2769 IDirect3DIndexBuffer9 *indexBuffer = NULL;
2770 HRESULT result = D3DERR_INVALIDCALL;
2771
2772 if (supports32bitIndices())
2773 {
2774 result = device->CreateIndexBuffer(8, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &indexBuffer, 0);
2775
2776 if (SUCCEEDED(result))
2777 {
2778 unsigned int *data;
2779 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2780
2781 if (SUCCEEDED(result))
2782 {
2783 data[0] = last;
2784 data[1] = first;
2785 }
2786 }
2787 }
2788 else
2789 {
2790 result = device->CreateIndexBuffer(4, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexBuffer, 0);
2791
2792 if (SUCCEEDED(result))
2793 {
2794 unsigned short *data;
2795 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2796
2797 if (SUCCEEDED(result))
2798 {
2799 data[0] = last;
2800 data[1] = first;
2801 }
2802 }
2803 }
2804
2805 if (SUCCEEDED(result))
2806 {
2807 indexBuffer->Unlock();
2808 device->SetIndices(indexBuffer);
2809
2810 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, 0, 1);
2811
2812 indexBuffer->Release();
2813 }
2814 else
2815 {
2816 ERR("Could not create an index buffer for closing a line loop.");
2817 error(GL_OUT_OF_MEMORY);
2818 }
2819}
2820
2821void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2822{
2823 unsigned int first = 0;
2824 unsigned int last = 0;
2825
2826 if (mState.elementArrayBuffer.get())
2827 {
2828 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2829 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2830 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2831 }
2832
2833 switch (type)
2834 {
2835 case GL_UNSIGNED_BYTE:
2836 first = static_cast<const GLubyte*>(indices)[0];
2837 last = static_cast<const GLubyte*>(indices)[count - 1];
2838 break;
2839 case GL_UNSIGNED_SHORT:
2840 first = static_cast<const GLushort*>(indices)[0];
2841 last = static_cast<const GLushort*>(indices)[count - 1];
2842 break;
2843 case GL_UNSIGNED_INT:
2844 first = static_cast<const GLuint*>(indices)[0];
2845 last = static_cast<const GLuint*>(indices)[count - 1];
2846 break;
2847 default: UNREACHABLE();
2848 }
2849
2850 drawClosingLine(first, last);
2851}
2852
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002853void Context::recordInvalidEnum()
2854{
2855 mInvalidEnum = true;
2856}
2857
2858void Context::recordInvalidValue()
2859{
2860 mInvalidValue = true;
2861}
2862
2863void Context::recordInvalidOperation()
2864{
2865 mInvalidOperation = true;
2866}
2867
2868void Context::recordOutOfMemory()
2869{
2870 mOutOfMemory = true;
2871}
2872
2873void Context::recordInvalidFramebufferOperation()
2874{
2875 mInvalidFramebufferOperation = true;
2876}
2877
2878// Get one of the recorded errors and clear its flag, if any.
2879// [OpenGL ES 2.0.24] section 2.5 page 13.
2880GLenum Context::getError()
2881{
2882 if (mInvalidEnum)
2883 {
2884 mInvalidEnum = false;
2885
2886 return GL_INVALID_ENUM;
2887 }
2888
2889 if (mInvalidValue)
2890 {
2891 mInvalidValue = false;
2892
2893 return GL_INVALID_VALUE;
2894 }
2895
2896 if (mInvalidOperation)
2897 {
2898 mInvalidOperation = false;
2899
2900 return GL_INVALID_OPERATION;
2901 }
2902
2903 if (mOutOfMemory)
2904 {
2905 mOutOfMemory = false;
2906
2907 return GL_OUT_OF_MEMORY;
2908 }
2909
2910 if (mInvalidFramebufferOperation)
2911 {
2912 mInvalidFramebufferOperation = false;
2913
2914 return GL_INVALID_FRAMEBUFFER_OPERATION;
2915 }
2916
2917 return GL_NO_ERROR;
2918}
2919
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002920bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002921{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002922 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002923}
2924
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002925int Context::getMaximumVaryingVectors() const
2926{
2927 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2928}
2929
daniel@transgaming.com458da142010-11-28 02:03:02 +00002930int Context::getMaximumFragmentUniformVectors() const
2931{
2932 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2933}
2934
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002935int Context::getMaxSupportedSamples() const
2936{
2937 return mMaxSupportedSamples;
2938}
2939
2940int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2941{
2942 if (requested == 0)
2943 {
2944 return requested;
2945 }
2946
2947 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2948 if (itr == mMultiSampleSupport.end())
2949 {
2950 return -1;
2951 }
2952
2953 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2954 {
2955 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2956 {
2957 return i;
2958 }
2959 }
2960
2961 return -1;
2962}
2963
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002964bool Context::supportsEventQueries() const
2965{
2966 return mSupportsEventQueries;
2967}
2968
daniel@transgaming.com01868132010-08-24 19:21:17 +00002969bool Context::supportsCompressedTextures() const
2970{
2971 return mSupportsCompressedTextures;
2972}
2973
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002974bool Context::supportsFloatTextures() const
2975{
2976 return mSupportsFloatTextures;
2977}
2978
2979bool Context::supportsFloatLinearFilter() const
2980{
2981 return mSupportsFloatLinearFilter;
2982}
2983
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002984bool Context::supportsFloatRenderableTextures() const
2985{
2986 return mSupportsFloatRenderableTextures;
2987}
2988
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002989bool Context::supportsHalfFloatTextures() const
2990{
2991 return mSupportsHalfFloatTextures;
2992}
2993
2994bool Context::supportsHalfFloatLinearFilter() const
2995{
2996 return mSupportsHalfFloatLinearFilter;
2997}
2998
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002999bool Context::supportsHalfFloatRenderableTextures() const
3000{
3001 return mSupportsHalfFloatRenderableTextures;
3002}
3003
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003004int Context::getMaximumRenderbufferDimension() const
3005{
3006 return mMaxRenderbufferDimension;
3007}
3008
3009int Context::getMaximumTextureDimension() const
3010{
3011 return mMaxTextureDimension;
3012}
3013
3014int Context::getMaximumCubeTextureDimension() const
3015{
3016 return mMaxCubeTextureDimension;
3017}
3018
3019int Context::getMaximumTextureLevel() const
3020{
3021 return mMaxTextureLevel;
3022}
3023
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003024bool Context::supportsLuminanceTextures() const
3025{
3026 return mSupportsLuminanceTextures;
3027}
3028
3029bool Context::supportsLuminanceAlphaTextures() const
3030{
3031 return mSupportsLuminanceAlphaTextures;
3032}
3033
daniel@transgaming.com83921382011-01-08 05:46:00 +00003034bool Context::supports32bitIndices() const
3035{
3036 return mSupports32bitIndices;
3037}
3038
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003039void Context::detachBuffer(GLuint buffer)
3040{
3041 // [OpenGL ES 2.0.24] section 2.9 page 22:
3042 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3043 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3044
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003045 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003046 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003047 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003048 }
3049
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003050 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003051 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003052 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003053 }
3054
3055 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3056 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003057 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003058 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003059 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003060 }
3061 }
3062}
3063
3064void Context::detachTexture(GLuint texture)
3065{
3066 // [OpenGL ES 2.0.24] section 3.8 page 84:
3067 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3068 // rebound to texture object zero
3069
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003070 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003071 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003072 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003073 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003074 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003075 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003076 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003077 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003078 }
3079 }
3080
3081 // [OpenGL ES 2.0.24] section 4.4 page 112:
3082 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3083 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3084 // image was attached in the currently bound framebuffer.
3085
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003086 Framebuffer *readFramebuffer = getReadFramebuffer();
3087 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003088
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003089 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003090 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003091 readFramebuffer->detachTexture(texture);
3092 }
3093
3094 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3095 {
3096 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003097 }
3098}
3099
3100void Context::detachFramebuffer(GLuint framebuffer)
3101{
3102 // [OpenGL ES 2.0.24] section 4.4 page 107:
3103 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3104 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3105
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003106 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003107 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003108 bindReadFramebuffer(0);
3109 }
3110
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003111 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003112 {
3113 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003114 }
3115}
3116
3117void Context::detachRenderbuffer(GLuint renderbuffer)
3118{
3119 // [OpenGL ES 2.0.24] section 4.4 page 109:
3120 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3121 // had been executed with the target RENDERBUFFER and name of zero.
3122
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003123 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003124 {
3125 bindRenderbuffer(0);
3126 }
3127
3128 // [OpenGL ES 2.0.24] section 4.4 page 111:
3129 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3130 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3131 // point to which this image was attached in the currently bound framebuffer.
3132
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003133 Framebuffer *readFramebuffer = getReadFramebuffer();
3134 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003135
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003136 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003137 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003138 readFramebuffer->detachRenderbuffer(renderbuffer);
3139 }
3140
3141 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3142 {
3143 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003144 }
3145}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003146
3147Texture *Context::getIncompleteTexture(SamplerType type)
3148{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003149 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003150
3151 if (t == NULL)
3152 {
3153 static const GLubyte color[] = { 0, 0, 0, 255 };
3154
3155 switch (type)
3156 {
3157 default:
3158 UNREACHABLE();
3159 // default falls through to SAMPLER_2D
3160
3161 case SAMPLER_2D:
3162 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003163 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003164 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003165 t = incomplete2d;
3166 }
3167 break;
3168
3169 case SAMPLER_CUBE:
3170 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003171 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003172
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003173 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3174 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3175 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3176 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3177 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3178 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003179
3180 t = incompleteCube;
3181 }
3182 break;
3183 }
3184
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003185 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003186 }
3187
3188 return t;
3189}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003190
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003191bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003192{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003193 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003194}
3195
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003196bool Context::isTriangleMode(GLenum drawMode)
3197{
3198 switch (drawMode)
3199 {
3200 case GL_TRIANGLES:
3201 case GL_TRIANGLE_FAN:
3202 case GL_TRIANGLE_STRIP:
3203 return true;
3204 case GL_POINTS:
3205 case GL_LINES:
3206 case GL_LINE_LOOP:
3207 case GL_LINE_STRIP:
3208 return false;
3209 default: UNREACHABLE();
3210 }
3211
3212 return false;
3213}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003214
3215void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3216{
3217 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3218
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003219 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3220 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3221 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3222 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003223
daniel@transgaming.com83921382011-01-08 05:46:00 +00003224 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003225}
3226
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003227void Context::initExtensionString()
3228{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003229 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003230 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3231 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003232 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003233 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003234 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003235
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003236 if (supportsEventQueries())
3237 {
3238 mExtensionString += "GL_NV_fence ";
3239 }
3240
daniel@transgaming.com01868132010-08-24 19:21:17 +00003241 if (supportsCompressedTextures())
3242 {
3243 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3244 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003245
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003246 if (supportsFloatTextures())
3247 {
3248 mExtensionString += "GL_OES_texture_float ";
3249 }
3250
3251 if (supportsHalfFloatTextures())
3252 {
3253 mExtensionString += "GL_OES_texture_half_float ";
3254 }
3255
3256 if (supportsFloatLinearFilter())
3257 {
3258 mExtensionString += "GL_OES_texture_float_linear ";
3259 }
3260
3261 if (supportsHalfFloatLinearFilter())
3262 {
3263 mExtensionString += "GL_OES_texture_half_float_linear ";
3264 }
3265
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003266 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003267 {
3268 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3269 }
3270
daniel@transgaming.com83921382011-01-08 05:46:00 +00003271 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003272 {
3273 mExtensionString += "GL_OES_element_index_uint ";
3274 }
3275
3276 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3277 if (end != std::string::npos)
3278 {
3279 mExtensionString.resize(end+1);
3280 }
3281}
3282
3283const char *Context::getExtensionString() const
3284{
3285 return mExtensionString.c_str();
3286}
3287
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003288void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3289 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3290 GLbitfield mask)
3291{
3292 IDirect3DDevice9 *device = getDevice();
3293
3294 Framebuffer *readFramebuffer = getReadFramebuffer();
3295 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3296
3297 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3298 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3299 {
3300 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3301 }
3302
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003303 if (drawFramebuffer->getSamples() != 0)
3304 {
3305 return error(GL_INVALID_OPERATION);
3306 }
3307
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003308 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3309 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3310 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3311 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3312
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003313 RECT sourceRect;
3314 RECT destRect;
3315
3316 if (srcX0 < srcX1)
3317 {
3318 sourceRect.left = srcX0;
3319 sourceRect.right = srcX1;
3320 destRect.left = dstX0;
3321 destRect.right = dstX1;
3322 }
3323 else
3324 {
3325 sourceRect.left = srcX1;
3326 destRect.left = dstX1;
3327 sourceRect.right = srcX0;
3328 destRect.right = dstX0;
3329 }
3330
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003331 if (srcY0 < srcY1)
3332 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003333 sourceRect.top = readBufferHeight - srcY1;
3334 destRect.top = drawBufferHeight - dstY1;
3335 sourceRect.bottom = readBufferHeight - srcY0;
3336 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003337 }
3338 else
3339 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003340 sourceRect.top = readBufferHeight - srcY0;
3341 destRect.top = drawBufferHeight - dstY0;
3342 sourceRect.bottom = readBufferHeight - srcY1;
3343 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003344 }
3345
3346 RECT sourceScissoredRect = sourceRect;
3347 RECT destScissoredRect = destRect;
3348
3349 if (mState.scissorTest)
3350 {
3351 // Only write to parts of the destination framebuffer which pass the scissor test
3352 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3353 // rect will be checked against scissorY, rather than the bottom.
3354 if (destRect.left < mState.scissorX)
3355 {
3356 int xDiff = mState.scissorX - destRect.left;
3357 destScissoredRect.left = mState.scissorX;
3358 sourceScissoredRect.left += xDiff;
3359 }
3360
3361 if (destRect.right > mState.scissorX + mState.scissorWidth)
3362 {
3363 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3364 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3365 sourceScissoredRect.right -= xDiff;
3366 }
3367
3368 if (destRect.top < mState.scissorY)
3369 {
3370 int yDiff = mState.scissorY - destRect.top;
3371 destScissoredRect.top = mState.scissorY;
3372 sourceScissoredRect.top += yDiff;
3373 }
3374
3375 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3376 {
3377 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3378 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3379 sourceScissoredRect.bottom -= yDiff;
3380 }
3381 }
3382
3383 bool blitRenderTarget = false;
3384 bool blitDepthStencil = false;
3385
3386 RECT sourceTrimmedRect = sourceScissoredRect;
3387 RECT destTrimmedRect = destScissoredRect;
3388
3389 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3390 // the actual draw and read surfaces.
3391 if (sourceTrimmedRect.left < 0)
3392 {
3393 int xDiff = 0 - sourceTrimmedRect.left;
3394 sourceTrimmedRect.left = 0;
3395 destTrimmedRect.left += xDiff;
3396 }
3397
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003398 if (sourceTrimmedRect.right > readBufferWidth)
3399 {
3400 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3401 sourceTrimmedRect.right = readBufferWidth;
3402 destTrimmedRect.right -= xDiff;
3403 }
3404
3405 if (sourceTrimmedRect.top < 0)
3406 {
3407 int yDiff = 0 - sourceTrimmedRect.top;
3408 sourceTrimmedRect.top = 0;
3409 destTrimmedRect.top += yDiff;
3410 }
3411
3412 if (sourceTrimmedRect.bottom > readBufferHeight)
3413 {
3414 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3415 sourceTrimmedRect.bottom = readBufferHeight;
3416 destTrimmedRect.bottom -= yDiff;
3417 }
3418
3419 if (destTrimmedRect.left < 0)
3420 {
3421 int xDiff = 0 - destTrimmedRect.left;
3422 destTrimmedRect.left = 0;
3423 sourceTrimmedRect.left += xDiff;
3424 }
3425
3426 if (destTrimmedRect.right > drawBufferWidth)
3427 {
3428 int xDiff = destTrimmedRect.right - drawBufferWidth;
3429 destTrimmedRect.right = drawBufferWidth;
3430 sourceTrimmedRect.right -= xDiff;
3431 }
3432
3433 if (destTrimmedRect.top < 0)
3434 {
3435 int yDiff = 0 - destTrimmedRect.top;
3436 destTrimmedRect.top = 0;
3437 sourceTrimmedRect.top += yDiff;
3438 }
3439
3440 if (destTrimmedRect.bottom > drawBufferHeight)
3441 {
3442 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3443 destTrimmedRect.bottom = drawBufferHeight;
3444 sourceTrimmedRect.bottom -= yDiff;
3445 }
3446
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003447 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003448 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3449 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3450 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3451 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003452 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3453 {
3454 partialBufferCopy = true;
3455 }
3456
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003457 if (mask & GL_COLOR_BUFFER_BIT)
3458 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003459 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3460 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3461 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3462 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3463 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003464 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3465 {
3466 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3467 return error(GL_INVALID_OPERATION);
3468 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003469
3470 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3471 {
3472 return error(GL_INVALID_OPERATION);
3473 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003474
3475 blitRenderTarget = true;
3476
3477 }
3478
3479 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3480 {
3481 DepthStencilbuffer *readDSBuffer = NULL;
3482 DepthStencilbuffer *drawDSBuffer = NULL;
3483
3484 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3485 // both a depth and stencil buffer, it will be the same buffer.
3486
3487 if (mask & GL_DEPTH_BUFFER_BIT)
3488 {
3489 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3490 {
3491 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3492 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3493 {
3494 return error(GL_INVALID_OPERATION);
3495 }
3496
3497 blitDepthStencil = true;
3498 readDSBuffer = readFramebuffer->getDepthbuffer();
3499 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3500 }
3501 }
3502
3503 if (mask & GL_STENCIL_BUFFER_BIT)
3504 {
3505 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3506 {
3507 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3508 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3509 {
3510 return error(GL_INVALID_OPERATION);
3511 }
3512
3513 blitDepthStencil = true;
3514 readDSBuffer = readFramebuffer->getStencilbuffer();
3515 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3516 }
3517 }
3518
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003519 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003520 {
3521 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3522 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3523 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003524
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003525 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3526 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003527 {
3528 return error(GL_INVALID_OPERATION);
3529 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003530 }
3531
3532 if (blitRenderTarget || blitDepthStencil)
3533 {
3534 egl::Display *display = getDisplay();
3535 display->endScene();
3536
3537 if (blitRenderTarget)
3538 {
3539 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3540 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3541
3542 if (FAILED(result))
3543 {
3544 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3545 return;
3546 }
3547 }
3548
3549 if (blitDepthStencil)
3550 {
3551 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3552
3553 if (FAILED(result))
3554 {
3555 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3556 return;
3557 }
3558 }
3559 }
3560}
3561
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003562}
3563
3564extern "C"
3565{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003566gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003567{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003568 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003569}
3570
3571void glDestroyContext(gl::Context *context)
3572{
3573 delete context;
3574
3575 if (context == gl::getContext())
3576 {
3577 gl::makeCurrent(NULL, NULL, NULL);
3578 }
3579}
3580
3581void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3582{
3583 gl::makeCurrent(context, display, surface);
3584}
3585
3586gl::Context *glGetCurrentContext()
3587{
3588 return gl::getContext();
3589}
3590}