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