blob: 67895dbec61feb54575bf1cafc54b16f1375b80e [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.combaa74512011-04-13 14:56:47 +000037 : mConfig(config), mMaxLru(0)
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.combaa74512011-04-13 14:56:47 +0000157
158 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
159 {
160 mVertexDeclCache[i].vertexDeclaration = NULL;
161 mVertexDeclCache[i].lruCount = 0;
162 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163}
164
165Context::~Context()
166{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000167 if (mState.currentProgram != 0)
168 {
169 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
170 if (programObject)
171 {
172 programObject->release();
173 }
174 mState.currentProgram = 0;
175 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000176
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000177 while (!mFramebufferMap.empty())
178 {
179 deleteFramebuffer(mFramebufferMap.begin()->first);
180 }
181
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000182 while (!mFenceMap.empty())
183 {
184 deleteFence(mFenceMap.begin()->first);
185 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000186
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000187 while (!mMultiSampleSupport.empty())
188 {
189 delete [] mMultiSampleSupport.begin()->second;
190 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
191 }
192
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000193 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
194 {
195 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
196 {
197 mState.samplerTexture[type][sampler].set(NULL);
198 }
199 }
200
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000201 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
202 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000203 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000204 }
205
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000206 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
207 {
208 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
209 }
210
211 mState.arrayBuffer.set(NULL);
212 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000213 mState.renderbuffer.set(NULL);
214
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000215 mTexture2DZero.set(NULL);
216 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000218 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000219 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000220 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000221
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000222 if (mMaskedClearSavedState)
223 {
224 mMaskedClearSavedState->Release();
225 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000226
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000227 mResourceManager->release();
daniel@transgaming.combaa74512011-04-13 14:56:47 +0000228
229 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
230 {
231 if (mVertexDeclCache[i].vertexDeclaration)
232 {
233 mVertexDeclCache[i].vertexDeclaration->Release();
234 }
235 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236}
237
238void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
239{
240 IDirect3DDevice9 *device = display->getDevice();
241
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000242 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000243 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000244 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000245
daniel@transgaming.com83921382011-01-08 05:46:00 +0000246 mVertexDataManager = new VertexDataManager(this, device);
247 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000248 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000249
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000250 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
251
252 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
253 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
254 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
255 mMaxRenderbufferDimension = mMaxTextureDimension;
256 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
257 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
258 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
259
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000260 const D3DFORMAT renderBufferFormats[] =
261 {
262 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000263 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000264 D3DFMT_R5G6B5,
265 D3DFMT_D24S8
266 };
267
268 int max = 0;
269 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
270 {
271 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
272 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
273 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
274
275 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
276 {
277 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
278 {
279 max = j;
280 }
281 }
282 }
283
284 mMaxSupportedSamples = max;
285
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000286 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000287 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000288 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
289 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000290 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
291 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000292
daniel@transgaming.com83921382011-01-08 05:46:00 +0000293 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
294
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000295 initExtensionString();
296
297 mState.viewportX = 0;
298 mState.viewportY = 0;
299 mState.viewportWidth = surface->getWidth();
300 mState.viewportHeight = surface->getHeight();
301
302 mState.scissorX = 0;
303 mState.scissorY = 0;
304 mState.scissorWidth = surface->getWidth();
305 mState.scissorHeight = surface->getHeight();
306
307 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000308 }
309
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
311 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000312 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000315 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000316 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317
318 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000320 if (defaultRenderTarget)
321 {
322 defaultRenderTarget->Release();
323 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000325 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000327 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000329
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000330 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331}
332
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000333// 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 +0000334void Context::markAllStateDirty()
335{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000336 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
337 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000338 mAppliedTextureSerial[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000339 }
340
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000341 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000342 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000343 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000344 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000345 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000346
347 mClearStateDirty = true;
348 mCullStateDirty = true;
349 mDepthStateDirty = true;
350 mMaskStateDirty = true;
351 mBlendStateDirty = true;
352 mStencilStateDirty = true;
353 mPolygonOffsetStateDirty = true;
354 mScissorStateDirty = true;
355 mSampleStateDirty = true;
356 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000357 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000358}
359
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360void Context::setClearColor(float red, float green, float blue, float alpha)
361{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000362 mState.colorClearValue.red = red;
363 mState.colorClearValue.green = green;
364 mState.colorClearValue.blue = blue;
365 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366}
367
368void Context::setClearDepth(float depth)
369{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000370 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371}
372
373void Context::setClearStencil(int stencil)
374{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000375 mState.stencilClearValue = stencil;
376}
377
378void Context::setCullFace(bool enabled)
379{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000380 if (mState.cullFace != enabled)
381 {
382 mState.cullFace = enabled;
383 mCullStateDirty = true;
384 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000385}
386
387bool Context::isCullFaceEnabled() const
388{
389 return mState.cullFace;
390}
391
392void Context::setCullMode(GLenum mode)
393{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000394 if (mState.cullMode != mode)
395 {
396 mState.cullMode = mode;
397 mCullStateDirty = true;
398 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000399}
400
401void Context::setFrontFace(GLenum front)
402{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000403 if (mState.frontFace != front)
404 {
405 mState.frontFace = front;
406 mFrontFaceDirty = true;
407 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000408}
409
410void Context::setDepthTest(bool enabled)
411{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000412 if (mState.depthTest != enabled)
413 {
414 mState.depthTest = enabled;
415 mDepthStateDirty = true;
416 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000417}
418
419bool Context::isDepthTestEnabled() const
420{
421 return mState.depthTest;
422}
423
424void Context::setDepthFunc(GLenum depthFunc)
425{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000426 if (mState.depthFunc != depthFunc)
427 {
428 mState.depthFunc = depthFunc;
429 mDepthStateDirty = true;
430 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000431}
432
433void Context::setDepthRange(float zNear, float zFar)
434{
435 mState.zNear = zNear;
436 mState.zFar = zFar;
437}
438
439void Context::setBlend(bool enabled)
440{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000441 if (mState.blend != enabled)
442 {
443 mState.blend = enabled;
444 mBlendStateDirty = true;
445 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000446}
447
448bool Context::isBlendEnabled() const
449{
450 return mState.blend;
451}
452
453void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
454{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000455 if (mState.sourceBlendRGB != sourceRGB ||
456 mState.sourceBlendAlpha != sourceAlpha ||
457 mState.destBlendRGB != destRGB ||
458 mState.destBlendAlpha != destAlpha)
459 {
460 mState.sourceBlendRGB = sourceRGB;
461 mState.destBlendRGB = destRGB;
462 mState.sourceBlendAlpha = sourceAlpha;
463 mState.destBlendAlpha = destAlpha;
464 mBlendStateDirty = true;
465 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000466}
467
468void Context::setBlendColor(float red, float green, float blue, float alpha)
469{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000470 if (mState.blendColor.red != red ||
471 mState.blendColor.green != green ||
472 mState.blendColor.blue != blue ||
473 mState.blendColor.alpha != alpha)
474 {
475 mState.blendColor.red = red;
476 mState.blendColor.green = green;
477 mState.blendColor.blue = blue;
478 mState.blendColor.alpha = alpha;
479 mBlendStateDirty = true;
480 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000481}
482
483void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
484{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000485 if (mState.blendEquationRGB != rgbEquation ||
486 mState.blendEquationAlpha != alphaEquation)
487 {
488 mState.blendEquationRGB = rgbEquation;
489 mState.blendEquationAlpha = alphaEquation;
490 mBlendStateDirty = true;
491 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000492}
493
494void Context::setStencilTest(bool enabled)
495{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000496 if (mState.stencilTest != enabled)
497 {
498 mState.stencilTest = enabled;
499 mStencilStateDirty = true;
500 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000501}
502
503bool Context::isStencilTestEnabled() const
504{
505 return mState.stencilTest;
506}
507
508void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
509{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 if (mState.stencilFunc != stencilFunc ||
511 mState.stencilRef != stencilRef ||
512 mState.stencilMask != stencilMask)
513 {
514 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000515 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000516 mState.stencilMask = stencilMask;
517 mStencilStateDirty = true;
518 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000519}
520
521void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
522{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000523 if (mState.stencilBackFunc != stencilBackFunc ||
524 mState.stencilBackRef != stencilBackRef ||
525 mState.stencilBackMask != stencilBackMask)
526 {
527 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000528 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000529 mState.stencilBackMask = stencilBackMask;
530 mStencilStateDirty = true;
531 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000532}
533
534void Context::setStencilWritemask(GLuint stencilWritemask)
535{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000536 if (mState.stencilWritemask != stencilWritemask)
537 {
538 mState.stencilWritemask = stencilWritemask;
539 mStencilStateDirty = true;
540 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000541}
542
543void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
544{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000545 if (mState.stencilBackWritemask != stencilBackWritemask)
546 {
547 mState.stencilBackWritemask = stencilBackWritemask;
548 mStencilStateDirty = true;
549 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000550}
551
552void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
553{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000554 if (mState.stencilFail != stencilFail ||
555 mState.stencilPassDepthFail != stencilPassDepthFail ||
556 mState.stencilPassDepthPass != stencilPassDepthPass)
557 {
558 mState.stencilFail = stencilFail;
559 mState.stencilPassDepthFail = stencilPassDepthFail;
560 mState.stencilPassDepthPass = stencilPassDepthPass;
561 mStencilStateDirty = true;
562 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000563}
564
565void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
566{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000567 if (mState.stencilBackFail != stencilBackFail ||
568 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
569 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
570 {
571 mState.stencilBackFail = stencilBackFail;
572 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
573 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
574 mStencilStateDirty = true;
575 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000576}
577
578void Context::setPolygonOffsetFill(bool enabled)
579{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000580 if (mState.polygonOffsetFill != enabled)
581 {
582 mState.polygonOffsetFill = enabled;
583 mPolygonOffsetStateDirty = true;
584 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000585}
586
587bool Context::isPolygonOffsetFillEnabled() const
588{
589 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000590
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.polygonOffsetFactor != factor ||
596 mState.polygonOffsetUnits != units)
597 {
598 mState.polygonOffsetFactor = factor;
599 mState.polygonOffsetUnits = units;
600 mPolygonOffsetStateDirty = true;
601 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000602}
603
604void Context::setSampleAlphaToCoverage(bool enabled)
605{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000606 if (mState.sampleAlphaToCoverage != enabled)
607 {
608 mState.sampleAlphaToCoverage = enabled;
609 mSampleStateDirty = true;
610 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000611}
612
613bool Context::isSampleAlphaToCoverageEnabled() const
614{
615 return mState.sampleAlphaToCoverage;
616}
617
618void Context::setSampleCoverage(bool enabled)
619{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000620 if (mState.sampleCoverage != enabled)
621 {
622 mState.sampleCoverage = enabled;
623 mSampleStateDirty = true;
624 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000625}
626
627bool Context::isSampleCoverageEnabled() const
628{
629 return mState.sampleCoverage;
630}
631
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000632void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000633{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000634 if (mState.sampleCoverageValue != value ||
635 mState.sampleCoverageInvert != invert)
636 {
637 mState.sampleCoverageValue = value;
638 mState.sampleCoverageInvert = invert;
639 mSampleStateDirty = true;
640 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000641}
642
643void Context::setScissorTest(bool enabled)
644{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000645 if (mState.scissorTest != enabled)
646 {
647 mState.scissorTest = enabled;
648 mScissorStateDirty = true;
649 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000650}
651
652bool Context::isScissorTestEnabled() const
653{
654 return mState.scissorTest;
655}
656
657void Context::setDither(bool enabled)
658{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000659 if (mState.dither != enabled)
660 {
661 mState.dither = enabled;
662 mDitherStateDirty = true;
663 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000664}
665
666bool Context::isDitherEnabled() const
667{
668 return mState.dither;
669}
670
671void Context::setLineWidth(GLfloat width)
672{
673 mState.lineWidth = width;
674}
675
676void Context::setGenerateMipmapHint(GLenum hint)
677{
678 mState.generateMipmapHint = hint;
679}
680
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000681void Context::setFragmentShaderDerivativeHint(GLenum hint)
682{
683 mState.fragmentShaderDerivativeHint = hint;
684 // TODO: Propagate the hint to shader translator so we can write
685 // ddx, ddx_coarse, or ddx_fine depending on the hint.
686 // Ignore for now. It is valid for implementations to ignore hint.
687}
688
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000689void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
690{
691 mState.viewportX = x;
692 mState.viewportY = y;
693 mState.viewportWidth = width;
694 mState.viewportHeight = height;
695}
696
697void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
698{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000699 if (mState.scissorX != x || mState.scissorY != y ||
700 mState.scissorWidth != width || mState.scissorHeight != height)
701 {
702 mState.scissorX = x;
703 mState.scissorY = y;
704 mState.scissorWidth = width;
705 mState.scissorHeight = height;
706 mScissorStateDirty = true;
707 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000708}
709
710void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
711{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000712 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
713 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
714 {
715 mState.colorMaskRed = red;
716 mState.colorMaskGreen = green;
717 mState.colorMaskBlue = blue;
718 mState.colorMaskAlpha = alpha;
719 mMaskStateDirty = true;
720 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000721}
722
723void Context::setDepthMask(bool mask)
724{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000725 if (mState.depthMask != mask)
726 {
727 mState.depthMask = mask;
728 mMaskStateDirty = true;
729 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000730}
731
732void Context::setActiveSampler(int active)
733{
734 mState.activeSampler = active;
735}
736
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000737GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000738{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000739 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000740}
741
742GLuint Context::getDrawFramebufferHandle() const
743{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000744 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000745}
746
747GLuint Context::getRenderbufferHandle() const
748{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000749 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000750}
751
752GLuint Context::getArrayBufferHandle() const
753{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000754 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000755}
756
daniel@transgaming.com83921382011-01-08 05:46:00 +0000757void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000758{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000759 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000760}
761
daniel@transgaming.com83921382011-01-08 05:46:00 +0000762const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000763{
764 return mState.vertexAttribute[attribNum];
765}
766
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000767void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000768 GLsizei stride, const void *pointer)
769{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000770 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000771 mState.vertexAttribute[attribNum].mSize = size;
772 mState.vertexAttribute[attribNum].mType = type;
773 mState.vertexAttribute[attribNum].mNormalized = normalized;
774 mState.vertexAttribute[attribNum].mStride = stride;
775 mState.vertexAttribute[attribNum].mPointer = pointer;
776}
777
778const void *Context::getVertexAttribPointer(unsigned int attribNum) const
779{
780 return mState.vertexAttribute[attribNum].mPointer;
781}
782
daniel@transgaming.com83921382011-01-08 05:46:00 +0000783const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000784{
785 return mState.vertexAttribute;
786}
787
788void Context::setPackAlignment(GLint alignment)
789{
790 mState.packAlignment = alignment;
791}
792
793GLint Context::getPackAlignment() const
794{
795 return mState.packAlignment;
796}
797
798void Context::setUnpackAlignment(GLint alignment)
799{
800 mState.unpackAlignment = alignment;
801}
802
803GLint Context::getUnpackAlignment() const
804{
805 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806}
807
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808GLuint Context::createBuffer()
809{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000810 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811}
812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813GLuint Context::createProgram()
814{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000815 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816}
817
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000818GLuint Context::createShader(GLenum type)
819{
820 return mResourceManager->createShader(type);
821}
822
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823GLuint Context::createTexture()
824{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000825 return mResourceManager->createTexture();
826}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000828GLuint Context::createRenderbuffer()
829{
830 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831}
832
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000833// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834GLuint Context::createFramebuffer()
835{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000836 unsigned int handle = 1;
837
838 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
839 {
840 handle++;
841 }
842
843 mFramebufferMap[handle] = NULL;
844
845 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846}
847
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000848GLuint Context::createFence()
849{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000850 unsigned int handle = 0;
851
852 while (mFenceMap.find(handle) != mFenceMap.end())
853 {
854 handle++;
855 }
856
857 mFenceMap[handle] = new Fence;
858
859 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000860}
861
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862void Context::deleteBuffer(GLuint buffer)
863{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000864 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865 {
866 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000868
869 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870}
871
872void Context::deleteShader(GLuint shader)
873{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000874 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875}
876
877void Context::deleteProgram(GLuint program)
878{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000879 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880}
881
882void Context::deleteTexture(GLuint texture)
883{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000884 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885 {
886 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000888
889 mResourceManager->deleteTexture(texture);
890}
891
892void Context::deleteRenderbuffer(GLuint renderbuffer)
893{
894 if (mResourceManager->getRenderbuffer(renderbuffer))
895 {
896 detachRenderbuffer(renderbuffer);
897 }
898
899 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900}
901
902void Context::deleteFramebuffer(GLuint framebuffer)
903{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000904 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
905
906 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907 {
908 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000909
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000910 delete framebufferObject->second;
911 mFramebufferMap.erase(framebufferObject);
912 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000914
915void Context::deleteFence(GLuint fence)
916{
917 FenceMap::iterator fenceObject = mFenceMap.find(fence);
918
919 if (fenceObject != mFenceMap.end())
920 {
921 delete fenceObject->second;
922 mFenceMap.erase(fenceObject);
923 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000924}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000926Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000928 return mResourceManager->getBuffer(handle);
929}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000931Shader *Context::getShader(GLuint handle)
932{
933 return mResourceManager->getShader(handle);
934}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000936Program *Context::getProgram(GLuint handle)
937{
938 return mResourceManager->getProgram(handle);
939}
940
941Texture *Context::getTexture(GLuint handle)
942{
943 return mResourceManager->getTexture(handle);
944}
945
946Renderbuffer *Context::getRenderbuffer(GLuint handle)
947{
948 return mResourceManager->getRenderbuffer(handle);
949}
950
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000951Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000952{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000953 return getFramebuffer(mState.readFramebuffer);
954}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000955
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000956Framebuffer *Context::getDrawFramebuffer()
957{
958 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959}
960
961void Context::bindArrayBuffer(unsigned int buffer)
962{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000963 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000965 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
968void Context::bindElementArrayBuffer(unsigned int buffer)
969{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000970 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000972 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
975void Context::bindTexture2D(GLuint texture)
976{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000977 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000979 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980}
981
982void Context::bindTextureCubeMap(GLuint texture)
983{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000984 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000986 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987}
988
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000989void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000991 if (!getFramebuffer(framebuffer))
992 {
993 mFramebufferMap[framebuffer] = new Framebuffer();
994 }
995
996 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000997}
998
999void Context::bindDrawFramebuffer(GLuint framebuffer)
1000{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001001 if (!getFramebuffer(framebuffer))
1002 {
1003 mFramebufferMap[framebuffer] = new Framebuffer();
1004 }
1005
1006 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007}
1008
1009void Context::bindRenderbuffer(GLuint renderbuffer)
1010{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001011 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1012
1013 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014}
1015
1016void Context::useProgram(GLuint program)
1017{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001018 GLuint priorProgram = mState.currentProgram;
1019 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001020
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001021 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001023 Program *newProgram = mResourceManager->getProgram(program);
1024 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1025
1026 if (newProgram)
1027 {
1028 newProgram->addRef();
1029 }
1030
1031 if (oldProgram)
1032 {
1033 oldProgram->release();
1034 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036}
1037
1038void Context::setFramebufferZero(Framebuffer *buffer)
1039{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001040 delete mFramebufferMap[0];
1041 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042}
1043
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001044void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001046 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1047 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048}
1049
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001050Framebuffer *Context::getFramebuffer(unsigned int handle)
1051{
1052 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1053
1054 if (framebuffer == mFramebufferMap.end())
1055 {
1056 return NULL;
1057 }
1058 else
1059 {
1060 return framebuffer->second;
1061 }
1062}
1063
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001064Fence *Context::getFence(unsigned int handle)
1065{
1066 FenceMap::iterator fence = mFenceMap.find(handle);
1067
1068 if (fence == mFenceMap.end())
1069 {
1070 return NULL;
1071 }
1072 else
1073 {
1074 return fence->second;
1075 }
1076}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001077
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078Buffer *Context::getArrayBuffer()
1079{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001080 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081}
1082
1083Buffer *Context::getElementArrayBuffer()
1084{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001085 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
1088Program *Context::getCurrentProgram()
1089{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001090 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091}
1092
1093Texture2D *Context::getTexture2D()
1094{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001095 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, SAMPLER_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096}
1097
1098TextureCubeMap *Context::getTextureCubeMap()
1099{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001100 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, SAMPLER_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101}
1102
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001103Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001105 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001106
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001107 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001108 {
1109 switch (type)
1110 {
1111 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001112 case SAMPLER_2D: return mTexture2DZero.get();
1113 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001114 }
1115 }
1116
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001117 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118}
1119
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001120bool Context::getBooleanv(GLenum pname, GLboolean *params)
1121{
1122 switch (pname)
1123 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001124 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1125 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1126 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001127 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001128 params[0] = mState.colorMaskRed;
1129 params[1] = mState.colorMaskGreen;
1130 params[2] = mState.colorMaskBlue;
1131 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001132 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001133 case GL_CULL_FACE: *params = mState.cullFace; break;
1134 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1135 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1136 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1137 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1138 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1139 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1140 case GL_BLEND: *params = mState.blend; break;
1141 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001142 default:
1143 return false;
1144 }
1145
1146 return true;
1147}
1148
1149bool Context::getFloatv(GLenum pname, GLfloat *params)
1150{
1151 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1152 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1153 // GetIntegerv as its native query function. As it would require conversion in any
1154 // case, this should make no difference to the calling application.
1155 switch (pname)
1156 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001157 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1158 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1159 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1160 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1161 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001162 case GL_ALIASED_LINE_WIDTH_RANGE:
1163 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1164 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1165 break;
1166 case GL_ALIASED_POINT_SIZE_RANGE:
1167 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001168 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 +00001169 break;
1170 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001171 params[0] = mState.zNear;
1172 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001173 break;
1174 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001175 params[0] = mState.colorClearValue.red;
1176 params[1] = mState.colorClearValue.green;
1177 params[2] = mState.colorClearValue.blue;
1178 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001179 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001180 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001181 params[0] = mState.blendColor.red;
1182 params[1] = mState.blendColor.green;
1183 params[2] = mState.blendColor.blue;
1184 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001185 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001186 default:
1187 return false;
1188 }
1189
1190 return true;
1191}
1192
1193bool Context::getIntegerv(GLenum pname, GLint *params)
1194{
1195 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1196 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1197 // GetIntegerv as its native query function. As it would require conversion in any
1198 // case, this should make no difference to the calling application. You may find it in
1199 // Context::getFloatv.
1200 switch (pname)
1201 {
1202 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1203 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001204 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001205 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1206 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1207 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001208 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001209 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001210 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001211 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001212 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1213 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001214 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1215 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1216 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001217 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001218 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1219 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1220 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1221 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001222 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001223 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1224 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1225 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1226 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1227 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1228 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1229 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1230 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1231 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1232 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1233 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1234 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1235 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1236 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1237 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1238 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1239 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1240 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1241 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1242 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1243 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1244 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1245 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1246 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001247 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1248 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001249 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1250 {
1251 if (supportsCompressedTextures())
1252 {
1253 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1254 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1255 *params = 2;
1256 }
1257 else
1258 {
1259 *params = 0;
1260 }
1261 }
1262 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001263 case GL_MAX_SAMPLES_ANGLE:
1264 {
1265 GLsizei maxSamples = getMaxSupportedSamples();
1266 if (maxSamples != 0)
1267 {
1268 *params = maxSamples;
1269 }
1270 else
1271 {
1272 return false;
1273 }
1274
1275 break;
1276 }
1277 case GL_SAMPLE_BUFFERS:
1278 case GL_SAMPLES:
1279 {
1280 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1281 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1282 {
1283 switch (pname)
1284 {
1285 case GL_SAMPLE_BUFFERS:
1286 if (framebuffer->getSamples() != 0)
1287 {
1288 *params = 1;
1289 }
1290 else
1291 {
1292 *params = 0;
1293 }
1294 break;
1295 case GL_SAMPLES:
1296 *params = framebuffer->getSamples();
1297 break;
1298 }
1299 }
1300 else
1301 {
1302 *params = 0;
1303 }
1304 }
1305 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001306 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1307 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1308 case GL_MAX_VIEWPORT_DIMS:
1309 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001310 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001311 params[0] = maxDimension;
1312 params[1] = maxDimension;
1313 }
1314 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001315 case GL_COMPRESSED_TEXTURE_FORMATS:
1316 {
1317 if (supportsCompressedTextures())
1318 {
1319 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1320 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1321 }
1322 }
1323 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001324 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001325 params[0] = mState.viewportX;
1326 params[1] = mState.viewportY;
1327 params[2] = mState.viewportWidth;
1328 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001329 break;
1330 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001331 params[0] = mState.scissorX;
1332 params[1] = mState.scissorY;
1333 params[2] = mState.scissorWidth;
1334 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001335 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001336 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1337 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001338 case GL_RED_BITS:
1339 case GL_GREEN_BITS:
1340 case GL_BLUE_BITS:
1341 case GL_ALPHA_BITS:
1342 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001343 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001344 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1345
1346 if (colorbuffer)
1347 {
1348 switch (pname)
1349 {
1350 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1351 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1352 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1353 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1354 }
1355 }
1356 else
1357 {
1358 *params = 0;
1359 }
1360 }
1361 break;
1362 case GL_DEPTH_BITS:
1363 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001364 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001365 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001366
1367 if (depthbuffer)
1368 {
1369 *params = depthbuffer->getDepthSize();
1370 }
1371 else
1372 {
1373 *params = 0;
1374 }
1375 }
1376 break;
1377 case GL_STENCIL_BITS:
1378 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001379 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001380 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001381
1382 if (stencilbuffer)
1383 {
1384 *params = stencilbuffer->getStencilSize();
1385 }
1386 else
1387 {
1388 *params = 0;
1389 }
1390 }
1391 break;
1392 case GL_TEXTURE_BINDING_2D:
1393 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001394 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001395 {
1396 error(GL_INVALID_OPERATION);
1397 return false;
1398 }
1399
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001400 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001401 }
1402 break;
1403 case GL_TEXTURE_BINDING_CUBE_MAP:
1404 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001405 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001406 {
1407 error(GL_INVALID_OPERATION);
1408 return false;
1409 }
1410
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001411 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001412 }
1413 break;
1414 default:
1415 return false;
1416 }
1417
1418 return true;
1419}
1420
1421bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1422{
1423 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1424 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1425 // to the fact that it is stored internally as a float, and so would require conversion
1426 // if returned from Context::getIntegerv. Since this conversion is already implemented
1427 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1428 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1429 // application.
1430 switch (pname)
1431 {
1432 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1433 case GL_SHADER_BINARY_FORMATS:
1434 {
1435 *type = GL_INT;
1436 *numParams = 0;
1437 }
1438 break;
1439 case GL_MAX_VERTEX_ATTRIBS:
1440 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1441 case GL_MAX_VARYING_VECTORS:
1442 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1443 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1444 case GL_MAX_TEXTURE_IMAGE_UNITS:
1445 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1446 case GL_MAX_RENDERBUFFER_SIZE:
1447 case GL_NUM_SHADER_BINARY_FORMATS:
1448 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1449 case GL_ARRAY_BUFFER_BINDING:
1450 case GL_FRAMEBUFFER_BINDING:
1451 case GL_RENDERBUFFER_BINDING:
1452 case GL_CURRENT_PROGRAM:
1453 case GL_PACK_ALIGNMENT:
1454 case GL_UNPACK_ALIGNMENT:
1455 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001456 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001457 case GL_RED_BITS:
1458 case GL_GREEN_BITS:
1459 case GL_BLUE_BITS:
1460 case GL_ALPHA_BITS:
1461 case GL_DEPTH_BITS:
1462 case GL_STENCIL_BITS:
1463 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1464 case GL_CULL_FACE_MODE:
1465 case GL_FRONT_FACE:
1466 case GL_ACTIVE_TEXTURE:
1467 case GL_STENCIL_FUNC:
1468 case GL_STENCIL_VALUE_MASK:
1469 case GL_STENCIL_REF:
1470 case GL_STENCIL_FAIL:
1471 case GL_STENCIL_PASS_DEPTH_FAIL:
1472 case GL_STENCIL_PASS_DEPTH_PASS:
1473 case GL_STENCIL_BACK_FUNC:
1474 case GL_STENCIL_BACK_VALUE_MASK:
1475 case GL_STENCIL_BACK_REF:
1476 case GL_STENCIL_BACK_FAIL:
1477 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1478 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1479 case GL_DEPTH_FUNC:
1480 case GL_BLEND_SRC_RGB:
1481 case GL_BLEND_SRC_ALPHA:
1482 case GL_BLEND_DST_RGB:
1483 case GL_BLEND_DST_ALPHA:
1484 case GL_BLEND_EQUATION_RGB:
1485 case GL_BLEND_EQUATION_ALPHA:
1486 case GL_STENCIL_WRITEMASK:
1487 case GL_STENCIL_BACK_WRITEMASK:
1488 case GL_STENCIL_CLEAR_VALUE:
1489 case GL_SUBPIXEL_BITS:
1490 case GL_MAX_TEXTURE_SIZE:
1491 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1492 case GL_SAMPLE_BUFFERS:
1493 case GL_SAMPLES:
1494 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1495 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1496 case GL_TEXTURE_BINDING_2D:
1497 case GL_TEXTURE_BINDING_CUBE_MAP:
1498 {
1499 *type = GL_INT;
1500 *numParams = 1;
1501 }
1502 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001503 case GL_MAX_SAMPLES_ANGLE:
1504 {
1505 if (getMaxSupportedSamples() != 0)
1506 {
1507 *type = GL_INT;
1508 *numParams = 1;
1509 }
1510 else
1511 {
1512 return false;
1513 }
1514 }
1515 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001516 case GL_MAX_VIEWPORT_DIMS:
1517 {
1518 *type = GL_INT;
1519 *numParams = 2;
1520 }
1521 break;
1522 case GL_VIEWPORT:
1523 case GL_SCISSOR_BOX:
1524 {
1525 *type = GL_INT;
1526 *numParams = 4;
1527 }
1528 break;
1529 case GL_SHADER_COMPILER:
1530 case GL_SAMPLE_COVERAGE_INVERT:
1531 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001532 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1533 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1534 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1535 case GL_SAMPLE_COVERAGE:
1536 case GL_SCISSOR_TEST:
1537 case GL_STENCIL_TEST:
1538 case GL_DEPTH_TEST:
1539 case GL_BLEND:
1540 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001541 {
1542 *type = GL_BOOL;
1543 *numParams = 1;
1544 }
1545 break;
1546 case GL_COLOR_WRITEMASK:
1547 {
1548 *type = GL_BOOL;
1549 *numParams = 4;
1550 }
1551 break;
1552 case GL_POLYGON_OFFSET_FACTOR:
1553 case GL_POLYGON_OFFSET_UNITS:
1554 case GL_SAMPLE_COVERAGE_VALUE:
1555 case GL_DEPTH_CLEAR_VALUE:
1556 case GL_LINE_WIDTH:
1557 {
1558 *type = GL_FLOAT;
1559 *numParams = 1;
1560 }
1561 break;
1562 case GL_ALIASED_LINE_WIDTH_RANGE:
1563 case GL_ALIASED_POINT_SIZE_RANGE:
1564 case GL_DEPTH_RANGE:
1565 {
1566 *type = GL_FLOAT;
1567 *numParams = 2;
1568 }
1569 break;
1570 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001571 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001572 {
1573 *type = GL_FLOAT;
1574 *numParams = 4;
1575 }
1576 break;
1577 default:
1578 return false;
1579 }
1580
1581 return true;
1582}
1583
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001584// Applies the render target surface, depth stencil surface, viewport rectangle and
1585// scissor rectangle to the Direct3D 9 device
1586bool Context::applyRenderTarget(bool ignoreViewport)
1587{
1588 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001589
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001590 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001591
1592 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1593 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001594 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595 }
1596
1597 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001598
1599 if (!renderTarget)
1600 {
1601 return false; // Context must be lost
1602 }
1603
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001604 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001605
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001606 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1607 if (renderTargetSerial != mAppliedRenderTargetSerial)
1608 {
1609 device->SetRenderTarget(0, renderTarget);
1610 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001611 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 +00001612 }
1613
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001614 unsigned int depthbufferSerial = 0;
1615 unsigned int stencilbufferSerial = 0;
1616 if (framebufferObject->getDepthbufferType() != GL_NONE)
1617 {
1618 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001619 if (!depthStencil)
1620 {
1621 ERR("Depth stencil pointer unexpectedly null.");
1622 return false;
1623 }
1624
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001625 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1626 }
1627 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1628 {
1629 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001630 if (!depthStencil)
1631 {
1632 ERR("Depth stencil pointer unexpectedly null.");
1633 return false;
1634 }
1635
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001636 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1637 }
1638
1639 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001640 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001641 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001642 {
1643 device->SetDepthStencilSurface(depthStencil);
1644 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001645 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001646 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001647 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648
1649 D3DVIEWPORT9 viewport;
1650 D3DSURFACE_DESC desc;
1651 renderTarget->GetDesc(&desc);
1652
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001653 float zNear = clamp01(mState.zNear);
1654 float zFar = clamp01(mState.zFar);
1655
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656 if (ignoreViewport)
1657 {
1658 viewport.X = 0;
1659 viewport.Y = 0;
1660 viewport.Width = desc.Width;
1661 viewport.Height = desc.Height;
1662 viewport.MinZ = 0.0f;
1663 viewport.MaxZ = 1.0f;
1664 }
1665 else
1666 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001667 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1668 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1669 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1670 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1671 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 +00001672 viewport.MinZ = zNear;
1673 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674 }
1675
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001676 if (viewport.Width <= 0 || viewport.Height <= 0)
1677 {
1678 return false; // Nothing to render
1679 }
1680
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681 device->SetViewport(&viewport);
1682
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001683 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001685 if (mState.scissorTest)
1686 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001687 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1688 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1689 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1690 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1691 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001692 device->SetScissorRect(&rect);
1693 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1694 }
1695 else
1696 {
1697 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1698 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001699
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001700 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001701 }
1702
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001703 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705 Program *programObject = getCurrentProgram();
1706
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001707 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001708 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001709 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001710
daniel@transgaming.com31754962010-11-28 02:02:52 +00001711 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001712 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1713 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1714 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001715 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001716
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001717 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001718 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001719 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001720
daniel@transgaming.com31754962010-11-28 02:02:52 +00001721 GLint depthRange = programObject->getDxDepthRangeLocation();
1722 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1723 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724 }
1725
1726 return true;
1727}
1728
1729// 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 +00001730void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731{
1732 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001733 Program *programObject = getCurrentProgram();
1734
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001735 Framebuffer *framebufferObject = getDrawFramebuffer();
1736
1737 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1738
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001739 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001740 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001741 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001743 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001744 GLint alwaysFront = !isTriangleMode(drawMode);
1745 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1746
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001749 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001751 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 }
1753 else
1754 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001755 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 }
1757
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001758 mCullStateDirty = false;
1759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001761 if (mDepthStateDirty)
1762 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001763 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1766 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767 }
1768 else
1769 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001770 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001772
1773 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774 }
1775
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001776 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001778 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001779 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001780 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1781
1782 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1783 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1784 {
1785 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1786 }
1787 else
1788 {
1789 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1790 unorm<8>(mState.blendColor.alpha),
1791 unorm<8>(mState.blendColor.alpha),
1792 unorm<8>(mState.blendColor.alpha)));
1793 }
1794
1795 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1796 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1797 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1798
1799 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1800 mState.destBlendRGB != mState.destBlendAlpha ||
1801 mState.blendEquationRGB != mState.blendEquationAlpha)
1802 {
1803 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1804
1805 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1806 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1807 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001808 }
1809 else
1810 {
1811 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1812 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001813 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001814 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001815 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001816 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001817 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001818
1819 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820 }
1821
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001822 if (mStencilStateDirty || mFrontFaceDirty)
1823 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001824 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001825 {
1826 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1827 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1828
1829 // FIXME: Unsupported by D3D9
1830 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1831 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1832 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1833 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1834 mState.stencilRef != mState.stencilBackRef ||
1835 mState.stencilMask != mState.stencilBackMask)
1836 {
1837 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1838 return error(GL_INVALID_OPERATION);
1839 }
1840
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001841 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001842 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001843 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1844
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001845 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1846 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001847 es2dx::ConvertComparison(mState.stencilFunc));
1848
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001849 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1850 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001851
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001852 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001853 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001854 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001855 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001856 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001857 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1858
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001859 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1860 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001861 es2dx::ConvertComparison(mState.stencilBackFunc));
1862
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001863 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1864 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001865
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001866 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001867 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001868 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001869 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001870 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001871 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1872 }
1873 else
1874 {
1875 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1876 }
1877
1878 mStencilStateDirty = false;
1879 }
1880
1881 if (mMaskStateDirty)
1882 {
1883 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1884 mState.colorMaskBlue, mState.colorMaskAlpha));
1885 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1886
1887 mMaskStateDirty = false;
1888 }
1889
1890 if (mPolygonOffsetStateDirty)
1891 {
1892 if (mState.polygonOffsetFill)
1893 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001894 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001895 if (depthbuffer)
1896 {
1897 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1898 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1899 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1900 }
1901 }
1902 else
1903 {
1904 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1905 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1906 }
1907
1908 mPolygonOffsetStateDirty = false;
1909 }
1910
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001911 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001913 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001914 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001915 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001916 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001917 FIXME("Sample alpha to coverage is unimplemented.");
1918 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001919
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001920 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1921 if (mState.sampleCoverage)
1922 {
1923 unsigned int mask = 0;
1924 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001925 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001926 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001927
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001928 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001929 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001930 mask <<= 1;
1931
1932 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1933 {
1934 threshold += 1.0f;
1935 mask |= 1;
1936 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001937 }
1938 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001939
1940 if (mState.sampleCoverageInvert)
1941 {
1942 mask = ~mask;
1943 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001944
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001945 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1946 }
1947 else
1948 {
1949 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1950 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001951 }
1952 else
1953 {
1954 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001955 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001956
1957 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001958 }
1959
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001960 if (mDitherStateDirty)
1961 {
1962 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1963
1964 mDitherStateDirty = false;
1965 }
1966
1967 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968}
1969
daniel@transgaming.com83921382011-01-08 05:46:00 +00001970GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001971{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001972 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001973
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001974 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001975 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001976 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001977 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001978 }
1979
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001980 IDirect3DDevice9 *device = getDevice();
1981 Program *program = getCurrentProgram();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001982
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001983 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
1984 D3DVERTEXELEMENT9 *element = &elements[0];
1985
1986 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1987 {
1988 if (attributes[i].active)
1989 {
1990 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
1991
1992 element->Stream = i;
1993 element->Offset = 0;
1994 element->Type = attributes[i].type;
1995 element->Method = D3DDECLMETHOD_DEFAULT;
1996 element->Usage = D3DDECLUSAGE_TEXCOORD;
1997 element->UsageIndex = program->getSemanticIndex(i);
1998 element++;
1999 }
2000 }
2001
2002 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
2003 *(element++) = end;
2004
2005 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
2006 {
2007 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
2008 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
2009 {
2010 entry->lruCount = ++mMaxLru;
2011 device->SetVertexDeclaration(entry->vertexDeclaration);
2012 return GL_NO_ERROR;
2013 }
2014 }
2015
2016 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
2017
2018 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
2019 {
2020 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
2021 {
2022 lastCache = &mVertexDeclCache[i];
2023 }
2024 }
2025
2026 if (lastCache->vertexDeclaration != NULL)
2027 {
2028 lastCache->vertexDeclaration->Release();
2029 lastCache->vertexDeclaration = NULL;
2030 }
2031
2032 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
2033 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
2034 device->SetVertexDeclaration(lastCache->vertexDeclaration);
2035 lastCache->lruCount = ++mMaxLru;
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002036
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002037 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002038}
2039
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002040// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002041GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002042{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002043 IDirect3DDevice9 *device = getDevice();
2044 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002045
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002046 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002047 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00002048 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002049 }
2050
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002051 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052}
2053
2054// Applies the shaders and shader constants to the Direct3D 9 device
2055void Context::applyShaders()
2056{
2057 IDirect3DDevice9 *device = getDevice();
2058 Program *programObject = getCurrentProgram();
2059 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2060 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2061
2062 device->SetVertexShader(vertexShader);
2063 device->SetPixelShader(pixelShader);
2064
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002065 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002066 {
2067 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002068 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002069 }
2070
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071 programObject->applyUniforms();
2072}
2073
2074// Applies the textures and sampler states to the Direct3D 9 device
2075void Context::applyTextures()
2076{
2077 IDirect3DDevice9 *device = getDevice();
2078 Program *programObject = getCurrentProgram();
2079
2080 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2081 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002082 int textureUnit = programObject->getSamplerMapping(sampler);
2083 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002085 SamplerType textureType = programObject->getSamplerType(sampler);
2086
2087 Texture *texture = getSamplerTexture(textureUnit, textureType);
2088
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002089 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002090 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002091 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2092
2093 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002094 {
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002095 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter())
2096 {
2097 GLenum wrapS = texture->getWrapS();
2098 GLenum wrapT = texture->getWrapT();
2099 GLenum minFilter = texture->getMinFilter();
2100 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002102 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2103 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002105 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2106 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2107 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2108 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2109 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
2110 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002112 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyImage())
2113 {
2114 device->SetTexture(sampler, d3dTexture);
2115 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002116 }
2117 else
2118 {
2119 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2120 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002121
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002122 mAppliedTextureSerial[sampler] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002123 texture->resetDirty();
2124 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002126 else
2127 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002128 if (mAppliedTextureSerial[sampler] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002129 {
2130 device->SetTexture(sampler, NULL);
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002131 mAppliedTextureSerial[sampler] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002132 }
2133 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134 }
2135}
2136
2137void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2138{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002139 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002140
2141 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2142 {
2143 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2144 }
2145
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002146 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2147 {
2148 return error(GL_INVALID_OPERATION);
2149 }
2150
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002152
2153 if (!renderTarget)
2154 {
2155 return; // Context must be lost, return silently
2156 }
2157
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 IDirect3DDevice9 *device = getDevice();
2159
2160 D3DSURFACE_DESC desc;
2161 renderTarget->GetDesc(&desc);
2162
2163 IDirect3DSurface9 *systemSurface;
2164 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2165
2166 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2167 {
2168 return error(GL_OUT_OF_MEMORY);
2169 }
2170
2171 ASSERT(SUCCEEDED(result));
2172
2173 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2174 {
2175 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2176 }
2177
2178 result = device->GetRenderTargetData(renderTarget, systemSurface);
2179
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180 if (FAILED(result))
2181 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002182 systemSurface->Release();
2183
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002184 switch (result)
2185 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002186 case D3DERR_DRIVERINTERNALERROR:
2187 case D3DERR_DEVICELOST:
2188 return error(GL_OUT_OF_MEMORY);
2189 default:
2190 UNREACHABLE();
2191 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002192 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002193 }
2194
2195 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002196 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2197 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2198 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2199 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2200 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201
2202 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2203
2204 if (FAILED(result))
2205 {
2206 UNREACHABLE();
2207 systemSurface->Release();
2208
2209 return; // No sensible error to generate
2210 }
2211
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002212 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002214 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002215 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002216 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002217
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002218 for (int j = 0; j < rect.bottom - rect.top; j++)
2219 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002220 if (desc.Format == D3DFMT_A8R8G8B8 &&
2221 format == GL_BGRA_EXT &&
2222 type == GL_UNSIGNED_BYTE)
2223 {
2224 // Fast path for EXT_read_format_bgra, given
2225 // an RGBA source buffer. Note that buffers with no
2226 // alpha go through the slow path below.
2227 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002228 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002229 (rect.right - rect.left) * 4);
2230 continue;
2231 }
2232
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233 for (int i = 0; i < rect.right - rect.left; i++)
2234 {
2235 float r;
2236 float g;
2237 float b;
2238 float a;
2239
2240 switch (desc.Format)
2241 {
2242 case D3DFMT_R5G6B5:
2243 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002244 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245
2246 a = 1.0f;
2247 b = (rgb & 0x001F) * (1.0f / 0x001F);
2248 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2249 r = (rgb & 0xF800) * (1.0f / 0xF800);
2250 }
2251 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 case D3DFMT_A1R5G5B5:
2253 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002254 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255
2256 a = (argb & 0x8000) ? 1.0f : 0.0f;
2257 b = (argb & 0x001F) * (1.0f / 0x001F);
2258 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2259 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2260 }
2261 break;
2262 case D3DFMT_A8R8G8B8:
2263 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002264 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265
2266 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2267 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2268 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2269 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2270 }
2271 break;
2272 case D3DFMT_X8R8G8B8:
2273 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002274 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275
2276 a = 1.0f;
2277 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2278 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2279 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2280 }
2281 break;
2282 case D3DFMT_A2R10G10B10:
2283 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002284 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285
2286 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2287 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2288 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2289 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2290 }
2291 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002292 case D3DFMT_A32B32G32R32F:
2293 {
2294 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002295 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2296 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2297 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2298 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002299 }
2300 break;
2301 case D3DFMT_A16B16G16R16F:
2302 {
2303 // float formats in D3D are stored rgba, rather than the other way round
2304 float abgr[4];
2305
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002306 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002307
2308 a = abgr[3];
2309 b = abgr[2];
2310 g = abgr[1];
2311 r = abgr[0];
2312 }
2313 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002314 default:
2315 UNIMPLEMENTED(); // FIXME
2316 UNREACHABLE();
2317 }
2318
2319 switch (format)
2320 {
2321 case GL_RGBA:
2322 switch (type)
2323 {
2324 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002325 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2326 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2327 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2328 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329 break;
2330 default: UNREACHABLE();
2331 }
2332 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002333 case GL_BGRA_EXT:
2334 switch (type)
2335 {
2336 case GL_UNSIGNED_BYTE:
2337 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2338 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2339 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2340 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2341 break;
2342 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2343 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2344 // this type is packed as follows:
2345 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2346 // --------------------------------------------------------------------------------
2347 // | 4th | 3rd | 2nd | 1st component |
2348 // --------------------------------------------------------------------------------
2349 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2350 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2351 ((unsigned short)(15 * a + 0.5f) << 12)|
2352 ((unsigned short)(15 * r + 0.5f) << 8) |
2353 ((unsigned short)(15 * g + 0.5f) << 4) |
2354 ((unsigned short)(15 * b + 0.5f) << 0);
2355 break;
2356 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2357 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2358 // this type is packed as follows:
2359 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2360 // --------------------------------------------------------------------------------
2361 // | 4th | 3rd | 2nd | 1st component |
2362 // --------------------------------------------------------------------------------
2363 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2364 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2365 ((unsigned short)( a + 0.5f) << 15) |
2366 ((unsigned short)(31 * r + 0.5f) << 10) |
2367 ((unsigned short)(31 * g + 0.5f) << 5) |
2368 ((unsigned short)(31 * b + 0.5f) << 0);
2369 break;
2370 default: UNREACHABLE();
2371 }
2372 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002373 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 switch (type)
2375 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002376 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002377 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2378 ((unsigned short)(31 * b + 0.5f) << 0) |
2379 ((unsigned short)(63 * g + 0.5f) << 5) |
2380 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 break;
2382 default: UNREACHABLE();
2383 }
2384 break;
2385 default: UNREACHABLE();
2386 }
2387 }
2388 }
2389
2390 systemSurface->UnlockRect();
2391
2392 systemSurface->Release();
2393}
2394
2395void Context::clear(GLbitfield mask)
2396{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002397 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002398
2399 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2400 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002401 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002402 }
2403
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002404 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 IDirect3DDevice9 *device = getDevice();
2406 DWORD flags = 0;
2407
2408 if (mask & GL_COLOR_BUFFER_BIT)
2409 {
2410 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002411
2412 if (framebufferObject->getColorbufferType() != GL_NONE)
2413 {
2414 flags |= D3DCLEAR_TARGET;
2415 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416 }
2417
2418 if (mask & GL_DEPTH_BUFFER_BIT)
2419 {
2420 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002421 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 {
2423 flags |= D3DCLEAR_ZBUFFER;
2424 }
2425 }
2426
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 GLuint stencilUnmasked = 0x0;
2428
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002429 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002432 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002434 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002435 if (!depthStencil)
2436 {
2437 ERR("Depth stencil pointer unexpectedly null.");
2438 return;
2439 }
2440
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002441 D3DSURFACE_DESC desc;
2442 depthStencil->GetDesc(&desc);
2443
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002444 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002445 stencilUnmasked = (0x1 << stencilSize) - 1;
2446
2447 if (stencilUnmasked != 0x0)
2448 {
2449 flags |= D3DCLEAR_STENCIL;
2450 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451 }
2452 }
2453
2454 if (mask != 0)
2455 {
2456 return error(GL_INVALID_VALUE);
2457 }
2458
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002459 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2460 {
2461 return;
2462 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002464 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002465 unorm<8>(mState.colorClearValue.red),
2466 unorm<8>(mState.colorClearValue.green),
2467 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002468 float depth = clamp01(mState.depthClearValue);
2469 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002470
2471 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2472
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002473 if (!renderTarget)
2474 {
2475 return; // Context must be lost, return silently
2476 }
2477
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478 D3DSURFACE_DESC desc;
2479 renderTarget->GetDesc(&desc);
2480
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002481 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002482
2483 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002484 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002486 !(mState.colorMaskRed && mState.colorMaskGreen &&
2487 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488
2489 if (needMaskedColorClear || needMaskedStencilClear)
2490 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002491 // State which is altered in all paths from this point to the clear call is saved.
2492 // State which is altered in only some paths will be flagged dirty in the case that
2493 // that path is taken.
2494 HRESULT hr;
2495 if (mMaskedClearSavedState == NULL)
2496 {
2497 hr = device->BeginStateBlock();
2498 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2499
2500 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2501 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2502 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2503 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2504 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2505 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2506 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2507 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2508 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2509 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2510 device->SetPixelShader(NULL);
2511 device->SetVertexShader(NULL);
2512 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002513 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2514 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2515 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2516 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2517 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2518 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2519 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002520
2521 hr = device->EndStateBlock(&mMaskedClearSavedState);
2522 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2523 }
2524
2525 ASSERT(mMaskedClearSavedState != NULL);
2526
2527 if (mMaskedClearSavedState != NULL)
2528 {
2529 hr = mMaskedClearSavedState->Capture();
2530 ASSERT(SUCCEEDED(hr));
2531 }
2532
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002533 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2534 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2535 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2536 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2537 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2538 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2539 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2540 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2541
2542 if (flags & D3DCLEAR_TARGET)
2543 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002544 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545 }
2546 else
2547 {
2548 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2549 }
2550
2551 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2552 {
2553 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2554 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2555 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2556 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002557 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002558 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2560 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002561 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 }
2563 else
2564 {
2565 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2566 }
2567
2568 device->SetPixelShader(NULL);
2569 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002570 device->SetFVF(D3DFVF_XYZRHW);
2571 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2572 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2573 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2574 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2575 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2576 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2577 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002578
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002579 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2580 quad[0][0] = -0.5f;
2581 quad[0][1] = desc.Height - 0.5f;
2582 quad[0][2] = 0.0f;
2583 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002585 quad[1][0] = desc.Width - 0.5f;
2586 quad[1][1] = desc.Height - 0.5f;
2587 quad[1][2] = 0.0f;
2588 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002590 quad[2][0] = -0.5f;
2591 quad[2][1] = -0.5f;
2592 quad[2][2] = 0.0f;
2593 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002595 quad[3][0] = desc.Width - 0.5f;
2596 quad[3][1] = -0.5f;
2597 quad[3][2] = 0.0f;
2598 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002600 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002601 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602
2603 if (flags & D3DCLEAR_ZBUFFER)
2604 {
2605 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2606 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2607 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2608 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002609
2610 if (mMaskedClearSavedState != NULL)
2611 {
2612 mMaskedClearSavedState->Apply();
2613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002615 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 {
2617 device->Clear(0, NULL, flags, color, depth, stencil);
2618 }
2619}
2620
2621void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2622{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002623 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 {
2625 return error(GL_INVALID_OPERATION);
2626 }
2627
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002628 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 IDirect3DDevice9 *device = getDevice();
2630 D3DPRIMITIVETYPE primitiveType;
2631 int primitiveCount;
2632
2633 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2634 return error(GL_INVALID_ENUM);
2635
2636 if (primitiveCount <= 0)
2637 {
2638 return;
2639 }
2640
2641 if (!applyRenderTarget(false))
2642 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002643 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644 }
2645
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002646 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002647
daniel@transgaming.com83921382011-01-08 05:46:00 +00002648 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002649 if (err != GL_NO_ERROR)
2650 {
2651 return error(err);
2652 }
2653
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002654 applyShaders();
2655 applyTextures();
2656
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002657 if (!getCurrentProgram()->validateSamplers())
2658 {
2659 return error(GL_INVALID_OPERATION);
2660 }
2661
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002662 if (!cullSkipsDraw(mode))
2663 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002664 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002665
2666 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002667
2668 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2669 {
2670 drawClosingLine(first, first + count - 1);
2671 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002672 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673}
2674
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002675void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002677 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002678 {
2679 return error(GL_INVALID_OPERATION);
2680 }
2681
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002682 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002683 {
2684 return error(GL_INVALID_OPERATION);
2685 }
2686
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002687 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688 IDirect3DDevice9 *device = getDevice();
2689 D3DPRIMITIVETYPE primitiveType;
2690 int primitiveCount;
2691
2692 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2693 return error(GL_INVALID_ENUM);
2694
2695 if (primitiveCount <= 0)
2696 {
2697 return;
2698 }
2699
2700 if (!applyRenderTarget(false))
2701 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002702 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 }
2704
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002705 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002706
2707 TranslatedIndexData indexInfo;
2708 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2709 if (err != GL_NO_ERROR)
2710 {
2711 return error(err);
2712 }
2713
daniel@transgaming.com83921382011-01-08 05:46:00 +00002714 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2715 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002716 if (err != GL_NO_ERROR)
2717 {
2718 return error(err);
2719 }
2720
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721 applyShaders();
2722 applyTextures();
2723
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002724 if (!getCurrentProgram()->validateSamplers())
2725 {
2726 return error(GL_INVALID_OPERATION);
2727 }
2728
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002729 if (!cullSkipsDraw(mode))
2730 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002731 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002732
daniel@transgaming.com83921382011-01-08 05:46:00 +00002733 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002734
2735 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2736 {
2737 drawClosingLine(count, type, indices);
2738 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740}
2741
2742void Context::finish()
2743{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002744 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002745 IDirect3DDevice9 *device = getDevice();
2746 IDirect3DQuery9 *occlusionQuery = NULL;
2747
2748 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2749
2750 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2751 {
2752 return error(GL_OUT_OF_MEMORY);
2753 }
2754
2755 ASSERT(SUCCEEDED(result));
2756
2757 if (occlusionQuery)
2758 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002759 IDirect3DStateBlock9 *savedState = NULL;
2760 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2761
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002762 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2763 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764
2765 // Render something outside the render target
2766 device->SetPixelShader(NULL);
2767 device->SetVertexShader(NULL);
2768 device->SetFVF(D3DFVF_XYZRHW);
2769 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002770 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002771 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002772
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002773 result = occlusionQuery->Issue(D3DISSUE_END);
2774 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775
2776 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2777 {
2778 // Keep polling, but allow other threads to do something useful first
2779 Sleep(0);
2780 }
2781
2782 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002783
2784 if (savedState)
2785 {
2786 savedState->Apply();
2787 savedState->Release();
2788 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789 }
2790}
2791
2792void Context::flush()
2793{
2794 IDirect3DDevice9 *device = getDevice();
2795 IDirect3DQuery9 *eventQuery = NULL;
2796
2797 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2798
2799 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2800 {
2801 return error(GL_OUT_OF_MEMORY);
2802 }
2803
2804 ASSERT(SUCCEEDED(result));
2805
2806 if (eventQuery)
2807 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002808 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2809 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002811 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002812 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002813
2814 if (result == D3DERR_DEVICELOST)
2815 {
2816 error(GL_OUT_OF_MEMORY);
2817 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818 }
2819}
2820
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002821void Context::drawClosingLine(unsigned int first, unsigned int last)
2822{
2823 IDirect3DDevice9 *device = getDevice();
2824 IDirect3DIndexBuffer9 *indexBuffer = NULL;
2825 HRESULT result = D3DERR_INVALIDCALL;
2826
2827 if (supports32bitIndices())
2828 {
2829 result = device->CreateIndexBuffer(8, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &indexBuffer, 0);
2830
2831 if (SUCCEEDED(result))
2832 {
2833 unsigned int *data;
2834 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2835
2836 if (SUCCEEDED(result))
2837 {
2838 data[0] = last;
2839 data[1] = first;
2840 }
2841 }
2842 }
2843 else
2844 {
2845 result = device->CreateIndexBuffer(4, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexBuffer, 0);
2846
2847 if (SUCCEEDED(result))
2848 {
2849 unsigned short *data;
2850 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2851
2852 if (SUCCEEDED(result))
2853 {
2854 data[0] = last;
2855 data[1] = first;
2856 }
2857 }
2858 }
2859
2860 if (SUCCEEDED(result))
2861 {
2862 indexBuffer->Unlock();
2863 device->SetIndices(indexBuffer);
2864
2865 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, 0, 1);
2866
2867 indexBuffer->Release();
2868 }
2869 else
2870 {
2871 ERR("Could not create an index buffer for closing a line loop.");
2872 error(GL_OUT_OF_MEMORY);
2873 }
2874}
2875
2876void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2877{
2878 unsigned int first = 0;
2879 unsigned int last = 0;
2880
2881 if (mState.elementArrayBuffer.get())
2882 {
2883 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2884 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2885 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2886 }
2887
2888 switch (type)
2889 {
2890 case GL_UNSIGNED_BYTE:
2891 first = static_cast<const GLubyte*>(indices)[0];
2892 last = static_cast<const GLubyte*>(indices)[count - 1];
2893 break;
2894 case GL_UNSIGNED_SHORT:
2895 first = static_cast<const GLushort*>(indices)[0];
2896 last = static_cast<const GLushort*>(indices)[count - 1];
2897 break;
2898 case GL_UNSIGNED_INT:
2899 first = static_cast<const GLuint*>(indices)[0];
2900 last = static_cast<const GLuint*>(indices)[count - 1];
2901 break;
2902 default: UNREACHABLE();
2903 }
2904
2905 drawClosingLine(first, last);
2906}
2907
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002908void Context::recordInvalidEnum()
2909{
2910 mInvalidEnum = true;
2911}
2912
2913void Context::recordInvalidValue()
2914{
2915 mInvalidValue = true;
2916}
2917
2918void Context::recordInvalidOperation()
2919{
2920 mInvalidOperation = true;
2921}
2922
2923void Context::recordOutOfMemory()
2924{
2925 mOutOfMemory = true;
2926}
2927
2928void Context::recordInvalidFramebufferOperation()
2929{
2930 mInvalidFramebufferOperation = true;
2931}
2932
2933// Get one of the recorded errors and clear its flag, if any.
2934// [OpenGL ES 2.0.24] section 2.5 page 13.
2935GLenum Context::getError()
2936{
2937 if (mInvalidEnum)
2938 {
2939 mInvalidEnum = false;
2940
2941 return GL_INVALID_ENUM;
2942 }
2943
2944 if (mInvalidValue)
2945 {
2946 mInvalidValue = false;
2947
2948 return GL_INVALID_VALUE;
2949 }
2950
2951 if (mInvalidOperation)
2952 {
2953 mInvalidOperation = false;
2954
2955 return GL_INVALID_OPERATION;
2956 }
2957
2958 if (mOutOfMemory)
2959 {
2960 mOutOfMemory = false;
2961
2962 return GL_OUT_OF_MEMORY;
2963 }
2964
2965 if (mInvalidFramebufferOperation)
2966 {
2967 mInvalidFramebufferOperation = false;
2968
2969 return GL_INVALID_FRAMEBUFFER_OPERATION;
2970 }
2971
2972 return GL_NO_ERROR;
2973}
2974
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002975bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002976{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002977 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002978}
2979
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002980int Context::getMaximumVaryingVectors() const
2981{
2982 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2983}
2984
daniel@transgaming.com458da142010-11-28 02:03:02 +00002985int Context::getMaximumFragmentUniformVectors() const
2986{
2987 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2988}
2989
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002990int Context::getMaxSupportedSamples() const
2991{
2992 return mMaxSupportedSamples;
2993}
2994
2995int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2996{
2997 if (requested == 0)
2998 {
2999 return requested;
3000 }
3001
3002 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3003 if (itr == mMultiSampleSupport.end())
3004 {
3005 return -1;
3006 }
3007
3008 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3009 {
3010 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3011 {
3012 return i;
3013 }
3014 }
3015
3016 return -1;
3017}
3018
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003019bool Context::supportsEventQueries() const
3020{
3021 return mSupportsEventQueries;
3022}
3023
daniel@transgaming.com01868132010-08-24 19:21:17 +00003024bool Context::supportsCompressedTextures() const
3025{
3026 return mSupportsCompressedTextures;
3027}
3028
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003029bool Context::supportsFloatTextures() const
3030{
3031 return mSupportsFloatTextures;
3032}
3033
3034bool Context::supportsFloatLinearFilter() const
3035{
3036 return mSupportsFloatLinearFilter;
3037}
3038
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003039bool Context::supportsFloatRenderableTextures() const
3040{
3041 return mSupportsFloatRenderableTextures;
3042}
3043
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003044bool Context::supportsHalfFloatTextures() const
3045{
3046 return mSupportsHalfFloatTextures;
3047}
3048
3049bool Context::supportsHalfFloatLinearFilter() const
3050{
3051 return mSupportsHalfFloatLinearFilter;
3052}
3053
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003054bool Context::supportsHalfFloatRenderableTextures() const
3055{
3056 return mSupportsHalfFloatRenderableTextures;
3057}
3058
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003059int Context::getMaximumRenderbufferDimension() const
3060{
3061 return mMaxRenderbufferDimension;
3062}
3063
3064int Context::getMaximumTextureDimension() const
3065{
3066 return mMaxTextureDimension;
3067}
3068
3069int Context::getMaximumCubeTextureDimension() const
3070{
3071 return mMaxCubeTextureDimension;
3072}
3073
3074int Context::getMaximumTextureLevel() const
3075{
3076 return mMaxTextureLevel;
3077}
3078
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003079bool Context::supportsLuminanceTextures() const
3080{
3081 return mSupportsLuminanceTextures;
3082}
3083
3084bool Context::supportsLuminanceAlphaTextures() const
3085{
3086 return mSupportsLuminanceAlphaTextures;
3087}
3088
daniel@transgaming.com83921382011-01-08 05:46:00 +00003089bool Context::supports32bitIndices() const
3090{
3091 return mSupports32bitIndices;
3092}
3093
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003094void Context::detachBuffer(GLuint buffer)
3095{
3096 // [OpenGL ES 2.0.24] section 2.9 page 22:
3097 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3098 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3099
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003100 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003101 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003102 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003103 }
3104
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003105 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003106 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003107 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003108 }
3109
3110 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3111 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003112 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003113 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003114 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003115 }
3116 }
3117}
3118
3119void Context::detachTexture(GLuint texture)
3120{
3121 // [OpenGL ES 2.0.24] section 3.8 page 84:
3122 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3123 // rebound to texture object zero
3124
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003125 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003126 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003127 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003128 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003129 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003130 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003131 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003132 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003133 }
3134 }
3135
3136 // [OpenGL ES 2.0.24] section 4.4 page 112:
3137 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3138 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3139 // image was attached in the currently bound framebuffer.
3140
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003141 Framebuffer *readFramebuffer = getReadFramebuffer();
3142 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003143
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003144 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003145 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003146 readFramebuffer->detachTexture(texture);
3147 }
3148
3149 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3150 {
3151 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003152 }
3153}
3154
3155void Context::detachFramebuffer(GLuint framebuffer)
3156{
3157 // [OpenGL ES 2.0.24] section 4.4 page 107:
3158 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3159 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3160
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003161 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003162 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003163 bindReadFramebuffer(0);
3164 }
3165
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003166 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003167 {
3168 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003169 }
3170}
3171
3172void Context::detachRenderbuffer(GLuint renderbuffer)
3173{
3174 // [OpenGL ES 2.0.24] section 4.4 page 109:
3175 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3176 // had been executed with the target RENDERBUFFER and name of zero.
3177
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003178 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003179 {
3180 bindRenderbuffer(0);
3181 }
3182
3183 // [OpenGL ES 2.0.24] section 4.4 page 111:
3184 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3185 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3186 // point to which this image was attached in the currently bound framebuffer.
3187
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003188 Framebuffer *readFramebuffer = getReadFramebuffer();
3189 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003190
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003191 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003192 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003193 readFramebuffer->detachRenderbuffer(renderbuffer);
3194 }
3195
3196 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3197 {
3198 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003199 }
3200}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003201
3202Texture *Context::getIncompleteTexture(SamplerType type)
3203{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003204 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003205
3206 if (t == NULL)
3207 {
3208 static const GLubyte color[] = { 0, 0, 0, 255 };
3209
3210 switch (type)
3211 {
3212 default:
3213 UNREACHABLE();
3214 // default falls through to SAMPLER_2D
3215
3216 case SAMPLER_2D:
3217 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003218 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003219 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003220 t = incomplete2d;
3221 }
3222 break;
3223
3224 case SAMPLER_CUBE:
3225 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003226 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003227
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003228 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3229 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3230 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3231 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3232 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3233 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003234
3235 t = incompleteCube;
3236 }
3237 break;
3238 }
3239
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003240 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003241 }
3242
3243 return t;
3244}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003245
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003246bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003247{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003248 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003249}
3250
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003251bool Context::isTriangleMode(GLenum drawMode)
3252{
3253 switch (drawMode)
3254 {
3255 case GL_TRIANGLES:
3256 case GL_TRIANGLE_FAN:
3257 case GL_TRIANGLE_STRIP:
3258 return true;
3259 case GL_POINTS:
3260 case GL_LINES:
3261 case GL_LINE_LOOP:
3262 case GL_LINE_STRIP:
3263 return false;
3264 default: UNREACHABLE();
3265 }
3266
3267 return false;
3268}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003269
3270void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3271{
3272 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3273
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003274 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3275 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3276 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3277 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003278
daniel@transgaming.com83921382011-01-08 05:46:00 +00003279 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003280}
3281
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003282void Context::initExtensionString()
3283{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003284 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003285 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3286 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003287 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003288 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003289 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003290
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003291 if (supportsEventQueries())
3292 {
3293 mExtensionString += "GL_NV_fence ";
3294 }
3295
daniel@transgaming.com01868132010-08-24 19:21:17 +00003296 if (supportsCompressedTextures())
3297 {
3298 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3299 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003300
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003301 if (supportsFloatTextures())
3302 {
3303 mExtensionString += "GL_OES_texture_float ";
3304 }
3305
3306 if (supportsHalfFloatTextures())
3307 {
3308 mExtensionString += "GL_OES_texture_half_float ";
3309 }
3310
3311 if (supportsFloatLinearFilter())
3312 {
3313 mExtensionString += "GL_OES_texture_float_linear ";
3314 }
3315
3316 if (supportsHalfFloatLinearFilter())
3317 {
3318 mExtensionString += "GL_OES_texture_half_float_linear ";
3319 }
3320
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003321 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003322 {
3323 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3324 }
3325
daniel@transgaming.com83921382011-01-08 05:46:00 +00003326 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003327 {
3328 mExtensionString += "GL_OES_element_index_uint ";
3329 }
3330
3331 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3332 if (end != std::string::npos)
3333 {
3334 mExtensionString.resize(end+1);
3335 }
3336}
3337
3338const char *Context::getExtensionString() const
3339{
3340 return mExtensionString.c_str();
3341}
3342
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003343void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3344 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3345 GLbitfield mask)
3346{
3347 IDirect3DDevice9 *device = getDevice();
3348
3349 Framebuffer *readFramebuffer = getReadFramebuffer();
3350 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3351
3352 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3353 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3354 {
3355 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3356 }
3357
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003358 if (drawFramebuffer->getSamples() != 0)
3359 {
3360 return error(GL_INVALID_OPERATION);
3361 }
3362
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003363 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3364 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3365 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3366 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3367
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003368 RECT sourceRect;
3369 RECT destRect;
3370
3371 if (srcX0 < srcX1)
3372 {
3373 sourceRect.left = srcX0;
3374 sourceRect.right = srcX1;
3375 destRect.left = dstX0;
3376 destRect.right = dstX1;
3377 }
3378 else
3379 {
3380 sourceRect.left = srcX1;
3381 destRect.left = dstX1;
3382 sourceRect.right = srcX0;
3383 destRect.right = dstX0;
3384 }
3385
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003386 if (srcY0 < srcY1)
3387 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003388 sourceRect.top = readBufferHeight - srcY1;
3389 destRect.top = drawBufferHeight - dstY1;
3390 sourceRect.bottom = readBufferHeight - srcY0;
3391 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003392 }
3393 else
3394 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003395 sourceRect.top = readBufferHeight - srcY0;
3396 destRect.top = drawBufferHeight - dstY0;
3397 sourceRect.bottom = readBufferHeight - srcY1;
3398 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003399 }
3400
3401 RECT sourceScissoredRect = sourceRect;
3402 RECT destScissoredRect = destRect;
3403
3404 if (mState.scissorTest)
3405 {
3406 // Only write to parts of the destination framebuffer which pass the scissor test
3407 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3408 // rect will be checked against scissorY, rather than the bottom.
3409 if (destRect.left < mState.scissorX)
3410 {
3411 int xDiff = mState.scissorX - destRect.left;
3412 destScissoredRect.left = mState.scissorX;
3413 sourceScissoredRect.left += xDiff;
3414 }
3415
3416 if (destRect.right > mState.scissorX + mState.scissorWidth)
3417 {
3418 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3419 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3420 sourceScissoredRect.right -= xDiff;
3421 }
3422
3423 if (destRect.top < mState.scissorY)
3424 {
3425 int yDiff = mState.scissorY - destRect.top;
3426 destScissoredRect.top = mState.scissorY;
3427 sourceScissoredRect.top += yDiff;
3428 }
3429
3430 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3431 {
3432 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3433 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3434 sourceScissoredRect.bottom -= yDiff;
3435 }
3436 }
3437
3438 bool blitRenderTarget = false;
3439 bool blitDepthStencil = false;
3440
3441 RECT sourceTrimmedRect = sourceScissoredRect;
3442 RECT destTrimmedRect = destScissoredRect;
3443
3444 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3445 // the actual draw and read surfaces.
3446 if (sourceTrimmedRect.left < 0)
3447 {
3448 int xDiff = 0 - sourceTrimmedRect.left;
3449 sourceTrimmedRect.left = 0;
3450 destTrimmedRect.left += xDiff;
3451 }
3452
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003453 if (sourceTrimmedRect.right > readBufferWidth)
3454 {
3455 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3456 sourceTrimmedRect.right = readBufferWidth;
3457 destTrimmedRect.right -= xDiff;
3458 }
3459
3460 if (sourceTrimmedRect.top < 0)
3461 {
3462 int yDiff = 0 - sourceTrimmedRect.top;
3463 sourceTrimmedRect.top = 0;
3464 destTrimmedRect.top += yDiff;
3465 }
3466
3467 if (sourceTrimmedRect.bottom > readBufferHeight)
3468 {
3469 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3470 sourceTrimmedRect.bottom = readBufferHeight;
3471 destTrimmedRect.bottom -= yDiff;
3472 }
3473
3474 if (destTrimmedRect.left < 0)
3475 {
3476 int xDiff = 0 - destTrimmedRect.left;
3477 destTrimmedRect.left = 0;
3478 sourceTrimmedRect.left += xDiff;
3479 }
3480
3481 if (destTrimmedRect.right > drawBufferWidth)
3482 {
3483 int xDiff = destTrimmedRect.right - drawBufferWidth;
3484 destTrimmedRect.right = drawBufferWidth;
3485 sourceTrimmedRect.right -= xDiff;
3486 }
3487
3488 if (destTrimmedRect.top < 0)
3489 {
3490 int yDiff = 0 - destTrimmedRect.top;
3491 destTrimmedRect.top = 0;
3492 sourceTrimmedRect.top += yDiff;
3493 }
3494
3495 if (destTrimmedRect.bottom > drawBufferHeight)
3496 {
3497 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3498 destTrimmedRect.bottom = drawBufferHeight;
3499 sourceTrimmedRect.bottom -= yDiff;
3500 }
3501
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003502 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003503 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3504 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3505 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3506 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003507 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3508 {
3509 partialBufferCopy = true;
3510 }
3511
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003512 if (mask & GL_COLOR_BUFFER_BIT)
3513 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003514 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3515 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3516 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3517 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3518 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003519 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3520 {
3521 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3522 return error(GL_INVALID_OPERATION);
3523 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003524
3525 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3526 {
3527 return error(GL_INVALID_OPERATION);
3528 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003529
3530 blitRenderTarget = true;
3531
3532 }
3533
3534 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3535 {
3536 DepthStencilbuffer *readDSBuffer = NULL;
3537 DepthStencilbuffer *drawDSBuffer = NULL;
3538
3539 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3540 // both a depth and stencil buffer, it will be the same buffer.
3541
3542 if (mask & GL_DEPTH_BUFFER_BIT)
3543 {
3544 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3545 {
3546 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3547 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3548 {
3549 return error(GL_INVALID_OPERATION);
3550 }
3551
3552 blitDepthStencil = true;
3553 readDSBuffer = readFramebuffer->getDepthbuffer();
3554 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3555 }
3556 }
3557
3558 if (mask & GL_STENCIL_BUFFER_BIT)
3559 {
3560 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3561 {
3562 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3563 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3564 {
3565 return error(GL_INVALID_OPERATION);
3566 }
3567
3568 blitDepthStencil = true;
3569 readDSBuffer = readFramebuffer->getStencilbuffer();
3570 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3571 }
3572 }
3573
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003574 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003575 {
3576 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3577 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3578 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003579
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003580 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3581 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003582 {
3583 return error(GL_INVALID_OPERATION);
3584 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003585 }
3586
3587 if (blitRenderTarget || blitDepthStencil)
3588 {
3589 egl::Display *display = getDisplay();
3590 display->endScene();
3591
3592 if (blitRenderTarget)
3593 {
3594 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3595 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3596
3597 if (FAILED(result))
3598 {
3599 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3600 return;
3601 }
3602 }
3603
3604 if (blitDepthStencil)
3605 {
3606 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3607
3608 if (FAILED(result))
3609 {
3610 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3611 return;
3612 }
3613 }
3614 }
3615}
3616
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003617}
3618
3619extern "C"
3620{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003621gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003622{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003623 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003624}
3625
3626void glDestroyContext(gl::Context *context)
3627{
3628 delete context;
3629
3630 if (context == gl::getContext())
3631 {
3632 gl::makeCurrent(NULL, NULL, NULL);
3633 }
3634}
3635
3636void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3637{
3638 gl::makeCurrent(context, display, surface);
3639}
3640
3641gl::Context *glGetCurrentContext()
3642{
3643 return gl::getContext();
3644}
3645}