blob: 26a9ff2161f7e33499338827e6effa9d4e3ad520 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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
jbauman@chromium.org399c35f2011-04-28 23:19:51 +000034namespace
35{
36 enum { CLOSING_INDEX_BUFFER_SIZE = 4096 };
37}
38
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039namespace gl
40{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +000041Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042{
benvanik@google.com1a233342011-04-28 19:44:39 +000043 mFenceHandleAllocator.setBaseHandle(0);
44
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000046
daniel@transgaming.com428d1582010-05-04 03:35:25 +000047 mState.depthClearValue = 1.0f;
48 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049
daniel@transgaming.com428d1582010-05-04 03:35:25 +000050 mState.cullFace = false;
51 mState.cullMode = GL_BACK;
52 mState.frontFace = GL_CCW;
53 mState.depthTest = false;
54 mState.depthFunc = GL_LESS;
55 mState.blend = false;
56 mState.sourceBlendRGB = GL_ONE;
57 mState.sourceBlendAlpha = GL_ONE;
58 mState.destBlendRGB = GL_ZERO;
59 mState.destBlendAlpha = GL_ZERO;
60 mState.blendEquationRGB = GL_FUNC_ADD;
61 mState.blendEquationAlpha = GL_FUNC_ADD;
62 mState.blendColor.red = 0;
63 mState.blendColor.green = 0;
64 mState.blendColor.blue = 0;
65 mState.blendColor.alpha = 0;
66 mState.stencilTest = false;
67 mState.stencilFunc = GL_ALWAYS;
68 mState.stencilRef = 0;
69 mState.stencilMask = -1;
70 mState.stencilWritemask = -1;
71 mState.stencilBackFunc = GL_ALWAYS;
72 mState.stencilBackRef = 0;
73 mState.stencilBackMask = - 1;
74 mState.stencilBackWritemask = -1;
75 mState.stencilFail = GL_KEEP;
76 mState.stencilPassDepthFail = GL_KEEP;
77 mState.stencilPassDepthPass = GL_KEEP;
78 mState.stencilBackFail = GL_KEEP;
79 mState.stencilBackPassDepthFail = GL_KEEP;
80 mState.stencilBackPassDepthPass = GL_KEEP;
81 mState.polygonOffsetFill = false;
82 mState.polygonOffsetFactor = 0.0f;
83 mState.polygonOffsetUnits = 0.0f;
84 mState.sampleAlphaToCoverage = false;
85 mState.sampleCoverage = false;
86 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000087 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.scissorTest = false;
89 mState.dither = true;
90 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000091 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092
daniel@transgaming.com428d1582010-05-04 03:35:25 +000093 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000094
daniel@transgaming.com428d1582010-05-04 03:35:25 +000095 mState.viewportX = 0;
96 mState.viewportY = 0;
97 mState.viewportWidth = config->mDisplayMode.Width;
98 mState.viewportHeight = config->mDisplayMode.Height;
99 mState.zNear = 0.0f;
100 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000102 mState.scissorX = 0;
103 mState.scissorY = 0;
104 mState.scissorWidth = config->mDisplayMode.Width;
105 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000107 mState.colorMaskRed = true;
108 mState.colorMaskGreen = true;
109 mState.colorMaskBlue = true;
110 mState.colorMaskAlpha = true;
111 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000113 if (shareContext != NULL)
114 {
115 mResourceManager = shareContext->mResourceManager;
116 mResourceManager->addRef();
117 }
118 else
119 {
120 mResourceManager = new ResourceManager();
121 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 // [OpenGL ES 2.0.24] section 3.7 page 83:
124 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
125 // and cube map texture state vectors respectively associated with them.
126 // In order that access to these initial textures not be lost, they are treated as texture
127 // objects all of whose names are 0.
128
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000129 mTexture2DZero.set(new Texture2D(0));
130 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000132 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000133 bindArrayBuffer(0);
134 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindTextureCubeMap(0);
136 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000137 bindReadFramebuffer(0);
138 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 bindRenderbuffer(0);
140
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000141 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.packAlignment = 4;
144 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000145
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000147 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000148 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000149 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000150
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 mInvalidEnum = false;
152 mInvalidValue = false;
153 mInvalidOperation = false;
154 mOutOfMemory = false;
155 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000156
157 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000158
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000159 mSupportsCompressedTextures = false;
160 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000161 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000162 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000163 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
166Context::~Context()
167{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000168 if (mState.currentProgram != 0)
169 {
170 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
171 if (programObject)
172 {
173 programObject->release();
174 }
175 mState.currentProgram = 0;
176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000178 while (!mFramebufferMap.empty())
179 {
180 deleteFramebuffer(mFramebufferMap.begin()->first);
181 }
182
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000183 while (!mFenceMap.empty())
184 {
185 deleteFence(mFenceMap.begin()->first);
186 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000187
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000188 while (!mMultiSampleSupport.empty())
189 {
190 delete [] mMultiSampleSupport.begin()->second;
191 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
192 }
193
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000194 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000195 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000196 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000197 {
198 mState.samplerTexture[type][sampler].set(NULL);
199 }
200 }
201
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000202 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000203 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000204 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000205 }
206
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000207 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
208 {
209 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
210 }
211
212 mState.arrayBuffer.set(NULL);
213 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000214 mState.renderbuffer.set(NULL);
215
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000216 mTexture2DZero.set(NULL);
217 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000219 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000220 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000221 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000222 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000223
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000224 if (mMaskedClearSavedState)
225 {
226 mMaskedClearSavedState->Release();
227 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000228
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000229 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230}
231
232void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
233{
234 IDirect3DDevice9 *device = display->getDevice();
235
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000236 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000237 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000238 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000239
daniel@transgaming.com83921382011-01-08 05:46:00 +0000240 mVertexDataManager = new VertexDataManager(this, device);
241 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000242 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000243
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000244 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +0000245 mSupportsVertexTexture = display->getVertexTextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000246
247 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
248 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
249 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
250 mMaxRenderbufferDimension = mMaxTextureDimension;
251 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
252 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
253 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
254
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000255 const D3DFORMAT renderBufferFormats[] =
256 {
257 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000258 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000259 D3DFMT_R5G6B5,
260 D3DFMT_D24S8
261 };
262
263 int max = 0;
264 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
265 {
266 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
267 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
268 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
269
270 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
271 {
272 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
273 {
274 max = j;
275 }
276 }
277 }
278
279 mMaxSupportedSamples = max;
280
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000281 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000282 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000283 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
284 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000285 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
286 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000287
daniel@transgaming.com83921382011-01-08 05:46:00 +0000288 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
289
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000290 initExtensionString();
291
292 mState.viewportX = 0;
293 mState.viewportY = 0;
294 mState.viewportWidth = surface->getWidth();
295 mState.viewportHeight = surface->getHeight();
296
297 mState.scissorX = 0;
298 mState.scissorY = 0;
299 mState.scissorWidth = surface->getWidth();
300 mState.scissorHeight = surface->getHeight();
301
302 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000303 }
304
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
306 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000307 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000310 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000311 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312
313 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000315 if (defaultRenderTarget)
316 {
317 defaultRenderTarget->Release();
318 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000320 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000322 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000324
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000325 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326}
327
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000328// 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 +0000329void Context::markAllStateDirty()
330{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000331 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
332 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000333 mAppliedTextureSerialPS[t] = 0;
334 }
335
336 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
337 {
338 mAppliedTextureSerialVS[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{
benvanik@google.com1a233342011-04-28 19:44:39 +0000836 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000837
838 mFramebufferMap[handle] = NULL;
839
840 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841}
842
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000843GLuint Context::createFence()
844{
benvanik@google.com1a233342011-04-28 19:44:39 +0000845 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000846
847 mFenceMap[handle] = new Fence;
848
849 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000850}
851
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852void Context::deleteBuffer(GLuint buffer)
853{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000854 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855 {
856 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000858
859 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862void Context::deleteShader(GLuint shader)
863{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000864 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
867void Context::deleteProgram(GLuint program)
868{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870}
871
872void Context::deleteTexture(GLuint texture)
873{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000874 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 {
876 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000878
879 mResourceManager->deleteTexture(texture);
880}
881
882void Context::deleteRenderbuffer(GLuint renderbuffer)
883{
884 if (mResourceManager->getRenderbuffer(renderbuffer))
885 {
886 detachRenderbuffer(renderbuffer);
887 }
888
889 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890}
891
892void Context::deleteFramebuffer(GLuint framebuffer)
893{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000894 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
895
896 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897 {
898 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000899
benvanik@google.com1a233342011-04-28 19:44:39 +0000900 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000901 delete framebufferObject->second;
902 mFramebufferMap.erase(framebufferObject);
903 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000905
906void Context::deleteFence(GLuint fence)
907{
908 FenceMap::iterator fenceObject = mFenceMap.find(fence);
909
910 if (fenceObject != mFenceMap.end())
911 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000912 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000913 delete fenceObject->second;
914 mFenceMap.erase(fenceObject);
915 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000916}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000920 return mResourceManager->getBuffer(handle);
921}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000923Shader *Context::getShader(GLuint handle)
924{
925 return mResourceManager->getShader(handle);
926}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000928Program *Context::getProgram(GLuint handle)
929{
930 return mResourceManager->getProgram(handle);
931}
932
933Texture *Context::getTexture(GLuint handle)
934{
935 return mResourceManager->getTexture(handle);
936}
937
938Renderbuffer *Context::getRenderbuffer(GLuint handle)
939{
940 return mResourceManager->getRenderbuffer(handle);
941}
942
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000943Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000944{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000945 return getFramebuffer(mState.readFramebuffer);
946}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000947
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000948Framebuffer *Context::getDrawFramebuffer()
949{
950 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951}
952
953void Context::bindArrayBuffer(unsigned int buffer)
954{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000955 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000957 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958}
959
960void Context::bindElementArrayBuffer(unsigned int buffer)
961{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000962 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000964 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965}
966
967void Context::bindTexture2D(GLuint texture)
968{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000969 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000971 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
974void Context::bindTextureCubeMap(GLuint texture)
975{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000976 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000978 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979}
980
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000981void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000983 if (!getFramebuffer(framebuffer))
984 {
985 mFramebufferMap[framebuffer] = new Framebuffer();
986 }
987
988 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000989}
990
991void Context::bindDrawFramebuffer(GLuint framebuffer)
992{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000993 if (!getFramebuffer(framebuffer))
994 {
995 mFramebufferMap[framebuffer] = new Framebuffer();
996 }
997
998 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999}
1000
1001void Context::bindRenderbuffer(GLuint renderbuffer)
1002{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001003 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1004
1005 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006}
1007
1008void Context::useProgram(GLuint program)
1009{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001010 GLuint priorProgram = mState.currentProgram;
1011 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001012
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001013 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001015 Program *newProgram = mResourceManager->getProgram(program);
1016 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1017
1018 if (newProgram)
1019 {
1020 newProgram->addRef();
1021 }
1022
1023 if (oldProgram)
1024 {
1025 oldProgram->release();
1026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
1030void Context::setFramebufferZero(Framebuffer *buffer)
1031{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001032 delete mFramebufferMap[0];
1033 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034}
1035
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001036void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001038 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1039 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040}
1041
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001042Framebuffer *Context::getFramebuffer(unsigned int handle)
1043{
1044 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1045
1046 if (framebuffer == mFramebufferMap.end())
1047 {
1048 return NULL;
1049 }
1050 else
1051 {
1052 return framebuffer->second;
1053 }
1054}
1055
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001056Fence *Context::getFence(unsigned int handle)
1057{
1058 FenceMap::iterator fence = mFenceMap.find(handle);
1059
1060 if (fence == mFenceMap.end())
1061 {
1062 return NULL;
1063 }
1064 else
1065 {
1066 return fence->second;
1067 }
1068}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001069
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070Buffer *Context::getArrayBuffer()
1071{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001072 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073}
1074
1075Buffer *Context::getElementArrayBuffer()
1076{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001077 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078}
1079
1080Program *Context::getCurrentProgram()
1081{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001082 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083}
1084
1085Texture2D *Context::getTexture2D()
1086{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001087 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088}
1089
1090TextureCubeMap *Context::getTextureCubeMap()
1091{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001092 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093}
1094
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001095Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001097 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001098
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001099 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001100 {
1101 switch (type)
1102 {
1103 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001104 case TEXTURE_2D: return mTexture2DZero.get();
1105 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001106 }
1107 }
1108
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001109 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110}
1111
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001112bool Context::getBooleanv(GLenum pname, GLboolean *params)
1113{
1114 switch (pname)
1115 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001116 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1117 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1118 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001119 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001120 params[0] = mState.colorMaskRed;
1121 params[1] = mState.colorMaskGreen;
1122 params[2] = mState.colorMaskBlue;
1123 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001124 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001125 case GL_CULL_FACE: *params = mState.cullFace; break;
1126 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1127 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1128 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1129 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1130 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1131 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1132 case GL_BLEND: *params = mState.blend; break;
1133 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001134 default:
1135 return false;
1136 }
1137
1138 return true;
1139}
1140
1141bool Context::getFloatv(GLenum pname, GLfloat *params)
1142{
1143 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1144 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1145 // GetIntegerv as its native query function. As it would require conversion in any
1146 // case, this should make no difference to the calling application.
1147 switch (pname)
1148 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001149 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1150 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1151 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1152 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1153 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001154 case GL_ALIASED_LINE_WIDTH_RANGE:
1155 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1156 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1157 break;
1158 case GL_ALIASED_POINT_SIZE_RANGE:
1159 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001160 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 +00001161 break;
1162 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001163 params[0] = mState.zNear;
1164 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001165 break;
1166 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001167 params[0] = mState.colorClearValue.red;
1168 params[1] = mState.colorClearValue.green;
1169 params[2] = mState.colorClearValue.blue;
1170 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001171 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001172 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001173 params[0] = mState.blendColor.red;
1174 params[1] = mState.blendColor.green;
1175 params[2] = mState.blendColor.blue;
1176 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001177 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001178 default:
1179 return false;
1180 }
1181
1182 return true;
1183}
1184
1185bool Context::getIntegerv(GLenum pname, GLint *params)
1186{
1187 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1188 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1189 // GetIntegerv as its native query function. As it would require conversion in any
1190 // case, this should make no difference to the calling application. You may find it in
1191 // Context::getFloatv.
1192 switch (pname)
1193 {
1194 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1195 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001196 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001197 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1198 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001199 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001200 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001201 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001202 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001203 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001204 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1205 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001206 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1207 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1208 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001209 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001210 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1211 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1212 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1213 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001214 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001215 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1216 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1217 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1218 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1219 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1220 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1221 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1222 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1223 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1224 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1225 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1226 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1227 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1228 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1229 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1230 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1231 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1232 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1233 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1234 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1235 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1236 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1237 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1238 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001239 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1240 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001241 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1242 {
1243 if (supportsCompressedTextures())
1244 {
1245 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1246 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1247 *params = 2;
1248 }
1249 else
1250 {
1251 *params = 0;
1252 }
1253 }
1254 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001255 case GL_MAX_SAMPLES_ANGLE:
1256 {
1257 GLsizei maxSamples = getMaxSupportedSamples();
1258 if (maxSamples != 0)
1259 {
1260 *params = maxSamples;
1261 }
1262 else
1263 {
1264 return false;
1265 }
1266
1267 break;
1268 }
1269 case GL_SAMPLE_BUFFERS:
1270 case GL_SAMPLES:
1271 {
1272 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1273 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1274 {
1275 switch (pname)
1276 {
1277 case GL_SAMPLE_BUFFERS:
1278 if (framebuffer->getSamples() != 0)
1279 {
1280 *params = 1;
1281 }
1282 else
1283 {
1284 *params = 0;
1285 }
1286 break;
1287 case GL_SAMPLES:
1288 *params = framebuffer->getSamples();
1289 break;
1290 }
1291 }
1292 else
1293 {
1294 *params = 0;
1295 }
1296 }
1297 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001298 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1299 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1300 case GL_MAX_VIEWPORT_DIMS:
1301 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001302 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001303 params[0] = maxDimension;
1304 params[1] = maxDimension;
1305 }
1306 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001307 case GL_COMPRESSED_TEXTURE_FORMATS:
1308 {
1309 if (supportsCompressedTextures())
1310 {
1311 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1312 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1313 }
1314 }
1315 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001316 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001317 params[0] = mState.viewportX;
1318 params[1] = mState.viewportY;
1319 params[2] = mState.viewportWidth;
1320 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001321 break;
1322 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001323 params[0] = mState.scissorX;
1324 params[1] = mState.scissorY;
1325 params[2] = mState.scissorWidth;
1326 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001327 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001328 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1329 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001330 case GL_RED_BITS:
1331 case GL_GREEN_BITS:
1332 case GL_BLUE_BITS:
1333 case GL_ALPHA_BITS:
1334 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001335 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001336 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1337
1338 if (colorbuffer)
1339 {
1340 switch (pname)
1341 {
1342 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1343 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1344 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1345 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1346 }
1347 }
1348 else
1349 {
1350 *params = 0;
1351 }
1352 }
1353 break;
1354 case GL_DEPTH_BITS:
1355 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001356 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001357 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001358
1359 if (depthbuffer)
1360 {
1361 *params = depthbuffer->getDepthSize();
1362 }
1363 else
1364 {
1365 *params = 0;
1366 }
1367 }
1368 break;
1369 case GL_STENCIL_BITS:
1370 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001371 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001372 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001373
1374 if (stencilbuffer)
1375 {
1376 *params = stencilbuffer->getStencilSize();
1377 }
1378 else
1379 {
1380 *params = 0;
1381 }
1382 }
1383 break;
1384 case GL_TEXTURE_BINDING_2D:
1385 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001386 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001387 {
1388 error(GL_INVALID_OPERATION);
1389 return false;
1390 }
1391
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001392 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001393 }
1394 break;
1395 case GL_TEXTURE_BINDING_CUBE_MAP:
1396 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001397 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001398 {
1399 error(GL_INVALID_OPERATION);
1400 return false;
1401 }
1402
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001403 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001404 }
1405 break;
1406 default:
1407 return false;
1408 }
1409
1410 return true;
1411}
1412
1413bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1414{
1415 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1416 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1417 // to the fact that it is stored internally as a float, and so would require conversion
1418 // if returned from Context::getIntegerv. Since this conversion is already implemented
1419 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1420 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1421 // application.
1422 switch (pname)
1423 {
1424 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1425 case GL_SHADER_BINARY_FORMATS:
1426 {
1427 *type = GL_INT;
1428 *numParams = 0;
1429 }
1430 break;
1431 case GL_MAX_VERTEX_ATTRIBS:
1432 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1433 case GL_MAX_VARYING_VECTORS:
1434 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1435 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1436 case GL_MAX_TEXTURE_IMAGE_UNITS:
1437 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1438 case GL_MAX_RENDERBUFFER_SIZE:
1439 case GL_NUM_SHADER_BINARY_FORMATS:
1440 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1441 case GL_ARRAY_BUFFER_BINDING:
1442 case GL_FRAMEBUFFER_BINDING:
1443 case GL_RENDERBUFFER_BINDING:
1444 case GL_CURRENT_PROGRAM:
1445 case GL_PACK_ALIGNMENT:
1446 case GL_UNPACK_ALIGNMENT:
1447 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001448 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001449 case GL_RED_BITS:
1450 case GL_GREEN_BITS:
1451 case GL_BLUE_BITS:
1452 case GL_ALPHA_BITS:
1453 case GL_DEPTH_BITS:
1454 case GL_STENCIL_BITS:
1455 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1456 case GL_CULL_FACE_MODE:
1457 case GL_FRONT_FACE:
1458 case GL_ACTIVE_TEXTURE:
1459 case GL_STENCIL_FUNC:
1460 case GL_STENCIL_VALUE_MASK:
1461 case GL_STENCIL_REF:
1462 case GL_STENCIL_FAIL:
1463 case GL_STENCIL_PASS_DEPTH_FAIL:
1464 case GL_STENCIL_PASS_DEPTH_PASS:
1465 case GL_STENCIL_BACK_FUNC:
1466 case GL_STENCIL_BACK_VALUE_MASK:
1467 case GL_STENCIL_BACK_REF:
1468 case GL_STENCIL_BACK_FAIL:
1469 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1470 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1471 case GL_DEPTH_FUNC:
1472 case GL_BLEND_SRC_RGB:
1473 case GL_BLEND_SRC_ALPHA:
1474 case GL_BLEND_DST_RGB:
1475 case GL_BLEND_DST_ALPHA:
1476 case GL_BLEND_EQUATION_RGB:
1477 case GL_BLEND_EQUATION_ALPHA:
1478 case GL_STENCIL_WRITEMASK:
1479 case GL_STENCIL_BACK_WRITEMASK:
1480 case GL_STENCIL_CLEAR_VALUE:
1481 case GL_SUBPIXEL_BITS:
1482 case GL_MAX_TEXTURE_SIZE:
1483 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1484 case GL_SAMPLE_BUFFERS:
1485 case GL_SAMPLES:
1486 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1487 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1488 case GL_TEXTURE_BINDING_2D:
1489 case GL_TEXTURE_BINDING_CUBE_MAP:
1490 {
1491 *type = GL_INT;
1492 *numParams = 1;
1493 }
1494 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001495 case GL_MAX_SAMPLES_ANGLE:
1496 {
1497 if (getMaxSupportedSamples() != 0)
1498 {
1499 *type = GL_INT;
1500 *numParams = 1;
1501 }
1502 else
1503 {
1504 return false;
1505 }
1506 }
1507 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001508 case GL_MAX_VIEWPORT_DIMS:
1509 {
1510 *type = GL_INT;
1511 *numParams = 2;
1512 }
1513 break;
1514 case GL_VIEWPORT:
1515 case GL_SCISSOR_BOX:
1516 {
1517 *type = GL_INT;
1518 *numParams = 4;
1519 }
1520 break;
1521 case GL_SHADER_COMPILER:
1522 case GL_SAMPLE_COVERAGE_INVERT:
1523 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001524 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1525 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1526 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1527 case GL_SAMPLE_COVERAGE:
1528 case GL_SCISSOR_TEST:
1529 case GL_STENCIL_TEST:
1530 case GL_DEPTH_TEST:
1531 case GL_BLEND:
1532 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001533 {
1534 *type = GL_BOOL;
1535 *numParams = 1;
1536 }
1537 break;
1538 case GL_COLOR_WRITEMASK:
1539 {
1540 *type = GL_BOOL;
1541 *numParams = 4;
1542 }
1543 break;
1544 case GL_POLYGON_OFFSET_FACTOR:
1545 case GL_POLYGON_OFFSET_UNITS:
1546 case GL_SAMPLE_COVERAGE_VALUE:
1547 case GL_DEPTH_CLEAR_VALUE:
1548 case GL_LINE_WIDTH:
1549 {
1550 *type = GL_FLOAT;
1551 *numParams = 1;
1552 }
1553 break;
1554 case GL_ALIASED_LINE_WIDTH_RANGE:
1555 case GL_ALIASED_POINT_SIZE_RANGE:
1556 case GL_DEPTH_RANGE:
1557 {
1558 *type = GL_FLOAT;
1559 *numParams = 2;
1560 }
1561 break;
1562 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001563 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001564 {
1565 *type = GL_FLOAT;
1566 *numParams = 4;
1567 }
1568 break;
1569 default:
1570 return false;
1571 }
1572
1573 return true;
1574}
1575
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001576// Applies the render target surface, depth stencil surface, viewport rectangle and
1577// scissor rectangle to the Direct3D 9 device
1578bool Context::applyRenderTarget(bool ignoreViewport)
1579{
1580 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001581
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001582 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001583
1584 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1585 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001586 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587 }
1588
1589 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001590
1591 if (!renderTarget)
1592 {
1593 return false; // Context must be lost
1594 }
1595
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001596 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001598 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1599 if (renderTargetSerial != mAppliedRenderTargetSerial)
1600 {
1601 device->SetRenderTarget(0, renderTarget);
1602 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001603 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 +00001604 }
1605
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001606 unsigned int depthbufferSerial = 0;
1607 unsigned int stencilbufferSerial = 0;
1608 if (framebufferObject->getDepthbufferType() != GL_NONE)
1609 {
1610 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001611 if (!depthStencil)
1612 {
1613 ERR("Depth stencil pointer unexpectedly null.");
1614 return false;
1615 }
1616
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001617 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1618 }
1619 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1620 {
1621 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001622 if (!depthStencil)
1623 {
1624 ERR("Depth stencil pointer unexpectedly null.");
1625 return false;
1626 }
1627
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001628 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1629 }
1630
1631 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001632 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001633 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001634 {
1635 device->SetDepthStencilSurface(depthStencil);
1636 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001637 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001638 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001639 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640
1641 D3DVIEWPORT9 viewport;
1642 D3DSURFACE_DESC desc;
1643 renderTarget->GetDesc(&desc);
1644
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001645 float zNear = clamp01(mState.zNear);
1646 float zFar = clamp01(mState.zFar);
1647
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648 if (ignoreViewport)
1649 {
1650 viewport.X = 0;
1651 viewport.Y = 0;
1652 viewport.Width = desc.Width;
1653 viewport.Height = desc.Height;
1654 viewport.MinZ = 0.0f;
1655 viewport.MaxZ = 1.0f;
1656 }
1657 else
1658 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001659 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1660 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1661 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1662 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1663 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 +00001664 viewport.MinZ = zNear;
1665 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666 }
1667
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001668 if (viewport.Width <= 0 || viewport.Height <= 0)
1669 {
1670 return false; // Nothing to render
1671 }
1672
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673 device->SetViewport(&viewport);
1674
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001675 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001677 if (mState.scissorTest)
1678 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001679 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1680 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1681 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1682 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1683 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001684 device->SetScissorRect(&rect);
1685 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1686 }
1687 else
1688 {
1689 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1690 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001691
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001692 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693 }
1694
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 Program *programObject = getCurrentProgram();
1698
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001699 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001700 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001701 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001702
daniel@transgaming.com31754962010-11-28 02:02:52 +00001703 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001704 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1705 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1706 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001707 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001708
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001709 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001710 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001711 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001712
daniel@transgaming.com31754962010-11-28 02:02:52 +00001713 GLint depthRange = programObject->getDxDepthRangeLocation();
1714 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1715 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716 }
1717
1718 return true;
1719}
1720
1721// 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 +00001722void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723{
1724 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001725 Program *programObject = getCurrentProgram();
1726
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001727 Framebuffer *framebufferObject = getDrawFramebuffer();
1728
1729 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1730
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001731 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001732 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001733 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001735 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001736 GLint alwaysFront = !isTriangleMode(drawMode);
1737 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1738
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001739 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001743 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744 }
1745 else
1746 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748 }
1749
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001750 mCullStateDirty = false;
1751 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001753 if (mDepthStateDirty)
1754 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001755 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1758 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 }
1760 else
1761 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001762 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001764
1765 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 }
1767
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001768 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001770 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001771 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001772 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1773
1774 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1775 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1776 {
1777 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1778 }
1779 else
1780 {
1781 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1782 unorm<8>(mState.blendColor.alpha),
1783 unorm<8>(mState.blendColor.alpha),
1784 unorm<8>(mState.blendColor.alpha)));
1785 }
1786
1787 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1788 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1789 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1790
1791 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1792 mState.destBlendRGB != mState.destBlendAlpha ||
1793 mState.blendEquationRGB != mState.blendEquationAlpha)
1794 {
1795 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1796
1797 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1798 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1799 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001800 }
1801 else
1802 {
1803 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1804 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001805 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001806 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001807 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001808 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001809 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001810
1811 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812 }
1813
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001814 if (mStencilStateDirty || mFrontFaceDirty)
1815 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001816 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001817 {
1818 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1819 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1820
1821 // FIXME: Unsupported by D3D9
1822 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1823 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1824 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1825 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1826 mState.stencilRef != mState.stencilBackRef ||
1827 mState.stencilMask != mState.stencilBackMask)
1828 {
1829 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1830 return error(GL_INVALID_OPERATION);
1831 }
1832
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001833 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001834 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001835 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1836
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001837 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1838 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001839 es2dx::ConvertComparison(mState.stencilFunc));
1840
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001841 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1842 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001843
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001844 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001845 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001846 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001847 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001848 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001849 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1850
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001851 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1852 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001853 es2dx::ConvertComparison(mState.stencilBackFunc));
1854
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001855 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1856 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001857
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001858 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001859 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001860 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001861 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001862 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001863 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1864 }
1865 else
1866 {
1867 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1868 }
1869
1870 mStencilStateDirty = false;
1871 }
1872
1873 if (mMaskStateDirty)
1874 {
1875 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1876 mState.colorMaskBlue, mState.colorMaskAlpha));
1877 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1878
1879 mMaskStateDirty = false;
1880 }
1881
1882 if (mPolygonOffsetStateDirty)
1883 {
1884 if (mState.polygonOffsetFill)
1885 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001886 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001887 if (depthbuffer)
1888 {
1889 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1890 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1891 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1892 }
1893 }
1894 else
1895 {
1896 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1897 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1898 }
1899
1900 mPolygonOffsetStateDirty = false;
1901 }
1902
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001903 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001905 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001906 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001907 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001908 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001909 FIXME("Sample alpha to coverage is unimplemented.");
1910 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001911
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001912 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1913 if (mState.sampleCoverage)
1914 {
1915 unsigned int mask = 0;
1916 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001917 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001918 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001919
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001920 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001921 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001922 mask <<= 1;
1923
1924 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1925 {
1926 threshold += 1.0f;
1927 mask |= 1;
1928 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001929 }
1930 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001931
1932 if (mState.sampleCoverageInvert)
1933 {
1934 mask = ~mask;
1935 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001936
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001937 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1938 }
1939 else
1940 {
1941 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1942 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001943 }
1944 else
1945 {
1946 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001947 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001948
1949 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001950 }
1951
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001952 if (mDitherStateDirty)
1953 {
1954 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1955
1956 mDitherStateDirty = false;
1957 }
1958
1959 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960}
1961
daniel@transgaming.com83921382011-01-08 05:46:00 +00001962GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001963{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001964 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001965
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001966 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001967 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001968 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001969 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001970 }
1971
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00001972 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001973}
1974
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001975// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001976GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001977{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001978 IDirect3DDevice9 *device = getDevice();
1979 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001980
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001981 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001982 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001983 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001984 }
1985
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001986 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987}
1988
1989// Applies the shaders and shader constants to the Direct3D 9 device
1990void Context::applyShaders()
1991{
1992 IDirect3DDevice9 *device = getDevice();
1993 Program *programObject = getCurrentProgram();
1994 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1995 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1996
1997 device->SetVertexShader(vertexShader);
1998 device->SetPixelShader(pixelShader);
1999
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002000 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002001 {
2002 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002003 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002004 }
2005
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006 programObject->applyUniforms();
2007}
2008
2009// Applies the textures and sampler states to the Direct3D 9 device
2010void Context::applyTextures()
2011{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002012 applyTextures(SAMPLER_PIXEL);
2013
2014 if (mSupportsVertexTexture)
2015 {
2016 applyTextures(SAMPLER_VERTEX);
2017 }
2018}
2019
2020void Context::applyTextures(SamplerType type)
2021{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022 IDirect3DDevice9 *device = getDevice();
2023 Program *programObject = getCurrentProgram();
2024
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002025 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF;
2026
2027 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002029 int textureUnit = programObject->getSamplerMapping(type, samplerIndex);
2030 int d3dSampler = (type == SAMPLER_PIXEL) ? samplerIndex : D3DVERTEXTEXTURESAMPLER0 + samplerIndex;
2031 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2032
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002033 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002035 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002036
2037 Texture *texture = getSamplerTexture(textureUnit, textureType);
2038
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002039 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002040 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002041 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2042
2043 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002044 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002045 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002046 {
2047 GLenum wrapS = texture->getWrapS();
2048 GLenum wrapT = texture->getWrapT();
2049 GLenum minFilter = texture->getMinFilter();
2050 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002052 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2053 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002055 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002056 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2057 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002058 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2059 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002060 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002061
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002062 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002063 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002064 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002065 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002066 }
2067 else
2068 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002069 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002070 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002071
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002072 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002073 texture->resetDirty();
2074 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002076 else
2077 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002078 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002079 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002080 device->SetTexture(d3dSampler, NULL);
2081 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002082 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002083 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 }
2085}
2086
2087void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2088{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002089 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002090
2091 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2092 {
2093 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2094 }
2095
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002096 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2097 {
2098 return error(GL_INVALID_OPERATION);
2099 }
2100
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002102
2103 if (!renderTarget)
2104 {
2105 return; // Context must be lost, return silently
2106 }
2107
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108 IDirect3DDevice9 *device = getDevice();
2109
2110 D3DSURFACE_DESC desc;
2111 renderTarget->GetDesc(&desc);
2112
2113 IDirect3DSurface9 *systemSurface;
2114 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2115
2116 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2117 {
2118 return error(GL_OUT_OF_MEMORY);
2119 }
2120
2121 ASSERT(SUCCEEDED(result));
2122
2123 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2124 {
2125 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2126 }
2127
2128 result = device->GetRenderTargetData(renderTarget, systemSurface);
2129
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130 if (FAILED(result))
2131 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132 systemSurface->Release();
2133
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002134 switch (result)
2135 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002136 case D3DERR_DRIVERINTERNALERROR:
2137 case D3DERR_DEVICELOST:
2138 return error(GL_OUT_OF_MEMORY);
2139 default:
2140 UNREACHABLE();
2141 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002142 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002143 }
2144
2145 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002146 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2147 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2148 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2149 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2150 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151
2152 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2153
2154 if (FAILED(result))
2155 {
2156 UNREACHABLE();
2157 systemSurface->Release();
2158
2159 return; // No sensible error to generate
2160 }
2161
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002162 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002164 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002165 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002166 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002167
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 for (int j = 0; j < rect.bottom - rect.top; j++)
2169 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002170 if (desc.Format == D3DFMT_A8R8G8B8 &&
2171 format == GL_BGRA_EXT &&
2172 type == GL_UNSIGNED_BYTE)
2173 {
2174 // Fast path for EXT_read_format_bgra, given
2175 // an RGBA source buffer. Note that buffers with no
2176 // alpha go through the slow path below.
2177 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002178 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002179 (rect.right - rect.left) * 4);
2180 continue;
2181 }
2182
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002183 for (int i = 0; i < rect.right - rect.left; i++)
2184 {
2185 float r;
2186 float g;
2187 float b;
2188 float a;
2189
2190 switch (desc.Format)
2191 {
2192 case D3DFMT_R5G6B5:
2193 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002194 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002195
2196 a = 1.0f;
2197 b = (rgb & 0x001F) * (1.0f / 0x001F);
2198 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2199 r = (rgb & 0xF800) * (1.0f / 0xF800);
2200 }
2201 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002202 case D3DFMT_A1R5G5B5:
2203 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002204 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205
2206 a = (argb & 0x8000) ? 1.0f : 0.0f;
2207 b = (argb & 0x001F) * (1.0f / 0x001F);
2208 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2209 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2210 }
2211 break;
2212 case D3DFMT_A8R8G8B8:
2213 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002214 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002215
2216 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2217 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2218 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2219 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2220 }
2221 break;
2222 case D3DFMT_X8R8G8B8:
2223 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002224 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002225
2226 a = 1.0f;
2227 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2228 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2229 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2230 }
2231 break;
2232 case D3DFMT_A2R10G10B10:
2233 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002234 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002235
2236 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2237 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2238 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2239 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2240 }
2241 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002242 case D3DFMT_A32B32G32R32F:
2243 {
2244 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002245 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2246 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2247 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2248 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002249 }
2250 break;
2251 case D3DFMT_A16B16G16R16F:
2252 {
2253 // float formats in D3D are stored rgba, rather than the other way round
2254 float abgr[4];
2255
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002256 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002257
2258 a = abgr[3];
2259 b = abgr[2];
2260 g = abgr[1];
2261 r = abgr[0];
2262 }
2263 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 default:
2265 UNIMPLEMENTED(); // FIXME
2266 UNREACHABLE();
2267 }
2268
2269 switch (format)
2270 {
2271 case GL_RGBA:
2272 switch (type)
2273 {
2274 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002275 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2276 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2277 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2278 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 break;
2280 default: UNREACHABLE();
2281 }
2282 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002283 case GL_BGRA_EXT:
2284 switch (type)
2285 {
2286 case GL_UNSIGNED_BYTE:
2287 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2288 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2289 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2290 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2291 break;
2292 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2293 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2294 // this type is packed as follows:
2295 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2296 // --------------------------------------------------------------------------------
2297 // | 4th | 3rd | 2nd | 1st component |
2298 // --------------------------------------------------------------------------------
2299 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2300 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2301 ((unsigned short)(15 * a + 0.5f) << 12)|
2302 ((unsigned short)(15 * r + 0.5f) << 8) |
2303 ((unsigned short)(15 * g + 0.5f) << 4) |
2304 ((unsigned short)(15 * b + 0.5f) << 0);
2305 break;
2306 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2307 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2308 // this type is packed as follows:
2309 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2310 // --------------------------------------------------------------------------------
2311 // | 4th | 3rd | 2nd | 1st component |
2312 // --------------------------------------------------------------------------------
2313 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2314 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2315 ((unsigned short)( a + 0.5f) << 15) |
2316 ((unsigned short)(31 * r + 0.5f) << 10) |
2317 ((unsigned short)(31 * g + 0.5f) << 5) |
2318 ((unsigned short)(31 * b + 0.5f) << 0);
2319 break;
2320 default: UNREACHABLE();
2321 }
2322 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002323 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002324 switch (type)
2325 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002326 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002327 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2328 ((unsigned short)(31 * b + 0.5f) << 0) |
2329 ((unsigned short)(63 * g + 0.5f) << 5) |
2330 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331 break;
2332 default: UNREACHABLE();
2333 }
2334 break;
2335 default: UNREACHABLE();
2336 }
2337 }
2338 }
2339
2340 systemSurface->UnlockRect();
2341
2342 systemSurface->Release();
2343}
2344
2345void Context::clear(GLbitfield mask)
2346{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002347 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002348
2349 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2350 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002351 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002352 }
2353
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002354 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 IDirect3DDevice9 *device = getDevice();
2356 DWORD flags = 0;
2357
2358 if (mask & GL_COLOR_BUFFER_BIT)
2359 {
2360 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002361
2362 if (framebufferObject->getColorbufferType() != GL_NONE)
2363 {
2364 flags |= D3DCLEAR_TARGET;
2365 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366 }
2367
2368 if (mask & GL_DEPTH_BUFFER_BIT)
2369 {
2370 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002371 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372 {
2373 flags |= D3DCLEAR_ZBUFFER;
2374 }
2375 }
2376
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377 GLuint stencilUnmasked = 0x0;
2378
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002379 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002382 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002384 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002385 if (!depthStencil)
2386 {
2387 ERR("Depth stencil pointer unexpectedly null.");
2388 return;
2389 }
2390
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002391 D3DSURFACE_DESC desc;
2392 depthStencil->GetDesc(&desc);
2393
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002394 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002395 stencilUnmasked = (0x1 << stencilSize) - 1;
2396
2397 if (stencilUnmasked != 0x0)
2398 {
2399 flags |= D3DCLEAR_STENCIL;
2400 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401 }
2402 }
2403
2404 if (mask != 0)
2405 {
2406 return error(GL_INVALID_VALUE);
2407 }
2408
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002409 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2410 {
2411 return;
2412 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002414 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002415 unorm<8>(mState.colorClearValue.red),
2416 unorm<8>(mState.colorClearValue.green),
2417 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002418 float depth = clamp01(mState.depthClearValue);
2419 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420
2421 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2422
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002423 if (!renderTarget)
2424 {
2425 return; // Context must be lost, return silently
2426 }
2427
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 D3DSURFACE_DESC desc;
2429 renderTarget->GetDesc(&desc);
2430
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002431 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432
2433 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002434 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002436 !(mState.colorMaskRed && mState.colorMaskGreen &&
2437 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438
2439 if (needMaskedColorClear || needMaskedStencilClear)
2440 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002441 // State which is altered in all paths from this point to the clear call is saved.
2442 // State which is altered in only some paths will be flagged dirty in the case that
2443 // that path is taken.
2444 HRESULT hr;
2445 if (mMaskedClearSavedState == NULL)
2446 {
2447 hr = device->BeginStateBlock();
2448 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2449
2450 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2451 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2452 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2453 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2454 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2455 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2456 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2457 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2458 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2459 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2460 device->SetPixelShader(NULL);
2461 device->SetVertexShader(NULL);
2462 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002463 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2464 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2465 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2466 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2467 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2468 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2469 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002470
2471 hr = device->EndStateBlock(&mMaskedClearSavedState);
2472 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2473 }
2474
2475 ASSERT(mMaskedClearSavedState != NULL);
2476
2477 if (mMaskedClearSavedState != NULL)
2478 {
2479 hr = mMaskedClearSavedState->Capture();
2480 ASSERT(SUCCEEDED(hr));
2481 }
2482
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2484 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2485 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2486 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2487 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2488 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2489 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2490 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2491
2492 if (flags & D3DCLEAR_TARGET)
2493 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002494 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002495 }
2496 else
2497 {
2498 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2499 }
2500
2501 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2502 {
2503 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2504 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2505 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2506 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002507 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002508 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002509 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2510 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002511 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 }
2513 else
2514 {
2515 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2516 }
2517
2518 device->SetPixelShader(NULL);
2519 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002520 device->SetFVF(D3DFVF_XYZRHW);
2521 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2522 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2523 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2524 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2525 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2526 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2527 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002528
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002529 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2530 quad[0][0] = -0.5f;
2531 quad[0][1] = desc.Height - 0.5f;
2532 quad[0][2] = 0.0f;
2533 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002535 quad[1][0] = desc.Width - 0.5f;
2536 quad[1][1] = desc.Height - 0.5f;
2537 quad[1][2] = 0.0f;
2538 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002540 quad[2][0] = -0.5f;
2541 quad[2][1] = -0.5f;
2542 quad[2][2] = 0.0f;
2543 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002545 quad[3][0] = desc.Width - 0.5f;
2546 quad[3][1] = -0.5f;
2547 quad[3][2] = 0.0f;
2548 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002549
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002550 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002551 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552
2553 if (flags & D3DCLEAR_ZBUFFER)
2554 {
2555 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2556 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2557 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2558 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002559
2560 if (mMaskedClearSavedState != NULL)
2561 {
2562 mMaskedClearSavedState->Apply();
2563 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002565 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002566 {
2567 device->Clear(0, NULL, flags, color, depth, stencil);
2568 }
2569}
2570
2571void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2572{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002573 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002574 {
2575 return error(GL_INVALID_OPERATION);
2576 }
2577
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002578 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579 IDirect3DDevice9 *device = getDevice();
2580 D3DPRIMITIVETYPE primitiveType;
2581 int primitiveCount;
2582
2583 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2584 return error(GL_INVALID_ENUM);
2585
2586 if (primitiveCount <= 0)
2587 {
2588 return;
2589 }
2590
2591 if (!applyRenderTarget(false))
2592 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002593 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594 }
2595
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002596 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002597
daniel@transgaming.com83921382011-01-08 05:46:00 +00002598 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002599 if (err != GL_NO_ERROR)
2600 {
2601 return error(err);
2602 }
2603
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604 applyShaders();
2605 applyTextures();
2606
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002607 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002608 {
2609 return error(GL_INVALID_OPERATION);
2610 }
2611
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002612 if (!cullSkipsDraw(mode))
2613 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002614 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002615
2616 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002617
2618 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2619 {
2620 drawClosingLine(first, first + count - 1);
2621 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002622 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002623}
2624
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002625void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002627 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002628 {
2629 return error(GL_INVALID_OPERATION);
2630 }
2631
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002632 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633 {
2634 return error(GL_INVALID_OPERATION);
2635 }
2636
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002637 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002638 IDirect3DDevice9 *device = getDevice();
2639 D3DPRIMITIVETYPE primitiveType;
2640 int primitiveCount;
2641
2642 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2643 return error(GL_INVALID_ENUM);
2644
2645 if (primitiveCount <= 0)
2646 {
2647 return;
2648 }
2649
2650 if (!applyRenderTarget(false))
2651 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002652 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653 }
2654
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002655 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002656
2657 TranslatedIndexData indexInfo;
2658 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2659 if (err != GL_NO_ERROR)
2660 {
2661 return error(err);
2662 }
2663
daniel@transgaming.com83921382011-01-08 05:46:00 +00002664 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2665 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002666 if (err != GL_NO_ERROR)
2667 {
2668 return error(err);
2669 }
2670
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671 applyShaders();
2672 applyTextures();
2673
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002674 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002675 {
2676 return error(GL_INVALID_OPERATION);
2677 }
2678
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002679 if (!cullSkipsDraw(mode))
2680 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002681 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002682
daniel@transgaming.com83921382011-01-08 05:46:00 +00002683 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002684
2685 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2686 {
2687 drawClosingLine(count, type, indices);
2688 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002689 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690}
2691
2692void Context::finish()
2693{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002694 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002695 IDirect3DDevice9 *device = getDevice();
2696 IDirect3DQuery9 *occlusionQuery = NULL;
2697
2698 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2699
2700 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2701 {
2702 return error(GL_OUT_OF_MEMORY);
2703 }
2704
2705 ASSERT(SUCCEEDED(result));
2706
2707 if (occlusionQuery)
2708 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002709 IDirect3DStateBlock9 *savedState = NULL;
2710 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2711
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002712 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2713 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714
2715 // Render something outside the render target
2716 device->SetPixelShader(NULL);
2717 device->SetVertexShader(NULL);
2718 device->SetFVF(D3DFVF_XYZRHW);
2719 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002720 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002722
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002723 result = occlusionQuery->Issue(D3DISSUE_END);
2724 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725
2726 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2727 {
2728 // Keep polling, but allow other threads to do something useful first
2729 Sleep(0);
2730 }
2731
2732 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002733
2734 if (savedState)
2735 {
2736 savedState->Apply();
2737 savedState->Release();
2738 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002739 }
2740}
2741
2742void Context::flush()
2743{
2744 IDirect3DDevice9 *device = getDevice();
2745 IDirect3DQuery9 *eventQuery = NULL;
2746
2747 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2748
2749 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2750 {
2751 return error(GL_OUT_OF_MEMORY);
2752 }
2753
2754 ASSERT(SUCCEEDED(result));
2755
2756 if (eventQuery)
2757 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002758 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2759 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002761 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002763
2764 if (result == D3DERR_DEVICELOST)
2765 {
2766 error(GL_OUT_OF_MEMORY);
2767 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768 }
2769}
2770
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002771void Context::drawClosingLine(unsigned int first, unsigned int last)
2772{
2773 IDirect3DDevice9 *device = getDevice();
2774 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002775 bool succeeded = false;
2776 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002777
2778 if (supports32bitIndices())
2779 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002780 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002781
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002782 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002783 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002784 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2785 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002786
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002787 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2788
2789 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2790 if (data)
2791 {
2792 data[0] = last;
2793 data[1] = first;
2794 mClosingIB->unmap();
2795 offset /= 4;
2796 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002797 }
2798 }
2799 else
2800 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002801 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002802
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002803 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002804 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002805 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2806 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002807
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002808 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2809
2810 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2811 if (data)
2812 {
2813 data[0] = last;
2814 data[1] = first;
2815 mClosingIB->unmap();
2816 offset /= 2;
2817 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002818 }
2819 }
2820
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002821 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002822 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002823 device->SetIndices(mClosingIB->getBuffer());
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002824
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002825 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002826 }
2827 else
2828 {
2829 ERR("Could not create an index buffer for closing a line loop.");
2830 error(GL_OUT_OF_MEMORY);
2831 }
2832}
2833
2834void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2835{
2836 unsigned int first = 0;
2837 unsigned int last = 0;
2838
2839 if (mState.elementArrayBuffer.get())
2840 {
2841 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2842 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2843 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2844 }
2845
2846 switch (type)
2847 {
2848 case GL_UNSIGNED_BYTE:
2849 first = static_cast<const GLubyte*>(indices)[0];
2850 last = static_cast<const GLubyte*>(indices)[count - 1];
2851 break;
2852 case GL_UNSIGNED_SHORT:
2853 first = static_cast<const GLushort*>(indices)[0];
2854 last = static_cast<const GLushort*>(indices)[count - 1];
2855 break;
2856 case GL_UNSIGNED_INT:
2857 first = static_cast<const GLuint*>(indices)[0];
2858 last = static_cast<const GLuint*>(indices)[count - 1];
2859 break;
2860 default: UNREACHABLE();
2861 }
2862
2863 drawClosingLine(first, last);
2864}
2865
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002866void Context::recordInvalidEnum()
2867{
2868 mInvalidEnum = true;
2869}
2870
2871void Context::recordInvalidValue()
2872{
2873 mInvalidValue = true;
2874}
2875
2876void Context::recordInvalidOperation()
2877{
2878 mInvalidOperation = true;
2879}
2880
2881void Context::recordOutOfMemory()
2882{
2883 mOutOfMemory = true;
2884}
2885
2886void Context::recordInvalidFramebufferOperation()
2887{
2888 mInvalidFramebufferOperation = true;
2889}
2890
2891// Get one of the recorded errors and clear its flag, if any.
2892// [OpenGL ES 2.0.24] section 2.5 page 13.
2893GLenum Context::getError()
2894{
2895 if (mInvalidEnum)
2896 {
2897 mInvalidEnum = false;
2898
2899 return GL_INVALID_ENUM;
2900 }
2901
2902 if (mInvalidValue)
2903 {
2904 mInvalidValue = false;
2905
2906 return GL_INVALID_VALUE;
2907 }
2908
2909 if (mInvalidOperation)
2910 {
2911 mInvalidOperation = false;
2912
2913 return GL_INVALID_OPERATION;
2914 }
2915
2916 if (mOutOfMemory)
2917 {
2918 mOutOfMemory = false;
2919
2920 return GL_OUT_OF_MEMORY;
2921 }
2922
2923 if (mInvalidFramebufferOperation)
2924 {
2925 mInvalidFramebufferOperation = false;
2926
2927 return GL_INVALID_FRAMEBUFFER_OPERATION;
2928 }
2929
2930 return GL_NO_ERROR;
2931}
2932
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002933bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002934{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002935 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002936}
2937
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002938int Context::getMaximumVaryingVectors() const
2939{
2940 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2941}
2942
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002943int Context::getMaximumVertexTextureImageUnits() const
2944{
2945 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
2946}
2947
2948int Context::getMaximumCombinedTextureImageUnits() const
2949{
2950 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
2951}
2952
daniel@transgaming.com458da142010-11-28 02:03:02 +00002953int Context::getMaximumFragmentUniformVectors() const
2954{
2955 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2956}
2957
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002958int Context::getMaxSupportedSamples() const
2959{
2960 return mMaxSupportedSamples;
2961}
2962
2963int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2964{
2965 if (requested == 0)
2966 {
2967 return requested;
2968 }
2969
2970 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2971 if (itr == mMultiSampleSupport.end())
2972 {
2973 return -1;
2974 }
2975
2976 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2977 {
2978 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2979 {
2980 return i;
2981 }
2982 }
2983
2984 return -1;
2985}
2986
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002987bool Context::supportsEventQueries() const
2988{
2989 return mSupportsEventQueries;
2990}
2991
daniel@transgaming.com01868132010-08-24 19:21:17 +00002992bool Context::supportsCompressedTextures() const
2993{
2994 return mSupportsCompressedTextures;
2995}
2996
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002997bool Context::supportsFloatTextures() const
2998{
2999 return mSupportsFloatTextures;
3000}
3001
3002bool Context::supportsFloatLinearFilter() const
3003{
3004 return mSupportsFloatLinearFilter;
3005}
3006
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003007bool Context::supportsFloatRenderableTextures() const
3008{
3009 return mSupportsFloatRenderableTextures;
3010}
3011
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003012bool Context::supportsHalfFloatTextures() const
3013{
3014 return mSupportsHalfFloatTextures;
3015}
3016
3017bool Context::supportsHalfFloatLinearFilter() const
3018{
3019 return mSupportsHalfFloatLinearFilter;
3020}
3021
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003022bool Context::supportsHalfFloatRenderableTextures() const
3023{
3024 return mSupportsHalfFloatRenderableTextures;
3025}
3026
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003027int Context::getMaximumRenderbufferDimension() const
3028{
3029 return mMaxRenderbufferDimension;
3030}
3031
3032int Context::getMaximumTextureDimension() const
3033{
3034 return mMaxTextureDimension;
3035}
3036
3037int Context::getMaximumCubeTextureDimension() const
3038{
3039 return mMaxCubeTextureDimension;
3040}
3041
3042int Context::getMaximumTextureLevel() const
3043{
3044 return mMaxTextureLevel;
3045}
3046
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003047bool Context::supportsLuminanceTextures() const
3048{
3049 return mSupportsLuminanceTextures;
3050}
3051
3052bool Context::supportsLuminanceAlphaTextures() const
3053{
3054 return mSupportsLuminanceAlphaTextures;
3055}
3056
daniel@transgaming.com83921382011-01-08 05:46:00 +00003057bool Context::supports32bitIndices() const
3058{
3059 return mSupports32bitIndices;
3060}
3061
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003062void Context::detachBuffer(GLuint buffer)
3063{
3064 // [OpenGL ES 2.0.24] section 2.9 page 22:
3065 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3066 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3067
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003068 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003069 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003070 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003071 }
3072
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003073 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003074 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003075 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003076 }
3077
3078 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3079 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003080 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003081 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003082 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003083 }
3084 }
3085}
3086
3087void Context::detachTexture(GLuint texture)
3088{
3089 // [OpenGL ES 2.0.24] section 3.8 page 84:
3090 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3091 // rebound to texture object zero
3092
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003093 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003094 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003095 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003096 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003097 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003098 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003099 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003101 }
3102 }
3103
3104 // [OpenGL ES 2.0.24] section 4.4 page 112:
3105 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3106 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3107 // image was attached in the currently bound framebuffer.
3108
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003109 Framebuffer *readFramebuffer = getReadFramebuffer();
3110 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003111
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003112 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003113 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003114 readFramebuffer->detachTexture(texture);
3115 }
3116
3117 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3118 {
3119 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003120 }
3121}
3122
3123void Context::detachFramebuffer(GLuint framebuffer)
3124{
3125 // [OpenGL ES 2.0.24] section 4.4 page 107:
3126 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3127 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3128
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003129 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003130 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003131 bindReadFramebuffer(0);
3132 }
3133
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003134 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003135 {
3136 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003137 }
3138}
3139
3140void Context::detachRenderbuffer(GLuint renderbuffer)
3141{
3142 // [OpenGL ES 2.0.24] section 4.4 page 109:
3143 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3144 // had been executed with the target RENDERBUFFER and name of zero.
3145
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003146 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003147 {
3148 bindRenderbuffer(0);
3149 }
3150
3151 // [OpenGL ES 2.0.24] section 4.4 page 111:
3152 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3153 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3154 // point to which this image was attached in the currently bound framebuffer.
3155
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003156 Framebuffer *readFramebuffer = getReadFramebuffer();
3157 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003158
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003159 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003160 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003161 readFramebuffer->detachRenderbuffer(renderbuffer);
3162 }
3163
3164 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3165 {
3166 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003167 }
3168}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003169
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003170Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003171{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003172 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003173
3174 if (t == NULL)
3175 {
3176 static const GLubyte color[] = { 0, 0, 0, 255 };
3177
3178 switch (type)
3179 {
3180 default:
3181 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003182 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003183
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003184 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003185 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003186 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003187 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003188 t = incomplete2d;
3189 }
3190 break;
3191
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003192 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003193 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003194 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003195
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003196 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3197 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3198 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3199 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3200 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3201 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003202
3203 t = incompleteCube;
3204 }
3205 break;
3206 }
3207
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003208 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003209 }
3210
3211 return t;
3212}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003213
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003214bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003215{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003216 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003217}
3218
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003219bool Context::isTriangleMode(GLenum drawMode)
3220{
3221 switch (drawMode)
3222 {
3223 case GL_TRIANGLES:
3224 case GL_TRIANGLE_FAN:
3225 case GL_TRIANGLE_STRIP:
3226 return true;
3227 case GL_POINTS:
3228 case GL_LINES:
3229 case GL_LINE_LOOP:
3230 case GL_LINE_STRIP:
3231 return false;
3232 default: UNREACHABLE();
3233 }
3234
3235 return false;
3236}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003237
3238void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3239{
3240 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3241
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003242 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3243 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3244 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3245 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003246
daniel@transgaming.com83921382011-01-08 05:46:00 +00003247 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003248}
3249
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003250void Context::initExtensionString()
3251{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003252 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003253 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3254 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003255 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003256 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003257 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003258
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003259 if (supportsEventQueries())
3260 {
3261 mExtensionString += "GL_NV_fence ";
3262 }
3263
daniel@transgaming.com01868132010-08-24 19:21:17 +00003264 if (supportsCompressedTextures())
3265 {
3266 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3267 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003268
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003269 if (supportsFloatTextures())
3270 {
3271 mExtensionString += "GL_OES_texture_float ";
3272 }
3273
3274 if (supportsHalfFloatTextures())
3275 {
3276 mExtensionString += "GL_OES_texture_half_float ";
3277 }
3278
3279 if (supportsFloatLinearFilter())
3280 {
3281 mExtensionString += "GL_OES_texture_float_linear ";
3282 }
3283
3284 if (supportsHalfFloatLinearFilter())
3285 {
3286 mExtensionString += "GL_OES_texture_half_float_linear ";
3287 }
3288
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003289 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003290 {
3291 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3292 }
3293
daniel@transgaming.com83921382011-01-08 05:46:00 +00003294 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003295 {
3296 mExtensionString += "GL_OES_element_index_uint ";
3297 }
3298
3299 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3300 if (end != std::string::npos)
3301 {
3302 mExtensionString.resize(end+1);
3303 }
3304}
3305
3306const char *Context::getExtensionString() const
3307{
3308 return mExtensionString.c_str();
3309}
3310
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003311void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3312 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3313 GLbitfield mask)
3314{
3315 IDirect3DDevice9 *device = getDevice();
3316
3317 Framebuffer *readFramebuffer = getReadFramebuffer();
3318 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3319
3320 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3321 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3322 {
3323 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3324 }
3325
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003326 if (drawFramebuffer->getSamples() != 0)
3327 {
3328 return error(GL_INVALID_OPERATION);
3329 }
3330
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003331 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3332 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3333 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3334 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3335
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003336 RECT sourceRect;
3337 RECT destRect;
3338
3339 if (srcX0 < srcX1)
3340 {
3341 sourceRect.left = srcX0;
3342 sourceRect.right = srcX1;
3343 destRect.left = dstX0;
3344 destRect.right = dstX1;
3345 }
3346 else
3347 {
3348 sourceRect.left = srcX1;
3349 destRect.left = dstX1;
3350 sourceRect.right = srcX0;
3351 destRect.right = dstX0;
3352 }
3353
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003354 if (srcY0 < srcY1)
3355 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003356 sourceRect.top = readBufferHeight - srcY1;
3357 destRect.top = drawBufferHeight - dstY1;
3358 sourceRect.bottom = readBufferHeight - srcY0;
3359 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003360 }
3361 else
3362 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003363 sourceRect.top = readBufferHeight - srcY0;
3364 destRect.top = drawBufferHeight - dstY0;
3365 sourceRect.bottom = readBufferHeight - srcY1;
3366 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003367 }
3368
3369 RECT sourceScissoredRect = sourceRect;
3370 RECT destScissoredRect = destRect;
3371
3372 if (mState.scissorTest)
3373 {
3374 // Only write to parts of the destination framebuffer which pass the scissor test
3375 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3376 // rect will be checked against scissorY, rather than the bottom.
3377 if (destRect.left < mState.scissorX)
3378 {
3379 int xDiff = mState.scissorX - destRect.left;
3380 destScissoredRect.left = mState.scissorX;
3381 sourceScissoredRect.left += xDiff;
3382 }
3383
3384 if (destRect.right > mState.scissorX + mState.scissorWidth)
3385 {
3386 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3387 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3388 sourceScissoredRect.right -= xDiff;
3389 }
3390
3391 if (destRect.top < mState.scissorY)
3392 {
3393 int yDiff = mState.scissorY - destRect.top;
3394 destScissoredRect.top = mState.scissorY;
3395 sourceScissoredRect.top += yDiff;
3396 }
3397
3398 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3399 {
3400 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3401 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3402 sourceScissoredRect.bottom -= yDiff;
3403 }
3404 }
3405
3406 bool blitRenderTarget = false;
3407 bool blitDepthStencil = false;
3408
3409 RECT sourceTrimmedRect = sourceScissoredRect;
3410 RECT destTrimmedRect = destScissoredRect;
3411
3412 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3413 // the actual draw and read surfaces.
3414 if (sourceTrimmedRect.left < 0)
3415 {
3416 int xDiff = 0 - sourceTrimmedRect.left;
3417 sourceTrimmedRect.left = 0;
3418 destTrimmedRect.left += xDiff;
3419 }
3420
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003421 if (sourceTrimmedRect.right > readBufferWidth)
3422 {
3423 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3424 sourceTrimmedRect.right = readBufferWidth;
3425 destTrimmedRect.right -= xDiff;
3426 }
3427
3428 if (sourceTrimmedRect.top < 0)
3429 {
3430 int yDiff = 0 - sourceTrimmedRect.top;
3431 sourceTrimmedRect.top = 0;
3432 destTrimmedRect.top += yDiff;
3433 }
3434
3435 if (sourceTrimmedRect.bottom > readBufferHeight)
3436 {
3437 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3438 sourceTrimmedRect.bottom = readBufferHeight;
3439 destTrimmedRect.bottom -= yDiff;
3440 }
3441
3442 if (destTrimmedRect.left < 0)
3443 {
3444 int xDiff = 0 - destTrimmedRect.left;
3445 destTrimmedRect.left = 0;
3446 sourceTrimmedRect.left += xDiff;
3447 }
3448
3449 if (destTrimmedRect.right > drawBufferWidth)
3450 {
3451 int xDiff = destTrimmedRect.right - drawBufferWidth;
3452 destTrimmedRect.right = drawBufferWidth;
3453 sourceTrimmedRect.right -= xDiff;
3454 }
3455
3456 if (destTrimmedRect.top < 0)
3457 {
3458 int yDiff = 0 - destTrimmedRect.top;
3459 destTrimmedRect.top = 0;
3460 sourceTrimmedRect.top += yDiff;
3461 }
3462
3463 if (destTrimmedRect.bottom > drawBufferHeight)
3464 {
3465 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3466 destTrimmedRect.bottom = drawBufferHeight;
3467 sourceTrimmedRect.bottom -= yDiff;
3468 }
3469
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003470 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003471 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3472 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3473 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3474 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003475 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3476 {
3477 partialBufferCopy = true;
3478 }
3479
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003480 if (mask & GL_COLOR_BUFFER_BIT)
3481 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003482 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3483 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3484 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3485 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3486 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003487 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3488 {
3489 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3490 return error(GL_INVALID_OPERATION);
3491 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003492
3493 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3494 {
3495 return error(GL_INVALID_OPERATION);
3496 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003497
3498 blitRenderTarget = true;
3499
3500 }
3501
3502 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3503 {
3504 DepthStencilbuffer *readDSBuffer = NULL;
3505 DepthStencilbuffer *drawDSBuffer = NULL;
3506
3507 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3508 // both a depth and stencil buffer, it will be the same buffer.
3509
3510 if (mask & GL_DEPTH_BUFFER_BIT)
3511 {
3512 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3513 {
3514 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3515 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3516 {
3517 return error(GL_INVALID_OPERATION);
3518 }
3519
3520 blitDepthStencil = true;
3521 readDSBuffer = readFramebuffer->getDepthbuffer();
3522 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3523 }
3524 }
3525
3526 if (mask & GL_STENCIL_BUFFER_BIT)
3527 {
3528 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3529 {
3530 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3531 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3532 {
3533 return error(GL_INVALID_OPERATION);
3534 }
3535
3536 blitDepthStencil = true;
3537 readDSBuffer = readFramebuffer->getStencilbuffer();
3538 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3539 }
3540 }
3541
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003542 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003543 {
3544 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3545 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3546 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003547
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003548 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3549 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003550 {
3551 return error(GL_INVALID_OPERATION);
3552 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003553 }
3554
3555 if (blitRenderTarget || blitDepthStencil)
3556 {
3557 egl::Display *display = getDisplay();
3558 display->endScene();
3559
3560 if (blitRenderTarget)
3561 {
3562 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3563 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3564
3565 if (FAILED(result))
3566 {
3567 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3568 return;
3569 }
3570 }
3571
3572 if (blitDepthStencil)
3573 {
3574 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3575
3576 if (FAILED(result))
3577 {
3578 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3579 return;
3580 }
3581 }
3582 }
3583}
3584
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003585VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3586{
3587 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3588 {
3589 mVertexDeclCache[i].vertexDeclaration = NULL;
3590 mVertexDeclCache[i].lruCount = 0;
3591 }
3592}
3593
3594VertexDeclarationCache::~VertexDeclarationCache()
3595{
3596 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3597 {
3598 if (mVertexDeclCache[i].vertexDeclaration)
3599 {
3600 mVertexDeclCache[i].vertexDeclaration->Release();
3601 }
3602 }
3603}
3604
3605GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3606{
3607 IDirect3DDevice9 *device = getDevice();
3608
3609 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3610 D3DVERTEXELEMENT9 *element = &elements[0];
3611
3612 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3613 {
3614 if (attributes[i].active)
3615 {
3616 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3617
3618 element->Stream = i;
3619 element->Offset = 0;
3620 element->Type = attributes[i].type;
3621 element->Method = D3DDECLMETHOD_DEFAULT;
3622 element->Usage = D3DDECLUSAGE_TEXCOORD;
3623 element->UsageIndex = program->getSemanticIndex(i);
3624 element++;
3625 }
3626 }
3627
3628 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3629 *(element++) = end;
3630
3631 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3632 {
3633 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3634 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3635 {
3636 entry->lruCount = ++mMaxLru;
3637 device->SetVertexDeclaration(entry->vertexDeclaration);
3638
3639 return GL_NO_ERROR;
3640 }
3641 }
3642
3643 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3644
3645 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3646 {
3647 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3648 {
3649 lastCache = &mVertexDeclCache[i];
3650 }
3651 }
3652
3653 if (lastCache->vertexDeclaration != NULL)
3654 {
3655 lastCache->vertexDeclaration->Release();
3656 lastCache->vertexDeclaration = NULL;
3657 }
3658
3659 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3660 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3661 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3662 lastCache->lruCount = ++mMaxLru;
3663
3664 return GL_NO_ERROR;
3665}
3666
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003667}
3668
3669extern "C"
3670{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003671gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003672{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003673 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003674}
3675
3676void glDestroyContext(gl::Context *context)
3677{
3678 delete context;
3679
3680 if (context == gl::getContext())
3681 {
3682 gl::makeCurrent(NULL, NULL, NULL);
3683 }
3684}
3685
3686void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3687{
3688 gl::makeCurrent(context, display, surface);
3689}
3690
3691gl::Context *glGetCurrentContext()
3692{
3693 return gl::getContext();
3694}
3695}