blob: d3c4ff769f3e83e1222ad6dc4c330ee572f8334c [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.combbf56f72010-04-20 18:52:13 +000028#include "libGLESv2/geometry/VertexDataManager.h"
29#include "libGLESv2/geometry/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.com092bd482010-05-12 03:39:36 +0000322 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000323 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000324 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000325 mDepthStencilInitialized = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000326 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000327
328 mClearStateDirty = true;
329 mCullStateDirty = true;
330 mDepthStateDirty = true;
331 mMaskStateDirty = true;
332 mBlendStateDirty = true;
333 mStencilStateDirty = true;
334 mPolygonOffsetStateDirty = true;
335 mScissorStateDirty = true;
336 mSampleStateDirty = true;
337 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000338 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000339}
340
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341void Context::setClearColor(float red, float green, float blue, float alpha)
342{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000343 mState.colorClearValue.red = red;
344 mState.colorClearValue.green = green;
345 mState.colorClearValue.blue = blue;
346 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347}
348
349void Context::setClearDepth(float depth)
350{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000351 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352}
353
354void Context::setClearStencil(int stencil)
355{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000356 mState.stencilClearValue = stencil;
357}
358
359void Context::setCullFace(bool enabled)
360{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000361 if (mState.cullFace != enabled)
362 {
363 mState.cullFace = enabled;
364 mCullStateDirty = true;
365 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000366}
367
368bool Context::isCullFaceEnabled() const
369{
370 return mState.cullFace;
371}
372
373void Context::setCullMode(GLenum mode)
374{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000375 if (mState.cullMode != mode)
376 {
377 mState.cullMode = mode;
378 mCullStateDirty = true;
379 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000380}
381
382void Context::setFrontFace(GLenum front)
383{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000384 if (mState.frontFace != front)
385 {
386 mState.frontFace = front;
387 mFrontFaceDirty = true;
388 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000389}
390
391void Context::setDepthTest(bool enabled)
392{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000393 if (mState.depthTest != enabled)
394 {
395 mState.depthTest = enabled;
396 mDepthStateDirty = true;
397 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000398}
399
400bool Context::isDepthTestEnabled() const
401{
402 return mState.depthTest;
403}
404
405void Context::setDepthFunc(GLenum depthFunc)
406{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000407 if (mState.depthFunc != depthFunc)
408 {
409 mState.depthFunc = depthFunc;
410 mDepthStateDirty = true;
411 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000412}
413
414void Context::setDepthRange(float zNear, float zFar)
415{
416 mState.zNear = zNear;
417 mState.zFar = zFar;
418}
419
420void Context::setBlend(bool enabled)
421{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000422 if (mState.blend != enabled)
423 {
424 mState.blend = enabled;
425 mBlendStateDirty = true;
426 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000427}
428
429bool Context::isBlendEnabled() const
430{
431 return mState.blend;
432}
433
434void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
435{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000436 if (mState.sourceBlendRGB != sourceRGB ||
437 mState.sourceBlendAlpha != sourceAlpha ||
438 mState.destBlendRGB != destRGB ||
439 mState.destBlendAlpha != destAlpha)
440 {
441 mState.sourceBlendRGB = sourceRGB;
442 mState.destBlendRGB = destRGB;
443 mState.sourceBlendAlpha = sourceAlpha;
444 mState.destBlendAlpha = destAlpha;
445 mBlendStateDirty = true;
446 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000447}
448
449void Context::setBlendColor(float red, float green, float blue, float alpha)
450{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000451 if (mState.blendColor.red != red ||
452 mState.blendColor.green != green ||
453 mState.blendColor.blue != blue ||
454 mState.blendColor.alpha != alpha)
455 {
456 mState.blendColor.red = red;
457 mState.blendColor.green = green;
458 mState.blendColor.blue = blue;
459 mState.blendColor.alpha = alpha;
460 mBlendStateDirty = true;
461 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000462}
463
464void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
465{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000466 if (mState.blendEquationRGB != rgbEquation ||
467 mState.blendEquationAlpha != alphaEquation)
468 {
469 mState.blendEquationRGB = rgbEquation;
470 mState.blendEquationAlpha = alphaEquation;
471 mBlendStateDirty = true;
472 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000473}
474
475void Context::setStencilTest(bool enabled)
476{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000477 if (mState.stencilTest != enabled)
478 {
479 mState.stencilTest = enabled;
480 mStencilStateDirty = true;
481 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000482}
483
484bool Context::isStencilTestEnabled() const
485{
486 return mState.stencilTest;
487}
488
489void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.stencilFunc != stencilFunc ||
492 mState.stencilRef != stencilRef ||
493 mState.stencilMask != stencilMask)
494 {
495 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000496 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 mState.stencilMask = stencilMask;
498 mStencilStateDirty = true;
499 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000500}
501
502void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
503{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000504 if (mState.stencilBackFunc != stencilBackFunc ||
505 mState.stencilBackRef != stencilBackRef ||
506 mState.stencilBackMask != stencilBackMask)
507 {
508 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000509 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 mState.stencilBackMask = stencilBackMask;
511 mStencilStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515void Context::setStencilWritemask(GLuint stencilWritemask)
516{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 if (mState.stencilWritemask != stencilWritemask)
518 {
519 mState.stencilWritemask = stencilWritemask;
520 mStencilStateDirty = true;
521 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000522}
523
524void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
525{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000526 if (mState.stencilBackWritemask != stencilBackWritemask)
527 {
528 mState.stencilBackWritemask = stencilBackWritemask;
529 mStencilStateDirty = true;
530 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000531}
532
533void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
534{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000535 if (mState.stencilFail != stencilFail ||
536 mState.stencilPassDepthFail != stencilPassDepthFail ||
537 mState.stencilPassDepthPass != stencilPassDepthPass)
538 {
539 mState.stencilFail = stencilFail;
540 mState.stencilPassDepthFail = stencilPassDepthFail;
541 mState.stencilPassDepthPass = stencilPassDepthPass;
542 mStencilStateDirty = true;
543 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000544}
545
546void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
547{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000548 if (mState.stencilBackFail != stencilBackFail ||
549 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
550 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
551 {
552 mState.stencilBackFail = stencilBackFail;
553 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
554 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
555 mStencilStateDirty = true;
556 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000557}
558
559void Context::setPolygonOffsetFill(bool enabled)
560{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000561 if (mState.polygonOffsetFill != enabled)
562 {
563 mState.polygonOffsetFill = enabled;
564 mPolygonOffsetStateDirty = true;
565 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000566}
567
568bool Context::isPolygonOffsetFillEnabled() const
569{
570 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000571
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000572}
573
574void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
575{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576 if (mState.polygonOffsetFactor != factor ||
577 mState.polygonOffsetUnits != units)
578 {
579 mState.polygonOffsetFactor = factor;
580 mState.polygonOffsetUnits = units;
581 mPolygonOffsetStateDirty = true;
582 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000583}
584
585void Context::setSampleAlphaToCoverage(bool enabled)
586{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000587 if (mState.sampleAlphaToCoverage != enabled)
588 {
589 mState.sampleAlphaToCoverage = enabled;
590 mSampleStateDirty = true;
591 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000592}
593
594bool Context::isSampleAlphaToCoverageEnabled() const
595{
596 return mState.sampleAlphaToCoverage;
597}
598
599void Context::setSampleCoverage(bool enabled)
600{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000601 if (mState.sampleCoverage != enabled)
602 {
603 mState.sampleCoverage = enabled;
604 mSampleStateDirty = true;
605 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000606}
607
608bool Context::isSampleCoverageEnabled() const
609{
610 return mState.sampleCoverage;
611}
612
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000613void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000614{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000615 if (mState.sampleCoverageValue != value ||
616 mState.sampleCoverageInvert != invert)
617 {
618 mState.sampleCoverageValue = value;
619 mState.sampleCoverageInvert = invert;
620 mSampleStateDirty = true;
621 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622}
623
624void Context::setScissorTest(bool enabled)
625{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000626 if (mState.scissorTest != enabled)
627 {
628 mState.scissorTest = enabled;
629 mScissorStateDirty = true;
630 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000631}
632
633bool Context::isScissorTestEnabled() const
634{
635 return mState.scissorTest;
636}
637
638void Context::setDither(bool enabled)
639{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000640 if (mState.dither != enabled)
641 {
642 mState.dither = enabled;
643 mDitherStateDirty = true;
644 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000645}
646
647bool Context::isDitherEnabled() const
648{
649 return mState.dither;
650}
651
652void Context::setLineWidth(GLfloat width)
653{
654 mState.lineWidth = width;
655}
656
657void Context::setGenerateMipmapHint(GLenum hint)
658{
659 mState.generateMipmapHint = hint;
660}
661
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000662void Context::setFragmentShaderDerivativeHint(GLenum hint)
663{
664 mState.fragmentShaderDerivativeHint = hint;
665 // TODO: Propagate the hint to shader translator so we can write
666 // ddx, ddx_coarse, or ddx_fine depending on the hint.
667 // Ignore for now. It is valid for implementations to ignore hint.
668}
669
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000670void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
671{
672 mState.viewportX = x;
673 mState.viewportY = y;
674 mState.viewportWidth = width;
675 mState.viewportHeight = height;
676}
677
678void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
679{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000680 if (mState.scissorX != x || mState.scissorY != y ||
681 mState.scissorWidth != width || mState.scissorHeight != height)
682 {
683 mState.scissorX = x;
684 mState.scissorY = y;
685 mState.scissorWidth = width;
686 mState.scissorHeight = height;
687 mScissorStateDirty = true;
688 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000689}
690
691void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
692{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000693 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
694 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
695 {
696 mState.colorMaskRed = red;
697 mState.colorMaskGreen = green;
698 mState.colorMaskBlue = blue;
699 mState.colorMaskAlpha = alpha;
700 mMaskStateDirty = true;
701 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000702}
703
704void Context::setDepthMask(bool mask)
705{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000706 if (mState.depthMask != mask)
707 {
708 mState.depthMask = mask;
709 mMaskStateDirty = true;
710 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000711}
712
713void Context::setActiveSampler(int active)
714{
715 mState.activeSampler = active;
716}
717
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000718GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000719{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000720 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000721}
722
723GLuint Context::getDrawFramebufferHandle() const
724{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000725 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000726}
727
728GLuint Context::getRenderbufferHandle() const
729{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000730 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000731}
732
733GLuint Context::getArrayBufferHandle() const
734{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000735 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000736}
737
daniel@transgaming.com83921382011-01-08 05:46:00 +0000738void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000740 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000741}
742
daniel@transgaming.com83921382011-01-08 05:46:00 +0000743const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000744{
745 return mState.vertexAttribute[attribNum];
746}
747
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000748void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749 GLsizei stride, const void *pointer)
750{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000751 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000752 mState.vertexAttribute[attribNum].mSize = size;
753 mState.vertexAttribute[attribNum].mType = type;
754 mState.vertexAttribute[attribNum].mNormalized = normalized;
755 mState.vertexAttribute[attribNum].mStride = stride;
756 mState.vertexAttribute[attribNum].mPointer = pointer;
757}
758
759const void *Context::getVertexAttribPointer(unsigned int attribNum) const
760{
761 return mState.vertexAttribute[attribNum].mPointer;
762}
763
daniel@transgaming.com83921382011-01-08 05:46:00 +0000764const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000765{
766 return mState.vertexAttribute;
767}
768
769void Context::setPackAlignment(GLint alignment)
770{
771 mState.packAlignment = alignment;
772}
773
774GLint Context::getPackAlignment() const
775{
776 return mState.packAlignment;
777}
778
779void Context::setUnpackAlignment(GLint alignment)
780{
781 mState.unpackAlignment = alignment;
782}
783
784GLint Context::getUnpackAlignment() const
785{
786 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787}
788
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789GLuint Context::createBuffer()
790{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000791 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792}
793
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794GLuint Context::createProgram()
795{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000796 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797}
798
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000799GLuint Context::createShader(GLenum type)
800{
801 return mResourceManager->createShader(type);
802}
803
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804GLuint Context::createTexture()
805{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000806 return mResourceManager->createTexture();
807}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000809GLuint Context::createRenderbuffer()
810{
811 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000814// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815GLuint Context::createFramebuffer()
816{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000817 unsigned int handle = 1;
818
819 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
820 {
821 handle++;
822 }
823
824 mFramebufferMap[handle] = NULL;
825
826 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827}
828
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000829GLuint Context::createFence()
830{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000831 unsigned int handle = 0;
832
833 while (mFenceMap.find(handle) != mFenceMap.end())
834 {
835 handle++;
836 }
837
838 mFenceMap[handle] = new Fence;
839
840 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000841}
842
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843void Context::deleteBuffer(GLuint buffer)
844{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000845 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846 {
847 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000849
850 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851}
852
853void Context::deleteShader(GLuint shader)
854{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000855 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
858void Context::deleteProgram(GLuint program)
859{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000860 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
863void Context::deleteTexture(GLuint texture)
864{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000865 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866 {
867 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869
870 mResourceManager->deleteTexture(texture);
871}
872
873void Context::deleteRenderbuffer(GLuint renderbuffer)
874{
875 if (mResourceManager->getRenderbuffer(renderbuffer))
876 {
877 detachRenderbuffer(renderbuffer);
878 }
879
880 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881}
882
883void Context::deleteFramebuffer(GLuint framebuffer)
884{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000885 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
886
887 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888 {
889 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000890
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000891 delete framebufferObject->second;
892 mFramebufferMap.erase(framebufferObject);
893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000895
896void Context::deleteFence(GLuint fence)
897{
898 FenceMap::iterator fenceObject = mFenceMap.find(fence);
899
900 if (fenceObject != mFenceMap.end())
901 {
902 delete fenceObject->second;
903 mFenceMap.erase(fenceObject);
904 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000905}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000909 return mResourceManager->getBuffer(handle);
910}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000912Shader *Context::getShader(GLuint handle)
913{
914 return mResourceManager->getShader(handle);
915}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000917Program *Context::getProgram(GLuint handle)
918{
919 return mResourceManager->getProgram(handle);
920}
921
922Texture *Context::getTexture(GLuint handle)
923{
924 return mResourceManager->getTexture(handle);
925}
926
927Renderbuffer *Context::getRenderbuffer(GLuint handle)
928{
929 return mResourceManager->getRenderbuffer(handle);
930}
931
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000932Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000933{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000934 return getFramebuffer(mState.readFramebuffer);
935}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000936
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000937Framebuffer *Context::getDrawFramebuffer()
938{
939 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940}
941
942void Context::bindArrayBuffer(unsigned int buffer)
943{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000944 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000946 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947}
948
949void Context::bindElementArrayBuffer(unsigned int buffer)
950{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000951 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000953 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954}
955
956void Context::bindTexture2D(GLuint texture)
957{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000958 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000960 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961}
962
963void Context::bindTextureCubeMap(GLuint texture)
964{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000965 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000967 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968}
969
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000970void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000972 if (!getFramebuffer(framebuffer))
973 {
974 mFramebufferMap[framebuffer] = new Framebuffer();
975 }
976
977 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000978}
979
980void Context::bindDrawFramebuffer(GLuint framebuffer)
981{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000982 if (!getFramebuffer(framebuffer))
983 {
984 mFramebufferMap[framebuffer] = new Framebuffer();
985 }
986
987 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988}
989
990void Context::bindRenderbuffer(GLuint renderbuffer)
991{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000992 mResourceManager->checkRenderbufferAllocation(renderbuffer);
993
994 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995}
996
997void Context::useProgram(GLuint program)
998{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000999 GLuint priorProgram = mState.currentProgram;
1000 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001001
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001002 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001004 Program *newProgram = mResourceManager->getProgram(program);
1005 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1006
1007 if (newProgram)
1008 {
1009 newProgram->addRef();
1010 }
1011
1012 if (oldProgram)
1013 {
1014 oldProgram->release();
1015 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017}
1018
1019void Context::setFramebufferZero(Framebuffer *buffer)
1020{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001021 delete mFramebufferMap[0];
1022 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023}
1024
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001025void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001027 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1028 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001031Framebuffer *Context::getFramebuffer(unsigned int handle)
1032{
1033 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1034
1035 if (framebuffer == mFramebufferMap.end())
1036 {
1037 return NULL;
1038 }
1039 else
1040 {
1041 return framebuffer->second;
1042 }
1043}
1044
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001045Fence *Context::getFence(unsigned int handle)
1046{
1047 FenceMap::iterator fence = mFenceMap.find(handle);
1048
1049 if (fence == mFenceMap.end())
1050 {
1051 return NULL;
1052 }
1053 else
1054 {
1055 return fence->second;
1056 }
1057}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001058
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059Buffer *Context::getArrayBuffer()
1060{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001061 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062}
1063
1064Buffer *Context::getElementArrayBuffer()
1065{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001066 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067}
1068
1069Program *Context::getCurrentProgram()
1070{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001071 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072}
1073
1074Texture2D *Context::getTexture2D()
1075{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001076 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, SAMPLER_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
1079TextureCubeMap *Context::getTextureCubeMap()
1080{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001081 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, SAMPLER_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082}
1083
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001084Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001086 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001087
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001088 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001089 {
1090 switch (type)
1091 {
1092 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001093 case SAMPLER_2D: return mTexture2DZero.get();
1094 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001095 }
1096 }
1097
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001098 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001101bool Context::getBooleanv(GLenum pname, GLboolean *params)
1102{
1103 switch (pname)
1104 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001105 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1106 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1107 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001108 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001109 params[0] = mState.colorMaskRed;
1110 params[1] = mState.colorMaskGreen;
1111 params[2] = mState.colorMaskBlue;
1112 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001113 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001114 case GL_CULL_FACE: *params = mState.cullFace; break;
1115 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1116 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1117 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1118 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1119 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1120 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1121 case GL_BLEND: *params = mState.blend; break;
1122 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001123 default:
1124 return false;
1125 }
1126
1127 return true;
1128}
1129
1130bool Context::getFloatv(GLenum pname, GLfloat *params)
1131{
1132 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1133 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1134 // GetIntegerv as its native query function. As it would require conversion in any
1135 // case, this should make no difference to the calling application.
1136 switch (pname)
1137 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001138 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1139 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1140 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1141 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1142 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001143 case GL_ALIASED_LINE_WIDTH_RANGE:
1144 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1145 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1146 break;
1147 case GL_ALIASED_POINT_SIZE_RANGE:
1148 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001149 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 +00001150 break;
1151 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001152 params[0] = mState.zNear;
1153 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001154 break;
1155 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001156 params[0] = mState.colorClearValue.red;
1157 params[1] = mState.colorClearValue.green;
1158 params[2] = mState.colorClearValue.blue;
1159 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001160 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001161 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001162 params[0] = mState.blendColor.red;
1163 params[1] = mState.blendColor.green;
1164 params[2] = mState.blendColor.blue;
1165 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001166 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001167 default:
1168 return false;
1169 }
1170
1171 return true;
1172}
1173
1174bool Context::getIntegerv(GLenum pname, GLint *params)
1175{
1176 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1177 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1178 // GetIntegerv as its native query function. As it would require conversion in any
1179 // case, this should make no difference to the calling application. You may find it in
1180 // Context::getFloatv.
1181 switch (pname)
1182 {
1183 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1184 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001185 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001186 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1187 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1188 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001189 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001190 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001191 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001192 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001193 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1194 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001195 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1196 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1197 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001198 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001199 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1200 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1201 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1202 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001203 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001204 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1205 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1206 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1207 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1208 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1209 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1210 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1211 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1212 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1213 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1214 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1215 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1216 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1217 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1218 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1219 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1220 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1221 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1222 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1223 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1224 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1225 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1226 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1227 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001228 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1229 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001230 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1231 {
1232 if (supportsCompressedTextures())
1233 {
1234 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1235 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1236 *params = 2;
1237 }
1238 else
1239 {
1240 *params = 0;
1241 }
1242 }
1243 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001244 case GL_MAX_SAMPLES_ANGLE:
1245 {
1246 GLsizei maxSamples = getMaxSupportedSamples();
1247 if (maxSamples != 0)
1248 {
1249 *params = maxSamples;
1250 }
1251 else
1252 {
1253 return false;
1254 }
1255
1256 break;
1257 }
1258 case GL_SAMPLE_BUFFERS:
1259 case GL_SAMPLES:
1260 {
1261 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1262 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1263 {
1264 switch (pname)
1265 {
1266 case GL_SAMPLE_BUFFERS:
1267 if (framebuffer->getSamples() != 0)
1268 {
1269 *params = 1;
1270 }
1271 else
1272 {
1273 *params = 0;
1274 }
1275 break;
1276 case GL_SAMPLES:
1277 *params = framebuffer->getSamples();
1278 break;
1279 }
1280 }
1281 else
1282 {
1283 *params = 0;
1284 }
1285 }
1286 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001287 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1288 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1289 case GL_MAX_VIEWPORT_DIMS:
1290 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001291 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001292 params[0] = maxDimension;
1293 params[1] = maxDimension;
1294 }
1295 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001296 case GL_COMPRESSED_TEXTURE_FORMATS:
1297 {
1298 if (supportsCompressedTextures())
1299 {
1300 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1301 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1302 }
1303 }
1304 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001305 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001306 params[0] = mState.viewportX;
1307 params[1] = mState.viewportY;
1308 params[2] = mState.viewportWidth;
1309 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001310 break;
1311 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001312 params[0] = mState.scissorX;
1313 params[1] = mState.scissorY;
1314 params[2] = mState.scissorWidth;
1315 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001316 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001317 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1318 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001319 case GL_RED_BITS:
1320 case GL_GREEN_BITS:
1321 case GL_BLUE_BITS:
1322 case GL_ALPHA_BITS:
1323 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001324 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001325 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1326
1327 if (colorbuffer)
1328 {
1329 switch (pname)
1330 {
1331 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1332 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1333 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1334 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1335 }
1336 }
1337 else
1338 {
1339 *params = 0;
1340 }
1341 }
1342 break;
1343 case GL_DEPTH_BITS:
1344 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001345 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001346 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001347
1348 if (depthbuffer)
1349 {
1350 *params = depthbuffer->getDepthSize();
1351 }
1352 else
1353 {
1354 *params = 0;
1355 }
1356 }
1357 break;
1358 case GL_STENCIL_BITS:
1359 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001360 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001361 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001362
1363 if (stencilbuffer)
1364 {
1365 *params = stencilbuffer->getStencilSize();
1366 }
1367 else
1368 {
1369 *params = 0;
1370 }
1371 }
1372 break;
1373 case GL_TEXTURE_BINDING_2D:
1374 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001375 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001376 {
1377 error(GL_INVALID_OPERATION);
1378 return false;
1379 }
1380
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001381 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001382 }
1383 break;
1384 case GL_TEXTURE_BINDING_CUBE_MAP:
1385 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001386 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001387 {
1388 error(GL_INVALID_OPERATION);
1389 return false;
1390 }
1391
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001392 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001393 }
1394 break;
1395 default:
1396 return false;
1397 }
1398
1399 return true;
1400}
1401
1402bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1403{
1404 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1405 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1406 // to the fact that it is stored internally as a float, and so would require conversion
1407 // if returned from Context::getIntegerv. Since this conversion is already implemented
1408 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1409 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1410 // application.
1411 switch (pname)
1412 {
1413 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1414 case GL_SHADER_BINARY_FORMATS:
1415 {
1416 *type = GL_INT;
1417 *numParams = 0;
1418 }
1419 break;
1420 case GL_MAX_VERTEX_ATTRIBS:
1421 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1422 case GL_MAX_VARYING_VECTORS:
1423 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1424 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1425 case GL_MAX_TEXTURE_IMAGE_UNITS:
1426 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1427 case GL_MAX_RENDERBUFFER_SIZE:
1428 case GL_NUM_SHADER_BINARY_FORMATS:
1429 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1430 case GL_ARRAY_BUFFER_BINDING:
1431 case GL_FRAMEBUFFER_BINDING:
1432 case GL_RENDERBUFFER_BINDING:
1433 case GL_CURRENT_PROGRAM:
1434 case GL_PACK_ALIGNMENT:
1435 case GL_UNPACK_ALIGNMENT:
1436 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001437 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001438 case GL_RED_BITS:
1439 case GL_GREEN_BITS:
1440 case GL_BLUE_BITS:
1441 case GL_ALPHA_BITS:
1442 case GL_DEPTH_BITS:
1443 case GL_STENCIL_BITS:
1444 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1445 case GL_CULL_FACE_MODE:
1446 case GL_FRONT_FACE:
1447 case GL_ACTIVE_TEXTURE:
1448 case GL_STENCIL_FUNC:
1449 case GL_STENCIL_VALUE_MASK:
1450 case GL_STENCIL_REF:
1451 case GL_STENCIL_FAIL:
1452 case GL_STENCIL_PASS_DEPTH_FAIL:
1453 case GL_STENCIL_PASS_DEPTH_PASS:
1454 case GL_STENCIL_BACK_FUNC:
1455 case GL_STENCIL_BACK_VALUE_MASK:
1456 case GL_STENCIL_BACK_REF:
1457 case GL_STENCIL_BACK_FAIL:
1458 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1459 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1460 case GL_DEPTH_FUNC:
1461 case GL_BLEND_SRC_RGB:
1462 case GL_BLEND_SRC_ALPHA:
1463 case GL_BLEND_DST_RGB:
1464 case GL_BLEND_DST_ALPHA:
1465 case GL_BLEND_EQUATION_RGB:
1466 case GL_BLEND_EQUATION_ALPHA:
1467 case GL_STENCIL_WRITEMASK:
1468 case GL_STENCIL_BACK_WRITEMASK:
1469 case GL_STENCIL_CLEAR_VALUE:
1470 case GL_SUBPIXEL_BITS:
1471 case GL_MAX_TEXTURE_SIZE:
1472 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1473 case GL_SAMPLE_BUFFERS:
1474 case GL_SAMPLES:
1475 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1476 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1477 case GL_TEXTURE_BINDING_2D:
1478 case GL_TEXTURE_BINDING_CUBE_MAP:
1479 {
1480 *type = GL_INT;
1481 *numParams = 1;
1482 }
1483 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001484 case GL_MAX_SAMPLES_ANGLE:
1485 {
1486 if (getMaxSupportedSamples() != 0)
1487 {
1488 *type = GL_INT;
1489 *numParams = 1;
1490 }
1491 else
1492 {
1493 return false;
1494 }
1495 }
1496 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001497 case GL_MAX_VIEWPORT_DIMS:
1498 {
1499 *type = GL_INT;
1500 *numParams = 2;
1501 }
1502 break;
1503 case GL_VIEWPORT:
1504 case GL_SCISSOR_BOX:
1505 {
1506 *type = GL_INT;
1507 *numParams = 4;
1508 }
1509 break;
1510 case GL_SHADER_COMPILER:
1511 case GL_SAMPLE_COVERAGE_INVERT:
1512 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001513 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1514 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1515 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1516 case GL_SAMPLE_COVERAGE:
1517 case GL_SCISSOR_TEST:
1518 case GL_STENCIL_TEST:
1519 case GL_DEPTH_TEST:
1520 case GL_BLEND:
1521 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001522 {
1523 *type = GL_BOOL;
1524 *numParams = 1;
1525 }
1526 break;
1527 case GL_COLOR_WRITEMASK:
1528 {
1529 *type = GL_BOOL;
1530 *numParams = 4;
1531 }
1532 break;
1533 case GL_POLYGON_OFFSET_FACTOR:
1534 case GL_POLYGON_OFFSET_UNITS:
1535 case GL_SAMPLE_COVERAGE_VALUE:
1536 case GL_DEPTH_CLEAR_VALUE:
1537 case GL_LINE_WIDTH:
1538 {
1539 *type = GL_FLOAT;
1540 *numParams = 1;
1541 }
1542 break;
1543 case GL_ALIASED_LINE_WIDTH_RANGE:
1544 case GL_ALIASED_POINT_SIZE_RANGE:
1545 case GL_DEPTH_RANGE:
1546 {
1547 *type = GL_FLOAT;
1548 *numParams = 2;
1549 }
1550 break;
1551 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001552 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001553 {
1554 *type = GL_FLOAT;
1555 *numParams = 4;
1556 }
1557 break;
1558 default:
1559 return false;
1560 }
1561
1562 return true;
1563}
1564
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001565// Applies the render target surface, depth stencil surface, viewport rectangle and
1566// scissor rectangle to the Direct3D 9 device
1567bool Context::applyRenderTarget(bool ignoreViewport)
1568{
1569 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001570
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001571 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001572
1573 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1574 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001575 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1576
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577 return false;
1578 }
1579
1580 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001581
1582 if (!renderTarget)
1583 {
1584 return false; // Context must be lost
1585 }
1586
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001587 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001589 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1590 if (renderTargetSerial != mAppliedRenderTargetSerial)
1591 {
1592 device->SetRenderTarget(0, renderTarget);
1593 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001594 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 +00001595 }
1596
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001597 unsigned int depthbufferSerial = 0;
1598 unsigned int stencilbufferSerial = 0;
1599 if (framebufferObject->getDepthbufferType() != GL_NONE)
1600 {
1601 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001602 if (!depthStencil)
1603 {
1604 ERR("Depth stencil pointer unexpectedly null.");
1605 return false;
1606 }
1607
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001608 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1609 }
1610 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1611 {
1612 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001613 if (!depthStencil)
1614 {
1615 ERR("Depth stencil pointer unexpectedly null.");
1616 return false;
1617 }
1618
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001619 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1620 }
1621
1622 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001623 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001624 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001625 {
1626 device->SetDepthStencilSurface(depthStencil);
1627 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001628 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001629 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001630 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001631
1632 D3DVIEWPORT9 viewport;
1633 D3DSURFACE_DESC desc;
1634 renderTarget->GetDesc(&desc);
1635
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001636 float zNear = clamp01(mState.zNear);
1637 float zFar = clamp01(mState.zFar);
1638
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639 if (ignoreViewport)
1640 {
1641 viewport.X = 0;
1642 viewport.Y = 0;
1643 viewport.Width = desc.Width;
1644 viewport.Height = desc.Height;
1645 viewport.MinZ = 0.0f;
1646 viewport.MaxZ = 1.0f;
1647 }
1648 else
1649 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001650 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1651 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1652 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1653 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1654 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 +00001655 viewport.MinZ = zNear;
1656 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657 }
1658
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001659 if (viewport.Width <= 0 || viewport.Height <= 0)
1660 {
1661 return false; // Nothing to render
1662 }
1663
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664 device->SetViewport(&viewport);
1665
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001666 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001668 if (mState.scissorTest)
1669 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001670 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1671 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1672 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1673 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1674 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001675 device->SetScissorRect(&rect);
1676 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1677 }
1678 else
1679 {
1680 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1681 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001682
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001683 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684 }
1685
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001686 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 Program *programObject = getCurrentProgram();
1689
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001690 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001691 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001692 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001693
daniel@transgaming.com31754962010-11-28 02:02:52 +00001694 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1696 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1697 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001698 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001699
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001700 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001701 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001702 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001703
daniel@transgaming.com31754962010-11-28 02:02:52 +00001704 GLint depthRange = programObject->getDxDepthRangeLocation();
1705 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1706 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707 }
1708
1709 return true;
1710}
1711
1712// 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 +00001713void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714{
1715 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001716 Program *programObject = getCurrentProgram();
1717
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001718 Framebuffer *framebufferObject = getDrawFramebuffer();
1719
1720 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1721
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001722 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001723 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001724 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001725
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001726 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001727 GLint alwaysFront = !isTriangleMode(drawMode);
1728 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1729
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001730 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001732 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001734 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 }
1736 else
1737 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001738 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 }
1740
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 mCullStateDirty = false;
1742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001744 if (mDepthStateDirty)
1745 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001746 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1749 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 }
1751 else
1752 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001753 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001755
1756 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 }
1758
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001759 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001761 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001762 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1764
1765 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1766 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1767 {
1768 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1769 }
1770 else
1771 {
1772 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1773 unorm<8>(mState.blendColor.alpha),
1774 unorm<8>(mState.blendColor.alpha),
1775 unorm<8>(mState.blendColor.alpha)));
1776 }
1777
1778 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1779 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1780 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1781
1782 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1783 mState.destBlendRGB != mState.destBlendAlpha ||
1784 mState.blendEquationRGB != mState.blendEquationAlpha)
1785 {
1786 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1787
1788 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1789 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1790 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1791
1792 }
1793 else
1794 {
1795 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1796 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001797 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001798 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001799 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001800 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001801 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001802
1803 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804 }
1805
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001806 if (mStencilStateDirty || mFrontFaceDirty)
1807 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001808 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001809 {
1810 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1811 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1812
1813 // FIXME: Unsupported by D3D9
1814 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1815 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1816 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1817 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1818 mState.stencilRef != mState.stencilBackRef ||
1819 mState.stencilMask != mState.stencilBackMask)
1820 {
1821 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1822 return error(GL_INVALID_OPERATION);
1823 }
1824
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001825 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001826 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001827 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1828
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001829 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1830 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001831 es2dx::ConvertComparison(mState.stencilFunc));
1832
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001833 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1834 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001835
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001836 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001837 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001838 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001839 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001840 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001841 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1842
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001843 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1844 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001845 es2dx::ConvertComparison(mState.stencilBackFunc));
1846
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001847 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1848 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001849
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001850 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001851 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001852 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001853 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001854 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001855 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1856 }
1857 else
1858 {
1859 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1860 }
1861
1862 mStencilStateDirty = false;
1863 }
1864
1865 if (mMaskStateDirty)
1866 {
1867 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1868 mState.colorMaskBlue, mState.colorMaskAlpha));
1869 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1870
1871 mMaskStateDirty = false;
1872 }
1873
1874 if (mPolygonOffsetStateDirty)
1875 {
1876 if (mState.polygonOffsetFill)
1877 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001878 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001879 if (depthbuffer)
1880 {
1881 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1882 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1883 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1884 }
1885 }
1886 else
1887 {
1888 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1889 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1890 }
1891
1892 mPolygonOffsetStateDirty = false;
1893 }
1894
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001895 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001897 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001898 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001899 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001900 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001901 FIXME("Sample alpha to coverage is unimplemented.");
1902 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001903
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001904 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1905 if (mState.sampleCoverage)
1906 {
1907 unsigned int mask = 0;
1908 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001909 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001910 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001911
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001912 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001913 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001914 mask <<= 1;
1915
1916 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1917 {
1918 threshold += 1.0f;
1919 mask |= 1;
1920 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001921 }
1922 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001923
1924 if (mState.sampleCoverageInvert)
1925 {
1926 mask = ~mask;
1927 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001928
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001929 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1930 }
1931 else
1932 {
1933 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1934 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001935 }
1936 else
1937 {
1938 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001939 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001940
1941 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942 }
1943
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001944 if (mDitherStateDirty)
1945 {
1946 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1947
1948 mDitherStateDirty = false;
1949 }
1950
1951 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952}
1953
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001954// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001955void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001956{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001957 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001958 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001959 if (attributes[i].active)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001961 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 }
1963 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001964}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965
daniel@transgaming.com83921382011-01-08 05:46:00 +00001966GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001967{
1968 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1969
daniel@transgaming.com83921382011-01-08 05:46:00 +00001970 GLenum err = mVertexDataManager->prepareVertexData(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001971 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001972 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001973 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001974 }
1975
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001976 lookupAttributeMapping(translated);
1977
daniel@transgaming.com83921382011-01-08 05:46:00 +00001978 mVertexDataManager->setupAttributes(translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001979
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001980 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001981}
1982
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001983// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001984GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001985{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001986 IDirect3DDevice9 *device = getDevice();
1987 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001988
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001989 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001990 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001991 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001992 }
1993
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001994 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001995}
1996
1997// Applies the shaders and shader constants to the Direct3D 9 device
1998void Context::applyShaders()
1999{
2000 IDirect3DDevice9 *device = getDevice();
2001 Program *programObject = getCurrentProgram();
2002 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2003 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2004
2005 device->SetVertexShader(vertexShader);
2006 device->SetPixelShader(pixelShader);
2007
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002008 if (programObject->getSerial() != mAppliedProgram)
2009 {
2010 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002011 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002012 mAppliedProgram = programObject->getSerial();
2013 }
2014
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015 programObject->applyUniforms();
2016}
2017
2018// Applies the textures and sampler states to the Direct3D 9 device
2019void Context::applyTextures()
2020{
2021 IDirect3DDevice9 *device = getDevice();
2022 Program *programObject = getCurrentProgram();
2023
2024 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2025 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002026 int textureUnit = programObject->getSamplerMapping(sampler);
2027 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002029 SamplerType textureType = programObject->getSamplerType(sampler);
2030
2031 Texture *texture = getSamplerTexture(textureUnit, textureType);
2032
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002033 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002034 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002035 if (texture->isComplete())
2036 {
2037 GLenum wrapS = texture->getWrapS();
2038 GLenum wrapT = texture->getWrapT();
2039 GLenum minFilter = texture->getMinFilter();
2040 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002041
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002042 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2043 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002044
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002045 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2046 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2047 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2048 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2049 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002051 device->SetTexture(sampler, texture->getTexture());
2052 }
2053 else
2054 {
2055 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2056 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002057 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002058
2059 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002060 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002061 else
2062 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002063 if (programObject->isSamplerDirty(sampler))
2064 {
2065 device->SetTexture(sampler, NULL);
2066 programObject->setSamplerDirty(sampler, false);
2067 }
2068 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 }
2070}
2071
2072void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2073{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002074 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002075
2076 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2077 {
2078 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2079 }
2080
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002081 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2082 {
2083 return error(GL_INVALID_OPERATION);
2084 }
2085
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002087
2088 if (!renderTarget)
2089 {
2090 return; // Context must be lost, return silently
2091 }
2092
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 IDirect3DDevice9 *device = getDevice();
2094
2095 D3DSURFACE_DESC desc;
2096 renderTarget->GetDesc(&desc);
2097
2098 IDirect3DSurface9 *systemSurface;
2099 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2100
2101 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2102 {
2103 return error(GL_OUT_OF_MEMORY);
2104 }
2105
2106 ASSERT(SUCCEEDED(result));
2107
2108 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2109 {
2110 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2111 }
2112
2113 result = device->GetRenderTargetData(renderTarget, systemSurface);
2114
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 if (FAILED(result))
2116 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117 systemSurface->Release();
2118
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002119 switch (result)
2120 {
2121 case D3DERR_DRIVERINTERNALERROR:
2122 case D3DERR_DEVICELOST:
2123 return error(GL_OUT_OF_MEMORY);
2124 default:
2125 UNREACHABLE();
2126 return; // No sensible error to generate
2127 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 }
2129
2130 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002131 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2132 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2133 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2134 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2135 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136
2137 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2138
2139 if (FAILED(result))
2140 {
2141 UNREACHABLE();
2142 systemSurface->Release();
2143
2144 return; // No sensible error to generate
2145 }
2146
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002147 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002149 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002150 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002151 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002153 for (int j = 0; j < rect.bottom - rect.top; j++)
2154 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002155 if (desc.Format == D3DFMT_A8R8G8B8 &&
2156 format == GL_BGRA_EXT &&
2157 type == GL_UNSIGNED_BYTE)
2158 {
2159 // Fast path for EXT_read_format_bgra, given
2160 // an RGBA source buffer. Note that buffers with no
2161 // alpha go through the slow path below.
2162 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002163 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002164 (rect.right - rect.left) * 4);
2165 continue;
2166 }
2167
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 for (int i = 0; i < rect.right - rect.left; i++)
2169 {
2170 float r;
2171 float g;
2172 float b;
2173 float a;
2174
2175 switch (desc.Format)
2176 {
2177 case D3DFMT_R5G6B5:
2178 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002179 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180
2181 a = 1.0f;
2182 b = (rgb & 0x001F) * (1.0f / 0x001F);
2183 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2184 r = (rgb & 0xF800) * (1.0f / 0xF800);
2185 }
2186 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002187 case D3DFMT_A1R5G5B5:
2188 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002189 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190
2191 a = (argb & 0x8000) ? 1.0f : 0.0f;
2192 b = (argb & 0x001F) * (1.0f / 0x001F);
2193 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2194 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2195 }
2196 break;
2197 case D3DFMT_A8R8G8B8:
2198 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002199 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002200
2201 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2202 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2203 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2204 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2205 }
2206 break;
2207 case D3DFMT_X8R8G8B8:
2208 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002209 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002210
2211 a = 1.0f;
2212 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2213 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2214 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2215 }
2216 break;
2217 case D3DFMT_A2R10G10B10:
2218 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002219 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220
2221 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2222 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2223 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2224 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2225 }
2226 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002227 case D3DFMT_A32B32G32R32F:
2228 {
2229 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002230 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2231 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2232 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2233 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002234 }
2235 break;
2236 case D3DFMT_A16B16G16R16F:
2237 {
2238 // float formats in D3D are stored rgba, rather than the other way round
2239 float abgr[4];
2240
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002241 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002242
2243 a = abgr[3];
2244 b = abgr[2];
2245 g = abgr[1];
2246 r = abgr[0];
2247 }
2248 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002249 default:
2250 UNIMPLEMENTED(); // FIXME
2251 UNREACHABLE();
2252 }
2253
2254 switch (format)
2255 {
2256 case GL_RGBA:
2257 switch (type)
2258 {
2259 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002260 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2261 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2262 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2263 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 break;
2265 default: UNREACHABLE();
2266 }
2267 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002268 case GL_BGRA_EXT:
2269 switch (type)
2270 {
2271 case GL_UNSIGNED_BYTE:
2272 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2273 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2274 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2275 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2276 break;
2277 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2278 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2279 // this type is packed as follows:
2280 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2281 // --------------------------------------------------------------------------------
2282 // | 4th | 3rd | 2nd | 1st component |
2283 // --------------------------------------------------------------------------------
2284 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2285 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2286 ((unsigned short)(15 * a + 0.5f) << 12)|
2287 ((unsigned short)(15 * r + 0.5f) << 8) |
2288 ((unsigned short)(15 * g + 0.5f) << 4) |
2289 ((unsigned short)(15 * b + 0.5f) << 0);
2290 break;
2291 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2292 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2293 // this type is packed as follows:
2294 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2295 // --------------------------------------------------------------------------------
2296 // | 4th | 3rd | 2nd | 1st component |
2297 // --------------------------------------------------------------------------------
2298 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2299 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2300 ((unsigned short)( a + 0.5f) << 15) |
2301 ((unsigned short)(31 * r + 0.5f) << 10) |
2302 ((unsigned short)(31 * g + 0.5f) << 5) |
2303 ((unsigned short)(31 * b + 0.5f) << 0);
2304 break;
2305 default: UNREACHABLE();
2306 }
2307 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002308 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002309 switch (type)
2310 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002311 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002312 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2313 ((unsigned short)(31 * b + 0.5f) << 0) |
2314 ((unsigned short)(63 * g + 0.5f) << 5) |
2315 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316 break;
2317 default: UNREACHABLE();
2318 }
2319 break;
2320 default: UNREACHABLE();
2321 }
2322 }
2323 }
2324
2325 systemSurface->UnlockRect();
2326
2327 systemSurface->Release();
2328}
2329
2330void Context::clear(GLbitfield mask)
2331{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002332 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002333
2334 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2335 {
2336 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2337
2338 return;
2339 }
2340
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002341 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002342 IDirect3DDevice9 *device = getDevice();
2343 DWORD flags = 0;
2344
2345 if (mask & GL_COLOR_BUFFER_BIT)
2346 {
2347 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002348
2349 if (framebufferObject->getColorbufferType() != GL_NONE)
2350 {
2351 flags |= D3DCLEAR_TARGET;
2352 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353 }
2354
2355 if (mask & GL_DEPTH_BUFFER_BIT)
2356 {
2357 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002358 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 {
2360 flags |= D3DCLEAR_ZBUFFER;
2361 }
2362 }
2363
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002364 GLuint stencilUnmasked = 0x0;
2365
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002366 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002367 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002369 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002371 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002372 if (!depthStencil)
2373 {
2374 ERR("Depth stencil pointer unexpectedly null.");
2375 return;
2376 }
2377
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002378 D3DSURFACE_DESC desc;
2379 depthStencil->GetDesc(&desc);
2380
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002381 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002382 stencilUnmasked = (0x1 << stencilSize) - 1;
2383
2384 if (stencilUnmasked != 0x0)
2385 {
2386 flags |= D3DCLEAR_STENCIL;
2387 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 }
2389 }
2390
2391 if (mask != 0)
2392 {
2393 return error(GL_INVALID_VALUE);
2394 }
2395
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002396 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2397 {
2398 return;
2399 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002401 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2402 unorm<8>(mState.colorClearValue.red),
2403 unorm<8>(mState.colorClearValue.green),
2404 unorm<8>(mState.colorClearValue.blue));
2405 float depth = clamp01(mState.depthClearValue);
2406 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407
2408 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2409
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002410 if (!renderTarget)
2411 {
2412 return; // Context must be lost, return silently
2413 }
2414
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415 D3DSURFACE_DESC desc;
2416 renderTarget->GetDesc(&desc);
2417
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002418 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419
2420 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002421 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002423 !(mState.colorMaskRed && mState.colorMaskGreen &&
2424 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425
2426 if (needMaskedColorClear || needMaskedStencilClear)
2427 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002428 // State which is altered in all paths from this point to the clear call is saved.
2429 // State which is altered in only some paths will be flagged dirty in the case that
2430 // that path is taken.
2431 HRESULT hr;
2432 if (mMaskedClearSavedState == NULL)
2433 {
2434 hr = device->BeginStateBlock();
2435 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2436
2437 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2438 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2439 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2440 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2441 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2442 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2443 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2444 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2445 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2446 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2447 device->SetPixelShader(NULL);
2448 device->SetVertexShader(NULL);
2449 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002450 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002451
2452 hr = device->EndStateBlock(&mMaskedClearSavedState);
2453 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2454 }
2455
2456 ASSERT(mMaskedClearSavedState != NULL);
2457
2458 if (mMaskedClearSavedState != NULL)
2459 {
2460 hr = mMaskedClearSavedState->Capture();
2461 ASSERT(SUCCEEDED(hr));
2462 }
2463
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2465 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2466 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2467 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2468 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2469 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2470 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2471 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2472
2473 if (flags & D3DCLEAR_TARGET)
2474 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002475 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2476 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2477 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2478 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479 }
2480 else
2481 {
2482 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2483 }
2484
2485 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2486 {
2487 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2488 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2489 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2490 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002491 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002492 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2494 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002495 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496 }
2497 else
2498 {
2499 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2500 }
2501
2502 device->SetPixelShader(NULL);
2503 device->SetVertexShader(NULL);
2504 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002505 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002507 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508 {
2509 float x, y, z, w;
2510 D3DCOLOR diffuse;
2511 };
2512
2513 Vertex quad[4];
2514 quad[0].x = 0.0f;
2515 quad[0].y = (float)desc.Height;
2516 quad[0].z = 0.0f;
2517 quad[0].w = 1.0f;
2518 quad[0].diffuse = color;
2519
2520 quad[1].x = (float)desc.Width;
2521 quad[1].y = (float)desc.Height;
2522 quad[1].z = 0.0f;
2523 quad[1].w = 1.0f;
2524 quad[1].diffuse = color;
2525
2526 quad[2].x = 0.0f;
2527 quad[2].y = 0.0f;
2528 quad[2].z = 0.0f;
2529 quad[2].w = 1.0f;
2530 quad[2].diffuse = color;
2531
2532 quad[3].x = (float)desc.Width;
2533 quad[3].y = 0.0f;
2534 quad[3].z = 0.0f;
2535 quad[3].w = 1.0f;
2536 quad[3].diffuse = color;
2537
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002538 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540
2541 if (flags & D3DCLEAR_ZBUFFER)
2542 {
2543 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2544 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2545 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2546 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002547
2548 if (mMaskedClearSavedState != NULL)
2549 {
2550 mMaskedClearSavedState->Apply();
2551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002553 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002554 {
2555 device->Clear(0, NULL, flags, color, depth, stencil);
2556 }
2557}
2558
2559void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2560{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002561 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 {
2563 return error(GL_INVALID_OPERATION);
2564 }
2565
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002566 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567 IDirect3DDevice9 *device = getDevice();
2568 D3DPRIMITIVETYPE primitiveType;
2569 int primitiveCount;
2570
2571 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2572 return error(GL_INVALID_ENUM);
2573
2574 if (primitiveCount <= 0)
2575 {
2576 return;
2577 }
2578
2579 if (!applyRenderTarget(false))
2580 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002581 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002582 }
2583
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002584 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002585
daniel@transgaming.com83921382011-01-08 05:46:00 +00002586 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002587 if (err != GL_NO_ERROR)
2588 {
2589 return error(err);
2590 }
2591
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592 applyShaders();
2593 applyTextures();
2594
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002595 if (!getCurrentProgram()->validateSamplers())
2596 {
2597 return error(GL_INVALID_OPERATION);
2598 }
2599
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002600 if (!cullSkipsDraw(mode))
2601 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002602 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002603
2604 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002605
2606 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2607 {
2608 drawClosingLine(first, first + count - 1);
2609 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002610 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611}
2612
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002613void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002615 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 {
2617 return error(GL_INVALID_OPERATION);
2618 }
2619
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002620 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621 {
2622 return error(GL_INVALID_OPERATION);
2623 }
2624
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002625 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626 IDirect3DDevice9 *device = getDevice();
2627 D3DPRIMITIVETYPE primitiveType;
2628 int primitiveCount;
2629
2630 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2631 return error(GL_INVALID_ENUM);
2632
2633 if (primitiveCount <= 0)
2634 {
2635 return;
2636 }
2637
2638 if (!applyRenderTarget(false))
2639 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002640 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641 }
2642
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002643 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002644
2645 TranslatedIndexData indexInfo;
2646 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2647 if (err != GL_NO_ERROR)
2648 {
2649 return error(err);
2650 }
2651
daniel@transgaming.com83921382011-01-08 05:46:00 +00002652 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2653 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002654 if (err != GL_NO_ERROR)
2655 {
2656 return error(err);
2657 }
2658
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002659 applyShaders();
2660 applyTextures();
2661
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002662 if (!getCurrentProgram()->validateSamplers())
2663 {
2664 return error(GL_INVALID_OPERATION);
2665 }
2666
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002667 if (!cullSkipsDraw(mode))
2668 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002669 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002670
daniel@transgaming.com83921382011-01-08 05:46:00 +00002671 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002672
2673 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2674 {
2675 drawClosingLine(count, type, indices);
2676 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002677 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002678}
2679
2680void Context::finish()
2681{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002682 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002683 IDirect3DDevice9 *device = getDevice();
2684 IDirect3DQuery9 *occlusionQuery = NULL;
2685
2686 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2687
2688 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2689 {
2690 return error(GL_OUT_OF_MEMORY);
2691 }
2692
2693 ASSERT(SUCCEEDED(result));
2694
2695 if (occlusionQuery)
2696 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002697 IDirect3DStateBlock9 *savedState = NULL;
2698 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2699
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002700 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2701 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702
2703 // Render something outside the render target
2704 device->SetPixelShader(NULL);
2705 device->SetVertexShader(NULL);
2706 device->SetFVF(D3DFVF_XYZRHW);
2707 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002708 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002709 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002711 result = occlusionQuery->Issue(D3DISSUE_END);
2712 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713
2714 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2715 {
2716 // Keep polling, but allow other threads to do something useful first
2717 Sleep(0);
2718 }
2719
2720 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002721
2722 if (savedState)
2723 {
2724 savedState->Apply();
2725 savedState->Release();
2726 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002727 }
2728}
2729
2730void Context::flush()
2731{
2732 IDirect3DDevice9 *device = getDevice();
2733 IDirect3DQuery9 *eventQuery = NULL;
2734
2735 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2736
2737 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2738 {
2739 return error(GL_OUT_OF_MEMORY);
2740 }
2741
2742 ASSERT(SUCCEEDED(result));
2743
2744 if (eventQuery)
2745 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002746 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2747 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002749 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002750 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002751
2752 if (result == D3DERR_DEVICELOST)
2753 {
2754 error(GL_OUT_OF_MEMORY);
2755 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002756 }
2757}
2758
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002759void Context::drawClosingLine(unsigned int first, unsigned int last)
2760{
2761 IDirect3DDevice9 *device = getDevice();
2762 IDirect3DIndexBuffer9 *indexBuffer = NULL;
2763 HRESULT result = D3DERR_INVALIDCALL;
2764
2765 if (supports32bitIndices())
2766 {
2767 result = device->CreateIndexBuffer(8, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &indexBuffer, 0);
2768
2769 if (SUCCEEDED(result))
2770 {
2771 unsigned int *data;
2772 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2773
2774 if (SUCCEEDED(result))
2775 {
2776 data[0] = last;
2777 data[1] = first;
2778 }
2779 }
2780 }
2781 else
2782 {
2783 result = device->CreateIndexBuffer(4, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexBuffer, 0);
2784
2785 if (SUCCEEDED(result))
2786 {
2787 unsigned short *data;
2788 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2789
2790 if (SUCCEEDED(result))
2791 {
2792 data[0] = last;
2793 data[1] = first;
2794 }
2795 }
2796 }
2797
2798 if (SUCCEEDED(result))
2799 {
2800 indexBuffer->Unlock();
2801 device->SetIndices(indexBuffer);
2802
2803 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, 0, 1);
2804
2805 indexBuffer->Release();
2806 }
2807 else
2808 {
2809 ERR("Could not create an index buffer for closing a line loop.");
2810 error(GL_OUT_OF_MEMORY);
2811 }
2812}
2813
2814void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2815{
2816 unsigned int first = 0;
2817 unsigned int last = 0;
2818
2819 if (mState.elementArrayBuffer.get())
2820 {
2821 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2822 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2823 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2824 }
2825
2826 switch (type)
2827 {
2828 case GL_UNSIGNED_BYTE:
2829 first = static_cast<const GLubyte*>(indices)[0];
2830 last = static_cast<const GLubyte*>(indices)[count - 1];
2831 break;
2832 case GL_UNSIGNED_SHORT:
2833 first = static_cast<const GLushort*>(indices)[0];
2834 last = static_cast<const GLushort*>(indices)[count - 1];
2835 break;
2836 case GL_UNSIGNED_INT:
2837 first = static_cast<const GLuint*>(indices)[0];
2838 last = static_cast<const GLuint*>(indices)[count - 1];
2839 break;
2840 default: UNREACHABLE();
2841 }
2842
2843 drawClosingLine(first, last);
2844}
2845
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002846void Context::recordInvalidEnum()
2847{
2848 mInvalidEnum = true;
2849}
2850
2851void Context::recordInvalidValue()
2852{
2853 mInvalidValue = true;
2854}
2855
2856void Context::recordInvalidOperation()
2857{
2858 mInvalidOperation = true;
2859}
2860
2861void Context::recordOutOfMemory()
2862{
2863 mOutOfMemory = true;
2864}
2865
2866void Context::recordInvalidFramebufferOperation()
2867{
2868 mInvalidFramebufferOperation = true;
2869}
2870
2871// Get one of the recorded errors and clear its flag, if any.
2872// [OpenGL ES 2.0.24] section 2.5 page 13.
2873GLenum Context::getError()
2874{
2875 if (mInvalidEnum)
2876 {
2877 mInvalidEnum = false;
2878
2879 return GL_INVALID_ENUM;
2880 }
2881
2882 if (mInvalidValue)
2883 {
2884 mInvalidValue = false;
2885
2886 return GL_INVALID_VALUE;
2887 }
2888
2889 if (mInvalidOperation)
2890 {
2891 mInvalidOperation = false;
2892
2893 return GL_INVALID_OPERATION;
2894 }
2895
2896 if (mOutOfMemory)
2897 {
2898 mOutOfMemory = false;
2899
2900 return GL_OUT_OF_MEMORY;
2901 }
2902
2903 if (mInvalidFramebufferOperation)
2904 {
2905 mInvalidFramebufferOperation = false;
2906
2907 return GL_INVALID_FRAMEBUFFER_OPERATION;
2908 }
2909
2910 return GL_NO_ERROR;
2911}
2912
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002913bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002914{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002915 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002916}
2917
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002918int Context::getMaximumVaryingVectors() const
2919{
2920 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2921}
2922
daniel@transgaming.com458da142010-11-28 02:03:02 +00002923int Context::getMaximumFragmentUniformVectors() const
2924{
2925 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2926}
2927
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002928int Context::getMaxSupportedSamples() const
2929{
2930 return mMaxSupportedSamples;
2931}
2932
2933int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2934{
2935 if (requested == 0)
2936 {
2937 return requested;
2938 }
2939
2940 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2941 if (itr == mMultiSampleSupport.end())
2942 {
2943 return -1;
2944 }
2945
2946 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2947 {
2948 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2949 {
2950 return i;
2951 }
2952 }
2953
2954 return -1;
2955}
2956
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002957bool Context::supportsEventQueries() const
2958{
2959 return mSupportsEventQueries;
2960}
2961
daniel@transgaming.com01868132010-08-24 19:21:17 +00002962bool Context::supportsCompressedTextures() const
2963{
2964 return mSupportsCompressedTextures;
2965}
2966
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002967bool Context::supportsFloatTextures() const
2968{
2969 return mSupportsFloatTextures;
2970}
2971
2972bool Context::supportsFloatLinearFilter() const
2973{
2974 return mSupportsFloatLinearFilter;
2975}
2976
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002977bool Context::supportsFloatRenderableTextures() const
2978{
2979 return mSupportsFloatRenderableTextures;
2980}
2981
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002982bool Context::supportsHalfFloatTextures() const
2983{
2984 return mSupportsHalfFloatTextures;
2985}
2986
2987bool Context::supportsHalfFloatLinearFilter() const
2988{
2989 return mSupportsHalfFloatLinearFilter;
2990}
2991
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002992bool Context::supportsHalfFloatRenderableTextures() const
2993{
2994 return mSupportsHalfFloatRenderableTextures;
2995}
2996
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00002997int Context::getMaximumRenderbufferDimension() const
2998{
2999 return mMaxRenderbufferDimension;
3000}
3001
3002int Context::getMaximumTextureDimension() const
3003{
3004 return mMaxTextureDimension;
3005}
3006
3007int Context::getMaximumCubeTextureDimension() const
3008{
3009 return mMaxCubeTextureDimension;
3010}
3011
3012int Context::getMaximumTextureLevel() const
3013{
3014 return mMaxTextureLevel;
3015}
3016
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003017bool Context::supportsLuminanceTextures() const
3018{
3019 return mSupportsLuminanceTextures;
3020}
3021
3022bool Context::supportsLuminanceAlphaTextures() const
3023{
3024 return mSupportsLuminanceAlphaTextures;
3025}
3026
daniel@transgaming.com83921382011-01-08 05:46:00 +00003027bool Context::supports32bitIndices() const
3028{
3029 return mSupports32bitIndices;
3030}
3031
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003032void Context::detachBuffer(GLuint buffer)
3033{
3034 // [OpenGL ES 2.0.24] section 2.9 page 22:
3035 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3036 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3037
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003038 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003039 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003040 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003041 }
3042
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003043 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003044 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003045 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003046 }
3047
3048 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3049 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003050 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003051 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003052 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003053 }
3054 }
3055}
3056
3057void Context::detachTexture(GLuint texture)
3058{
3059 // [OpenGL ES 2.0.24] section 3.8 page 84:
3060 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3061 // rebound to texture object zero
3062
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003063 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003064 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003065 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003066 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003067 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003068 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003069 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003070 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003071 }
3072 }
3073
3074 // [OpenGL ES 2.0.24] section 4.4 page 112:
3075 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3076 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3077 // image was attached in the currently bound framebuffer.
3078
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003079 Framebuffer *readFramebuffer = getReadFramebuffer();
3080 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003081
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003082 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003083 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003084 readFramebuffer->detachTexture(texture);
3085 }
3086
3087 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3088 {
3089 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003090 }
3091}
3092
3093void Context::detachFramebuffer(GLuint framebuffer)
3094{
3095 // [OpenGL ES 2.0.24] section 4.4 page 107:
3096 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3097 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3098
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003099 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003100 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003101 bindReadFramebuffer(0);
3102 }
3103
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003104 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003105 {
3106 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003107 }
3108}
3109
3110void Context::detachRenderbuffer(GLuint renderbuffer)
3111{
3112 // [OpenGL ES 2.0.24] section 4.4 page 109:
3113 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3114 // had been executed with the target RENDERBUFFER and name of zero.
3115
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003116 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003117 {
3118 bindRenderbuffer(0);
3119 }
3120
3121 // [OpenGL ES 2.0.24] section 4.4 page 111:
3122 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3123 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3124 // point to which this image was attached in the currently bound framebuffer.
3125
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003126 Framebuffer *readFramebuffer = getReadFramebuffer();
3127 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003128
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003129 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003130 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003131 readFramebuffer->detachRenderbuffer(renderbuffer);
3132 }
3133
3134 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3135 {
3136 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003137 }
3138}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003139
3140Texture *Context::getIncompleteTexture(SamplerType type)
3141{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003142 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003143
3144 if (t == NULL)
3145 {
3146 static const GLubyte color[] = { 0, 0, 0, 255 };
3147
3148 switch (type)
3149 {
3150 default:
3151 UNREACHABLE();
3152 // default falls through to SAMPLER_2D
3153
3154 case SAMPLER_2D:
3155 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003156 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003157 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003158 t = incomplete2d;
3159 }
3160 break;
3161
3162 case SAMPLER_CUBE:
3163 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003164 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003165
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003166 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3167 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3168 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3169 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3170 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3171 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003172
3173 t = incompleteCube;
3174 }
3175 break;
3176 }
3177
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003178 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003179 }
3180
3181 return t;
3182}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003183
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003184bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003185{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003186 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003187}
3188
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003189bool Context::isTriangleMode(GLenum drawMode)
3190{
3191 switch (drawMode)
3192 {
3193 case GL_TRIANGLES:
3194 case GL_TRIANGLE_FAN:
3195 case GL_TRIANGLE_STRIP:
3196 return true;
3197 case GL_POINTS:
3198 case GL_LINES:
3199 case GL_LINE_LOOP:
3200 case GL_LINE_STRIP:
3201 return false;
3202 default: UNREACHABLE();
3203 }
3204
3205 return false;
3206}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003207
3208void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3209{
3210 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3211
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003212 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3213 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3214 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3215 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003216
daniel@transgaming.com83921382011-01-08 05:46:00 +00003217 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003218}
3219
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003220void Context::initExtensionString()
3221{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003222 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003223 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3224 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003225 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003226 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003227 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003228
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003229 if (supportsEventQueries())
3230 {
3231 mExtensionString += "GL_NV_fence ";
3232 }
3233
daniel@transgaming.com01868132010-08-24 19:21:17 +00003234 if (supportsCompressedTextures())
3235 {
3236 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3237 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003238
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003239 if (supportsFloatTextures())
3240 {
3241 mExtensionString += "GL_OES_texture_float ";
3242 }
3243
3244 if (supportsHalfFloatTextures())
3245 {
3246 mExtensionString += "GL_OES_texture_half_float ";
3247 }
3248
3249 if (supportsFloatLinearFilter())
3250 {
3251 mExtensionString += "GL_OES_texture_float_linear ";
3252 }
3253
3254 if (supportsHalfFloatLinearFilter())
3255 {
3256 mExtensionString += "GL_OES_texture_half_float_linear ";
3257 }
3258
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003259 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003260 {
3261 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3262 }
3263
daniel@transgaming.com83921382011-01-08 05:46:00 +00003264 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003265 {
3266 mExtensionString += "GL_OES_element_index_uint ";
3267 }
3268
3269 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3270 if (end != std::string::npos)
3271 {
3272 mExtensionString.resize(end+1);
3273 }
3274}
3275
3276const char *Context::getExtensionString() const
3277{
3278 return mExtensionString.c_str();
3279}
3280
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003281void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3282 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3283 GLbitfield mask)
3284{
3285 IDirect3DDevice9 *device = getDevice();
3286
3287 Framebuffer *readFramebuffer = getReadFramebuffer();
3288 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3289
3290 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3291 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3292 {
3293 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3294 }
3295
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003296 if (drawFramebuffer->getSamples() != 0)
3297 {
3298 return error(GL_INVALID_OPERATION);
3299 }
3300
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003301 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3302 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3303 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3304 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3305
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003306 RECT sourceRect;
3307 RECT destRect;
3308
3309 if (srcX0 < srcX1)
3310 {
3311 sourceRect.left = srcX0;
3312 sourceRect.right = srcX1;
3313 destRect.left = dstX0;
3314 destRect.right = dstX1;
3315 }
3316 else
3317 {
3318 sourceRect.left = srcX1;
3319 destRect.left = dstX1;
3320 sourceRect.right = srcX0;
3321 destRect.right = dstX0;
3322 }
3323
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003324 if (srcY0 < srcY1)
3325 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003326 sourceRect.top = readBufferHeight - srcY1;
3327 destRect.top = drawBufferHeight - dstY1;
3328 sourceRect.bottom = readBufferHeight - srcY0;
3329 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003330 }
3331 else
3332 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003333 sourceRect.top = readBufferHeight - srcY0;
3334 destRect.top = drawBufferHeight - dstY0;
3335 sourceRect.bottom = readBufferHeight - srcY1;
3336 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003337 }
3338
3339 RECT sourceScissoredRect = sourceRect;
3340 RECT destScissoredRect = destRect;
3341
3342 if (mState.scissorTest)
3343 {
3344 // Only write to parts of the destination framebuffer which pass the scissor test
3345 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3346 // rect will be checked against scissorY, rather than the bottom.
3347 if (destRect.left < mState.scissorX)
3348 {
3349 int xDiff = mState.scissorX - destRect.left;
3350 destScissoredRect.left = mState.scissorX;
3351 sourceScissoredRect.left += xDiff;
3352 }
3353
3354 if (destRect.right > mState.scissorX + mState.scissorWidth)
3355 {
3356 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3357 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3358 sourceScissoredRect.right -= xDiff;
3359 }
3360
3361 if (destRect.top < mState.scissorY)
3362 {
3363 int yDiff = mState.scissorY - destRect.top;
3364 destScissoredRect.top = mState.scissorY;
3365 sourceScissoredRect.top += yDiff;
3366 }
3367
3368 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3369 {
3370 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3371 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3372 sourceScissoredRect.bottom -= yDiff;
3373 }
3374 }
3375
3376 bool blitRenderTarget = false;
3377 bool blitDepthStencil = false;
3378
3379 RECT sourceTrimmedRect = sourceScissoredRect;
3380 RECT destTrimmedRect = destScissoredRect;
3381
3382 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3383 // the actual draw and read surfaces.
3384 if (sourceTrimmedRect.left < 0)
3385 {
3386 int xDiff = 0 - sourceTrimmedRect.left;
3387 sourceTrimmedRect.left = 0;
3388 destTrimmedRect.left += xDiff;
3389 }
3390
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003391 if (sourceTrimmedRect.right > readBufferWidth)
3392 {
3393 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3394 sourceTrimmedRect.right = readBufferWidth;
3395 destTrimmedRect.right -= xDiff;
3396 }
3397
3398 if (sourceTrimmedRect.top < 0)
3399 {
3400 int yDiff = 0 - sourceTrimmedRect.top;
3401 sourceTrimmedRect.top = 0;
3402 destTrimmedRect.top += yDiff;
3403 }
3404
3405 if (sourceTrimmedRect.bottom > readBufferHeight)
3406 {
3407 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3408 sourceTrimmedRect.bottom = readBufferHeight;
3409 destTrimmedRect.bottom -= yDiff;
3410 }
3411
3412 if (destTrimmedRect.left < 0)
3413 {
3414 int xDiff = 0 - destTrimmedRect.left;
3415 destTrimmedRect.left = 0;
3416 sourceTrimmedRect.left += xDiff;
3417 }
3418
3419 if (destTrimmedRect.right > drawBufferWidth)
3420 {
3421 int xDiff = destTrimmedRect.right - drawBufferWidth;
3422 destTrimmedRect.right = drawBufferWidth;
3423 sourceTrimmedRect.right -= xDiff;
3424 }
3425
3426 if (destTrimmedRect.top < 0)
3427 {
3428 int yDiff = 0 - destTrimmedRect.top;
3429 destTrimmedRect.top = 0;
3430 sourceTrimmedRect.top += yDiff;
3431 }
3432
3433 if (destTrimmedRect.bottom > drawBufferHeight)
3434 {
3435 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3436 destTrimmedRect.bottom = drawBufferHeight;
3437 sourceTrimmedRect.bottom -= yDiff;
3438 }
3439
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003440 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003441 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3442 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3443 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3444 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003445 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3446 {
3447 partialBufferCopy = true;
3448 }
3449
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003450 if (mask & GL_COLOR_BUFFER_BIT)
3451 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003452 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3453 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3454 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3455 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3456 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003457 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3458 {
3459 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3460 return error(GL_INVALID_OPERATION);
3461 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003462
3463 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3464 {
3465 return error(GL_INVALID_OPERATION);
3466 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003467
3468 blitRenderTarget = true;
3469
3470 }
3471
3472 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3473 {
3474 DepthStencilbuffer *readDSBuffer = NULL;
3475 DepthStencilbuffer *drawDSBuffer = NULL;
3476
3477 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3478 // both a depth and stencil buffer, it will be the same buffer.
3479
3480 if (mask & GL_DEPTH_BUFFER_BIT)
3481 {
3482 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3483 {
3484 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3485 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3486 {
3487 return error(GL_INVALID_OPERATION);
3488 }
3489
3490 blitDepthStencil = true;
3491 readDSBuffer = readFramebuffer->getDepthbuffer();
3492 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3493 }
3494 }
3495
3496 if (mask & GL_STENCIL_BUFFER_BIT)
3497 {
3498 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3499 {
3500 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3501 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3502 {
3503 return error(GL_INVALID_OPERATION);
3504 }
3505
3506 blitDepthStencil = true;
3507 readDSBuffer = readFramebuffer->getStencilbuffer();
3508 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3509 }
3510 }
3511
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003512 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003513 {
3514 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3515 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3516 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003517
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003518 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3519 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003520 {
3521 return error(GL_INVALID_OPERATION);
3522 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003523 }
3524
3525 if (blitRenderTarget || blitDepthStencil)
3526 {
3527 egl::Display *display = getDisplay();
3528 display->endScene();
3529
3530 if (blitRenderTarget)
3531 {
3532 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3533 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3534
3535 if (FAILED(result))
3536 {
3537 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3538 return;
3539 }
3540 }
3541
3542 if (blitDepthStencil)
3543 {
3544 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3545
3546 if (FAILED(result))
3547 {
3548 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3549 return;
3550 }
3551 }
3552 }
3553}
3554
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003555}
3556
3557extern "C"
3558{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003559gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003560{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003561 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003562}
3563
3564void glDestroyContext(gl::Context *context)
3565{
3566 delete context;
3567
3568 if (context == gl::getContext())
3569 {
3570 gl::makeCurrent(NULL, NULL, NULL);
3571 }
3572}
3573
3574void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3575{
3576 gl::makeCurrent(context, display, surface);
3577}
3578
3579gl::Context *glGetCurrentContext()
3580{
3581 return gl::getContext();
3582}
3583}