blob: 1988e221890bb9b8891f90598b50e5b6ac7d30d1 [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.com4f9ef0d2011-05-30 23:51:19 +0000246 mSupportsNonPower2Texture = display->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000247
248 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
249 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
250 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
251 mMaxRenderbufferDimension = mMaxTextureDimension;
252 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
253 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
254 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
255
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000256 const D3DFORMAT renderBufferFormats[] =
257 {
258 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000259 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000260 D3DFMT_R5G6B5,
261 D3DFMT_D24S8
262 };
263
264 int max = 0;
265 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
266 {
267 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
268 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
269 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
270
271 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
272 {
273 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
274 {
275 max = j;
276 }
277 }
278 }
279
280 mMaxSupportedSamples = max;
281
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000282 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000283 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000284 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
285 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000286 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
287 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000288
daniel@transgaming.com83921382011-01-08 05:46:00 +0000289 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
290
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000291 initExtensionString();
292
293 mState.viewportX = 0;
294 mState.viewportY = 0;
295 mState.viewportWidth = surface->getWidth();
296 mState.viewportHeight = surface->getHeight();
297
298 mState.scissorX = 0;
299 mState.scissorY = 0;
300 mState.scissorWidth = surface->getWidth();
301 mState.scissorHeight = surface->getHeight();
302
303 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000304 }
305
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
307 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000308 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000311 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000312 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
314 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000316 if (defaultRenderTarget)
317 {
318 defaultRenderTarget->Release();
319 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000321 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000323 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000325
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000326 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327}
328
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000329// 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 +0000330void Context::markAllStateDirty()
331{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000332 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
333 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000334 mAppliedTextureSerialPS[t] = 0;
335 }
336
337 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
338 {
339 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000340 }
341
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000342 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000343 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000344 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000345 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000346 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000347
348 mClearStateDirty = true;
349 mCullStateDirty = true;
350 mDepthStateDirty = true;
351 mMaskStateDirty = true;
352 mBlendStateDirty = true;
353 mStencilStateDirty = true;
354 mPolygonOffsetStateDirty = true;
355 mScissorStateDirty = true;
356 mSampleStateDirty = true;
357 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000358 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000359}
360
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361void Context::setClearColor(float red, float green, float blue, float alpha)
362{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000363 mState.colorClearValue.red = red;
364 mState.colorClearValue.green = green;
365 mState.colorClearValue.blue = blue;
366 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367}
368
369void Context::setClearDepth(float depth)
370{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000371 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372}
373
374void Context::setClearStencil(int stencil)
375{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000376 mState.stencilClearValue = stencil;
377}
378
379void Context::setCullFace(bool enabled)
380{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000381 if (mState.cullFace != enabled)
382 {
383 mState.cullFace = enabled;
384 mCullStateDirty = true;
385 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000386}
387
388bool Context::isCullFaceEnabled() const
389{
390 return mState.cullFace;
391}
392
393void Context::setCullMode(GLenum mode)
394{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000395 if (mState.cullMode != mode)
396 {
397 mState.cullMode = mode;
398 mCullStateDirty = true;
399 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000400}
401
402void Context::setFrontFace(GLenum front)
403{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000404 if (mState.frontFace != front)
405 {
406 mState.frontFace = front;
407 mFrontFaceDirty = true;
408 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000409}
410
411void Context::setDepthTest(bool enabled)
412{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000413 if (mState.depthTest != enabled)
414 {
415 mState.depthTest = enabled;
416 mDepthStateDirty = true;
417 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000418}
419
420bool Context::isDepthTestEnabled() const
421{
422 return mState.depthTest;
423}
424
425void Context::setDepthFunc(GLenum depthFunc)
426{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000427 if (mState.depthFunc != depthFunc)
428 {
429 mState.depthFunc = depthFunc;
430 mDepthStateDirty = true;
431 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000432}
433
434void Context::setDepthRange(float zNear, float zFar)
435{
436 mState.zNear = zNear;
437 mState.zFar = zFar;
438}
439
440void Context::setBlend(bool enabled)
441{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000442 if (mState.blend != enabled)
443 {
444 mState.blend = enabled;
445 mBlendStateDirty = true;
446 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000447}
448
449bool Context::isBlendEnabled() const
450{
451 return mState.blend;
452}
453
454void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
455{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000456 if (mState.sourceBlendRGB != sourceRGB ||
457 mState.sourceBlendAlpha != sourceAlpha ||
458 mState.destBlendRGB != destRGB ||
459 mState.destBlendAlpha != destAlpha)
460 {
461 mState.sourceBlendRGB = sourceRGB;
462 mState.destBlendRGB = destRGB;
463 mState.sourceBlendAlpha = sourceAlpha;
464 mState.destBlendAlpha = destAlpha;
465 mBlendStateDirty = true;
466 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000467}
468
469void Context::setBlendColor(float red, float green, float blue, float alpha)
470{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000471 if (mState.blendColor.red != red ||
472 mState.blendColor.green != green ||
473 mState.blendColor.blue != blue ||
474 mState.blendColor.alpha != alpha)
475 {
476 mState.blendColor.red = red;
477 mState.blendColor.green = green;
478 mState.blendColor.blue = blue;
479 mState.blendColor.alpha = alpha;
480 mBlendStateDirty = true;
481 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000482}
483
484void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
485{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000486 if (mState.blendEquationRGB != rgbEquation ||
487 mState.blendEquationAlpha != alphaEquation)
488 {
489 mState.blendEquationRGB = rgbEquation;
490 mState.blendEquationAlpha = alphaEquation;
491 mBlendStateDirty = true;
492 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000493}
494
495void Context::setStencilTest(bool enabled)
496{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 if (mState.stencilTest != enabled)
498 {
499 mState.stencilTest = enabled;
500 mStencilStateDirty = true;
501 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000502}
503
504bool Context::isStencilTestEnabled() const
505{
506 return mState.stencilTest;
507}
508
509void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
510{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000511 if (mState.stencilFunc != stencilFunc ||
512 mState.stencilRef != stencilRef ||
513 mState.stencilMask != stencilMask)
514 {
515 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000516 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 mState.stencilMask = stencilMask;
518 mStencilStateDirty = true;
519 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000520}
521
522void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
523{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000524 if (mState.stencilBackFunc != stencilBackFunc ||
525 mState.stencilBackRef != stencilBackRef ||
526 mState.stencilBackMask != stencilBackMask)
527 {
528 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000529 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000530 mState.stencilBackMask = stencilBackMask;
531 mStencilStateDirty = true;
532 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000533}
534
535void Context::setStencilWritemask(GLuint stencilWritemask)
536{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000537 if (mState.stencilWritemask != stencilWritemask)
538 {
539 mState.stencilWritemask = stencilWritemask;
540 mStencilStateDirty = true;
541 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000542}
543
544void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
545{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000546 if (mState.stencilBackWritemask != stencilBackWritemask)
547 {
548 mState.stencilBackWritemask = stencilBackWritemask;
549 mStencilStateDirty = true;
550 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000551}
552
553void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
554{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000555 if (mState.stencilFail != stencilFail ||
556 mState.stencilPassDepthFail != stencilPassDepthFail ||
557 mState.stencilPassDepthPass != stencilPassDepthPass)
558 {
559 mState.stencilFail = stencilFail;
560 mState.stencilPassDepthFail = stencilPassDepthFail;
561 mState.stencilPassDepthPass = stencilPassDepthPass;
562 mStencilStateDirty = true;
563 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000564}
565
566void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
567{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000568 if (mState.stencilBackFail != stencilBackFail ||
569 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
570 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
571 {
572 mState.stencilBackFail = stencilBackFail;
573 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
574 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
575 mStencilStateDirty = true;
576 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000577}
578
579void Context::setPolygonOffsetFill(bool enabled)
580{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000581 if (mState.polygonOffsetFill != enabled)
582 {
583 mState.polygonOffsetFill = enabled;
584 mPolygonOffsetStateDirty = true;
585 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000586}
587
588bool Context::isPolygonOffsetFillEnabled() const
589{
590 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000591
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000592}
593
594void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
595{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000596 if (mState.polygonOffsetFactor != factor ||
597 mState.polygonOffsetUnits != units)
598 {
599 mState.polygonOffsetFactor = factor;
600 mState.polygonOffsetUnits = units;
601 mPolygonOffsetStateDirty = true;
602 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000603}
604
605void Context::setSampleAlphaToCoverage(bool enabled)
606{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000607 if (mState.sampleAlphaToCoverage != enabled)
608 {
609 mState.sampleAlphaToCoverage = enabled;
610 mSampleStateDirty = true;
611 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000612}
613
614bool Context::isSampleAlphaToCoverageEnabled() const
615{
616 return mState.sampleAlphaToCoverage;
617}
618
619void Context::setSampleCoverage(bool enabled)
620{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000621 if (mState.sampleCoverage != enabled)
622 {
623 mState.sampleCoverage = enabled;
624 mSampleStateDirty = true;
625 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000626}
627
628bool Context::isSampleCoverageEnabled() const
629{
630 return mState.sampleCoverage;
631}
632
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000633void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000634{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000635 if (mState.sampleCoverageValue != value ||
636 mState.sampleCoverageInvert != invert)
637 {
638 mState.sampleCoverageValue = value;
639 mState.sampleCoverageInvert = invert;
640 mSampleStateDirty = true;
641 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000642}
643
644void Context::setScissorTest(bool enabled)
645{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000646 if (mState.scissorTest != enabled)
647 {
648 mState.scissorTest = enabled;
649 mScissorStateDirty = true;
650 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000651}
652
653bool Context::isScissorTestEnabled() const
654{
655 return mState.scissorTest;
656}
657
658void Context::setDither(bool enabled)
659{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000660 if (mState.dither != enabled)
661 {
662 mState.dither = enabled;
663 mDitherStateDirty = true;
664 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000665}
666
667bool Context::isDitherEnabled() const
668{
669 return mState.dither;
670}
671
672void Context::setLineWidth(GLfloat width)
673{
674 mState.lineWidth = width;
675}
676
677void Context::setGenerateMipmapHint(GLenum hint)
678{
679 mState.generateMipmapHint = hint;
680}
681
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000682void Context::setFragmentShaderDerivativeHint(GLenum hint)
683{
684 mState.fragmentShaderDerivativeHint = hint;
685 // TODO: Propagate the hint to shader translator so we can write
686 // ddx, ddx_coarse, or ddx_fine depending on the hint.
687 // Ignore for now. It is valid for implementations to ignore hint.
688}
689
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000690void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
691{
692 mState.viewportX = x;
693 mState.viewportY = y;
694 mState.viewportWidth = width;
695 mState.viewportHeight = height;
696}
697
698void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
699{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000700 if (mState.scissorX != x || mState.scissorY != y ||
701 mState.scissorWidth != width || mState.scissorHeight != height)
702 {
703 mState.scissorX = x;
704 mState.scissorY = y;
705 mState.scissorWidth = width;
706 mState.scissorHeight = height;
707 mScissorStateDirty = true;
708 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000709}
710
711void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
712{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000713 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
714 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
715 {
716 mState.colorMaskRed = red;
717 mState.colorMaskGreen = green;
718 mState.colorMaskBlue = blue;
719 mState.colorMaskAlpha = alpha;
720 mMaskStateDirty = true;
721 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000722}
723
724void Context::setDepthMask(bool mask)
725{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000726 if (mState.depthMask != mask)
727 {
728 mState.depthMask = mask;
729 mMaskStateDirty = true;
730 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000731}
732
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000733void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734{
735 mState.activeSampler = active;
736}
737
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000738GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000740 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000741}
742
743GLuint Context::getDrawFramebufferHandle() const
744{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000745 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000746}
747
748GLuint Context::getRenderbufferHandle() const
749{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000750 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000751}
752
753GLuint Context::getArrayBufferHandle() const
754{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000755 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000756}
757
daniel@transgaming.com83921382011-01-08 05:46:00 +0000758void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000759{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000760 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000761}
762
daniel@transgaming.com83921382011-01-08 05:46:00 +0000763const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000764{
765 return mState.vertexAttribute[attribNum];
766}
767
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000768void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000769 GLsizei stride, const void *pointer)
770{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000771 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000772 mState.vertexAttribute[attribNum].mSize = size;
773 mState.vertexAttribute[attribNum].mType = type;
774 mState.vertexAttribute[attribNum].mNormalized = normalized;
775 mState.vertexAttribute[attribNum].mStride = stride;
776 mState.vertexAttribute[attribNum].mPointer = pointer;
777}
778
779const void *Context::getVertexAttribPointer(unsigned int attribNum) const
780{
781 return mState.vertexAttribute[attribNum].mPointer;
782}
783
daniel@transgaming.com83921382011-01-08 05:46:00 +0000784const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000785{
786 return mState.vertexAttribute;
787}
788
789void Context::setPackAlignment(GLint alignment)
790{
791 mState.packAlignment = alignment;
792}
793
794GLint Context::getPackAlignment() const
795{
796 return mState.packAlignment;
797}
798
799void Context::setUnpackAlignment(GLint alignment)
800{
801 mState.unpackAlignment = alignment;
802}
803
804GLint Context::getUnpackAlignment() const
805{
806 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807}
808
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809GLuint Context::createBuffer()
810{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000811 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814GLuint Context::createProgram()
815{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000816 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817}
818
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000819GLuint Context::createShader(GLenum type)
820{
821 return mResourceManager->createShader(type);
822}
823
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824GLuint Context::createTexture()
825{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000826 return mResourceManager->createTexture();
827}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000829GLuint Context::createRenderbuffer()
830{
831 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000834// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835GLuint Context::createFramebuffer()
836{
benvanik@google.com1a233342011-04-28 19:44:39 +0000837 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000838
839 mFramebufferMap[handle] = NULL;
840
841 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842}
843
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000844GLuint Context::createFence()
845{
benvanik@google.com1a233342011-04-28 19:44:39 +0000846 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000847
848 mFenceMap[handle] = new Fence;
849
850 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000851}
852
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853void Context::deleteBuffer(GLuint buffer)
854{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000855 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856 {
857 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000859
860 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
863void Context::deleteShader(GLuint shader)
864{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000865 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866}
867
868void Context::deleteProgram(GLuint program)
869{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000870 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871}
872
873void Context::deleteTexture(GLuint texture)
874{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000875 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876 {
877 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000879
880 mResourceManager->deleteTexture(texture);
881}
882
883void Context::deleteRenderbuffer(GLuint renderbuffer)
884{
885 if (mResourceManager->getRenderbuffer(renderbuffer))
886 {
887 detachRenderbuffer(renderbuffer);
888 }
889
890 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
893void Context::deleteFramebuffer(GLuint framebuffer)
894{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000895 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
896
897 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898 {
899 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000900
benvanik@google.com1a233342011-04-28 19:44:39 +0000901 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000902 delete framebufferObject->second;
903 mFramebufferMap.erase(framebufferObject);
904 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000906
907void Context::deleteFence(GLuint fence)
908{
909 FenceMap::iterator fenceObject = mFenceMap.find(fence);
910
911 if (fenceObject != mFenceMap.end())
912 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000913 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000914 delete fenceObject->second;
915 mFenceMap.erase(fenceObject);
916 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000917}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000919Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000921 return mResourceManager->getBuffer(handle);
922}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000924Shader *Context::getShader(GLuint handle)
925{
926 return mResourceManager->getShader(handle);
927}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000929Program *Context::getProgram(GLuint handle)
930{
931 return mResourceManager->getProgram(handle);
932}
933
934Texture *Context::getTexture(GLuint handle)
935{
936 return mResourceManager->getTexture(handle);
937}
938
939Renderbuffer *Context::getRenderbuffer(GLuint handle)
940{
941 return mResourceManager->getRenderbuffer(handle);
942}
943
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000944Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000945{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000946 return getFramebuffer(mState.readFramebuffer);
947}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000948
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000949Framebuffer *Context::getDrawFramebuffer()
950{
951 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
954void Context::bindArrayBuffer(unsigned int buffer)
955{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000956 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000958 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959}
960
961void Context::bindElementArrayBuffer(unsigned int buffer)
962{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000963 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000965 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
968void Context::bindTexture2D(GLuint texture)
969{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000970 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000972 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
975void Context::bindTextureCubeMap(GLuint texture)
976{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000977 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000979 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980}
981
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000982void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000984 if (!getFramebuffer(framebuffer))
985 {
986 mFramebufferMap[framebuffer] = new Framebuffer();
987 }
988
989 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000990}
991
992void Context::bindDrawFramebuffer(GLuint framebuffer)
993{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000994 if (!getFramebuffer(framebuffer))
995 {
996 mFramebufferMap[framebuffer] = new Framebuffer();
997 }
998
999 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000}
1001
1002void Context::bindRenderbuffer(GLuint renderbuffer)
1003{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001004 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1005
1006 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007}
1008
1009void Context::useProgram(GLuint program)
1010{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001011 GLuint priorProgram = mState.currentProgram;
1012 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001013
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001014 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001016 Program *newProgram = mResourceManager->getProgram(program);
1017 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1018
1019 if (newProgram)
1020 {
1021 newProgram->addRef();
1022 }
1023
1024 if (oldProgram)
1025 {
1026 oldProgram->release();
1027 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
1031void Context::setFramebufferZero(Framebuffer *buffer)
1032{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001033 delete mFramebufferMap[0];
1034 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035}
1036
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001037void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001039 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1040 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041}
1042
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001043Framebuffer *Context::getFramebuffer(unsigned int handle)
1044{
1045 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1046
1047 if (framebuffer == mFramebufferMap.end())
1048 {
1049 return NULL;
1050 }
1051 else
1052 {
1053 return framebuffer->second;
1054 }
1055}
1056
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001057Fence *Context::getFence(unsigned int handle)
1058{
1059 FenceMap::iterator fence = mFenceMap.find(handle);
1060
1061 if (fence == mFenceMap.end())
1062 {
1063 return NULL;
1064 }
1065 else
1066 {
1067 return fence->second;
1068 }
1069}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001070
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071Buffer *Context::getArrayBuffer()
1072{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001073 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074}
1075
1076Buffer *Context::getElementArrayBuffer()
1077{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001078 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079}
1080
1081Program *Context::getCurrentProgram()
1082{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001083 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084}
1085
1086Texture2D *Context::getTexture2D()
1087{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001088 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089}
1090
1091TextureCubeMap *Context::getTextureCubeMap()
1092{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001093 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001096Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001098 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001099
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001100 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001101 {
1102 switch (type)
1103 {
1104 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001105 case TEXTURE_2D: return mTexture2DZero.get();
1106 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001107 }
1108 }
1109
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001110 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111}
1112
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001113bool Context::getBooleanv(GLenum pname, GLboolean *params)
1114{
1115 switch (pname)
1116 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001117 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1118 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1119 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001120 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001121 params[0] = mState.colorMaskRed;
1122 params[1] = mState.colorMaskGreen;
1123 params[2] = mState.colorMaskBlue;
1124 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001125 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001126 case GL_CULL_FACE: *params = mState.cullFace; break;
1127 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1128 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1129 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1130 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1131 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1132 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1133 case GL_BLEND: *params = mState.blend; break;
1134 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001135 default:
1136 return false;
1137 }
1138
1139 return true;
1140}
1141
1142bool Context::getFloatv(GLenum pname, GLfloat *params)
1143{
1144 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1145 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1146 // GetIntegerv as its native query function. As it would require conversion in any
1147 // case, this should make no difference to the calling application.
1148 switch (pname)
1149 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001150 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1151 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1152 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1153 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1154 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001155 case GL_ALIASED_LINE_WIDTH_RANGE:
1156 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1157 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1158 break;
1159 case GL_ALIASED_POINT_SIZE_RANGE:
1160 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001161 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 +00001162 break;
1163 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001164 params[0] = mState.zNear;
1165 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001166 break;
1167 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001168 params[0] = mState.colorClearValue.red;
1169 params[1] = mState.colorClearValue.green;
1170 params[2] = mState.colorClearValue.blue;
1171 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001172 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001173 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001174 params[0] = mState.blendColor.red;
1175 params[1] = mState.blendColor.green;
1176 params[2] = mState.blendColor.blue;
1177 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001178 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001179 default:
1180 return false;
1181 }
1182
1183 return true;
1184}
1185
1186bool Context::getIntegerv(GLenum pname, GLint *params)
1187{
1188 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1189 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1190 // GetIntegerv as its native query function. As it would require conversion in any
1191 // case, this should make no difference to the calling application. You may find it in
1192 // Context::getFloatv.
1193 switch (pname)
1194 {
1195 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1196 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001197 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001198 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1199 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001200 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001201 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001202 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001203 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001204 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001205 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1206 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001207 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1208 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1209 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001210 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001211 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1212 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1213 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1214 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001215 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001216 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1217 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1218 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1219 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1220 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1221 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1222 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1223 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1224 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1225 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1226 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1227 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1228 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1229 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1230 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1231 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1232 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1233 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1234 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1235 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1236 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1237 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1238 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1239 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001240 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1241 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001242 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1243 {
1244 if (supportsCompressedTextures())
1245 {
1246 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1247 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1248 *params = 2;
1249 }
1250 else
1251 {
1252 *params = 0;
1253 }
1254 }
1255 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001256 case GL_MAX_SAMPLES_ANGLE:
1257 {
1258 GLsizei maxSamples = getMaxSupportedSamples();
1259 if (maxSamples != 0)
1260 {
1261 *params = maxSamples;
1262 }
1263 else
1264 {
1265 return false;
1266 }
1267
1268 break;
1269 }
1270 case GL_SAMPLE_BUFFERS:
1271 case GL_SAMPLES:
1272 {
1273 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1274 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1275 {
1276 switch (pname)
1277 {
1278 case GL_SAMPLE_BUFFERS:
1279 if (framebuffer->getSamples() != 0)
1280 {
1281 *params = 1;
1282 }
1283 else
1284 {
1285 *params = 0;
1286 }
1287 break;
1288 case GL_SAMPLES:
1289 *params = framebuffer->getSamples();
1290 break;
1291 }
1292 }
1293 else
1294 {
1295 *params = 0;
1296 }
1297 }
1298 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001299 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1300 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1301 case GL_MAX_VIEWPORT_DIMS:
1302 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001303 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001304 params[0] = maxDimension;
1305 params[1] = maxDimension;
1306 }
1307 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001308 case GL_COMPRESSED_TEXTURE_FORMATS:
1309 {
1310 if (supportsCompressedTextures())
1311 {
1312 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1313 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1314 }
1315 }
1316 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001317 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001318 params[0] = mState.viewportX;
1319 params[1] = mState.viewportY;
1320 params[2] = mState.viewportWidth;
1321 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001322 break;
1323 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001324 params[0] = mState.scissorX;
1325 params[1] = mState.scissorY;
1326 params[2] = mState.scissorWidth;
1327 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001328 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001329 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1330 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001331 case GL_RED_BITS:
1332 case GL_GREEN_BITS:
1333 case GL_BLUE_BITS:
1334 case GL_ALPHA_BITS:
1335 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001336 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001337 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1338
1339 if (colorbuffer)
1340 {
1341 switch (pname)
1342 {
1343 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1344 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1345 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1346 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1347 }
1348 }
1349 else
1350 {
1351 *params = 0;
1352 }
1353 }
1354 break;
1355 case GL_DEPTH_BITS:
1356 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001357 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001358 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359
1360 if (depthbuffer)
1361 {
1362 *params = depthbuffer->getDepthSize();
1363 }
1364 else
1365 {
1366 *params = 0;
1367 }
1368 }
1369 break;
1370 case GL_STENCIL_BITS:
1371 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001372 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001373 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001374
1375 if (stencilbuffer)
1376 {
1377 *params = stencilbuffer->getStencilSize();
1378 }
1379 else
1380 {
1381 *params = 0;
1382 }
1383 }
1384 break;
1385 case GL_TEXTURE_BINDING_2D:
1386 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001387 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001388 {
1389 error(GL_INVALID_OPERATION);
1390 return false;
1391 }
1392
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001393 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001394 }
1395 break;
1396 case GL_TEXTURE_BINDING_CUBE_MAP:
1397 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001398 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001399 {
1400 error(GL_INVALID_OPERATION);
1401 return false;
1402 }
1403
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001404 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001405 }
1406 break;
1407 default:
1408 return false;
1409 }
1410
1411 return true;
1412}
1413
1414bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1415{
1416 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1417 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1418 // to the fact that it is stored internally as a float, and so would require conversion
1419 // if returned from Context::getIntegerv. Since this conversion is already implemented
1420 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1421 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1422 // application.
1423 switch (pname)
1424 {
1425 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1426 case GL_SHADER_BINARY_FORMATS:
1427 {
1428 *type = GL_INT;
1429 *numParams = 0;
1430 }
1431 break;
1432 case GL_MAX_VERTEX_ATTRIBS:
1433 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1434 case GL_MAX_VARYING_VECTORS:
1435 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1436 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1437 case GL_MAX_TEXTURE_IMAGE_UNITS:
1438 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1439 case GL_MAX_RENDERBUFFER_SIZE:
1440 case GL_NUM_SHADER_BINARY_FORMATS:
1441 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1442 case GL_ARRAY_BUFFER_BINDING:
1443 case GL_FRAMEBUFFER_BINDING:
1444 case GL_RENDERBUFFER_BINDING:
1445 case GL_CURRENT_PROGRAM:
1446 case GL_PACK_ALIGNMENT:
1447 case GL_UNPACK_ALIGNMENT:
1448 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001449 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001450 case GL_RED_BITS:
1451 case GL_GREEN_BITS:
1452 case GL_BLUE_BITS:
1453 case GL_ALPHA_BITS:
1454 case GL_DEPTH_BITS:
1455 case GL_STENCIL_BITS:
1456 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1457 case GL_CULL_FACE_MODE:
1458 case GL_FRONT_FACE:
1459 case GL_ACTIVE_TEXTURE:
1460 case GL_STENCIL_FUNC:
1461 case GL_STENCIL_VALUE_MASK:
1462 case GL_STENCIL_REF:
1463 case GL_STENCIL_FAIL:
1464 case GL_STENCIL_PASS_DEPTH_FAIL:
1465 case GL_STENCIL_PASS_DEPTH_PASS:
1466 case GL_STENCIL_BACK_FUNC:
1467 case GL_STENCIL_BACK_VALUE_MASK:
1468 case GL_STENCIL_BACK_REF:
1469 case GL_STENCIL_BACK_FAIL:
1470 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1471 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1472 case GL_DEPTH_FUNC:
1473 case GL_BLEND_SRC_RGB:
1474 case GL_BLEND_SRC_ALPHA:
1475 case GL_BLEND_DST_RGB:
1476 case GL_BLEND_DST_ALPHA:
1477 case GL_BLEND_EQUATION_RGB:
1478 case GL_BLEND_EQUATION_ALPHA:
1479 case GL_STENCIL_WRITEMASK:
1480 case GL_STENCIL_BACK_WRITEMASK:
1481 case GL_STENCIL_CLEAR_VALUE:
1482 case GL_SUBPIXEL_BITS:
1483 case GL_MAX_TEXTURE_SIZE:
1484 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1485 case GL_SAMPLE_BUFFERS:
1486 case GL_SAMPLES:
1487 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1488 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1489 case GL_TEXTURE_BINDING_2D:
1490 case GL_TEXTURE_BINDING_CUBE_MAP:
1491 {
1492 *type = GL_INT;
1493 *numParams = 1;
1494 }
1495 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001496 case GL_MAX_SAMPLES_ANGLE:
1497 {
1498 if (getMaxSupportedSamples() != 0)
1499 {
1500 *type = GL_INT;
1501 *numParams = 1;
1502 }
1503 else
1504 {
1505 return false;
1506 }
1507 }
1508 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001509 case GL_MAX_VIEWPORT_DIMS:
1510 {
1511 *type = GL_INT;
1512 *numParams = 2;
1513 }
1514 break;
1515 case GL_VIEWPORT:
1516 case GL_SCISSOR_BOX:
1517 {
1518 *type = GL_INT;
1519 *numParams = 4;
1520 }
1521 break;
1522 case GL_SHADER_COMPILER:
1523 case GL_SAMPLE_COVERAGE_INVERT:
1524 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001525 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1526 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1527 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1528 case GL_SAMPLE_COVERAGE:
1529 case GL_SCISSOR_TEST:
1530 case GL_STENCIL_TEST:
1531 case GL_DEPTH_TEST:
1532 case GL_BLEND:
1533 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001534 {
1535 *type = GL_BOOL;
1536 *numParams = 1;
1537 }
1538 break;
1539 case GL_COLOR_WRITEMASK:
1540 {
1541 *type = GL_BOOL;
1542 *numParams = 4;
1543 }
1544 break;
1545 case GL_POLYGON_OFFSET_FACTOR:
1546 case GL_POLYGON_OFFSET_UNITS:
1547 case GL_SAMPLE_COVERAGE_VALUE:
1548 case GL_DEPTH_CLEAR_VALUE:
1549 case GL_LINE_WIDTH:
1550 {
1551 *type = GL_FLOAT;
1552 *numParams = 1;
1553 }
1554 break;
1555 case GL_ALIASED_LINE_WIDTH_RANGE:
1556 case GL_ALIASED_POINT_SIZE_RANGE:
1557 case GL_DEPTH_RANGE:
1558 {
1559 *type = GL_FLOAT;
1560 *numParams = 2;
1561 }
1562 break;
1563 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001564 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001565 {
1566 *type = GL_FLOAT;
1567 *numParams = 4;
1568 }
1569 break;
1570 default:
1571 return false;
1572 }
1573
1574 return true;
1575}
1576
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577// Applies the render target surface, depth stencil surface, viewport rectangle and
1578// scissor rectangle to the Direct3D 9 device
1579bool Context::applyRenderTarget(bool ignoreViewport)
1580{
1581 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001582
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001583 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001584
1585 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1586 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001587 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588 }
1589
1590 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001591
1592 if (!renderTarget)
1593 {
1594 return false; // Context must be lost
1595 }
1596
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001597 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001599 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1600 if (renderTargetSerial != mAppliedRenderTargetSerial)
1601 {
1602 device->SetRenderTarget(0, renderTarget);
1603 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001604 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 +00001605 }
1606
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001607 unsigned int depthbufferSerial = 0;
1608 unsigned int stencilbufferSerial = 0;
1609 if (framebufferObject->getDepthbufferType() != GL_NONE)
1610 {
1611 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001612 if (!depthStencil)
1613 {
1614 ERR("Depth stencil pointer unexpectedly null.");
1615 return false;
1616 }
1617
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001618 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1619 }
1620 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1621 {
1622 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001623 if (!depthStencil)
1624 {
1625 ERR("Depth stencil pointer unexpectedly null.");
1626 return false;
1627 }
1628
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001629 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1630 }
1631
1632 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001633 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001634 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001635 {
1636 device->SetDepthStencilSurface(depthStencil);
1637 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001638 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001639 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001640 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641
1642 D3DVIEWPORT9 viewport;
1643 D3DSURFACE_DESC desc;
1644 renderTarget->GetDesc(&desc);
1645
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001646 float zNear = clamp01(mState.zNear);
1647 float zFar = clamp01(mState.zFar);
1648
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649 if (ignoreViewport)
1650 {
1651 viewport.X = 0;
1652 viewport.Y = 0;
1653 viewport.Width = desc.Width;
1654 viewport.Height = desc.Height;
1655 viewport.MinZ = 0.0f;
1656 viewport.MaxZ = 1.0f;
1657 }
1658 else
1659 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001660 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1661 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1662 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1663 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1664 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 +00001665 viewport.MinZ = zNear;
1666 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 }
1668
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001669 if (viewport.Width <= 0 || viewport.Height <= 0)
1670 {
1671 return false; // Nothing to render
1672 }
1673
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674 device->SetViewport(&viewport);
1675
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001676 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001678 if (mState.scissorTest)
1679 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001680 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1681 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1682 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1683 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1684 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001685 device->SetScissorRect(&rect);
1686 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1687 }
1688 else
1689 {
1690 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1691 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001692
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001693 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694 }
1695
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001696 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001698 Program *programObject = getCurrentProgram();
1699
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001700 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001701 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001702 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001703
daniel@transgaming.com31754962010-11-28 02:02:52 +00001704 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001705 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1706 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1707 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001708 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001709
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001710 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001711 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001712 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001713
daniel@transgaming.com31754962010-11-28 02:02:52 +00001714 GLint depthRange = programObject->getDxDepthRangeLocation();
1715 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1716 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717 }
1718
1719 return true;
1720}
1721
1722// 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 +00001723void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724{
1725 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001726 Program *programObject = getCurrentProgram();
1727
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001728 Framebuffer *framebufferObject = getDrawFramebuffer();
1729
1730 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1731
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001732 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001733 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001734 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001736 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001737 GLint alwaysFront = !isTriangleMode(drawMode);
1738 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1739
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001740 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001741 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001742 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001744 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745 }
1746 else
1747 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749 }
1750
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 mCullStateDirty = false;
1752 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001754 if (mDepthStateDirty)
1755 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001756 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001758 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1759 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 }
1761 else
1762 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765
1766 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767 }
1768
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001769 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001771 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001772 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001773 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1774
1775 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1776 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1777 {
1778 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1779 }
1780 else
1781 {
1782 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1783 unorm<8>(mState.blendColor.alpha),
1784 unorm<8>(mState.blendColor.alpha),
1785 unorm<8>(mState.blendColor.alpha)));
1786 }
1787
1788 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1789 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1790 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1791
1792 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1793 mState.destBlendRGB != mState.destBlendAlpha ||
1794 mState.blendEquationRGB != mState.blendEquationAlpha)
1795 {
1796 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1797
1798 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1799 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1800 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001801 }
1802 else
1803 {
1804 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1805 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001806 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001807 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001808 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001809 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001810 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001811
1812 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 }
1814
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001815 if (mStencilStateDirty || mFrontFaceDirty)
1816 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001817 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001818 {
1819 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1820 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1821
1822 // FIXME: Unsupported by D3D9
1823 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1824 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1825 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1826 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1827 mState.stencilRef != mState.stencilBackRef ||
1828 mState.stencilMask != mState.stencilBackMask)
1829 {
1830 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1831 return error(GL_INVALID_OPERATION);
1832 }
1833
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001834 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001835 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001836 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1837
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001838 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1839 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001840 es2dx::ConvertComparison(mState.stencilFunc));
1841
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001842 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1843 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001844
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001845 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001846 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001847 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001849 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001850 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1851
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001852 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1853 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001854 es2dx::ConvertComparison(mState.stencilBackFunc));
1855
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001856 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1857 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001858
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001859 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001860 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001861 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001862 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001863 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001864 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1865 }
1866 else
1867 {
1868 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1869 }
1870
1871 mStencilStateDirty = false;
1872 }
1873
1874 if (mMaskStateDirty)
1875 {
1876 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1877 mState.colorMaskBlue, mState.colorMaskAlpha));
1878 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1879
1880 mMaskStateDirty = false;
1881 }
1882
1883 if (mPolygonOffsetStateDirty)
1884 {
1885 if (mState.polygonOffsetFill)
1886 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001887 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001888 if (depthbuffer)
1889 {
1890 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1891 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1892 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1893 }
1894 }
1895 else
1896 {
1897 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1898 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1899 }
1900
1901 mPolygonOffsetStateDirty = false;
1902 }
1903
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001904 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001906 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001907 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001908 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001909 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001910 FIXME("Sample alpha to coverage is unimplemented.");
1911 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001912
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001913 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1914 if (mState.sampleCoverage)
1915 {
1916 unsigned int mask = 0;
1917 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001918 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001919 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001920
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001921 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001922 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001923 mask <<= 1;
1924
1925 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1926 {
1927 threshold += 1.0f;
1928 mask |= 1;
1929 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001930 }
1931 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001932
1933 if (mState.sampleCoverageInvert)
1934 {
1935 mask = ~mask;
1936 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001937
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001938 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1939 }
1940 else
1941 {
1942 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1943 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001944 }
1945 else
1946 {
1947 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001948 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001949
1950 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951 }
1952
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001953 if (mDitherStateDirty)
1954 {
1955 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1956
1957 mDitherStateDirty = false;
1958 }
1959
1960 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001961}
1962
daniel@transgaming.com83921382011-01-08 05:46:00 +00001963GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001964{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001965 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001966
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001967 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001968 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001969 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001970 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001971 }
1972
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00001973 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001974}
1975
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001976// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001977GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001978{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001979 IDirect3DDevice9 *device = getDevice();
1980 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001981
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001982 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001983 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001984 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001985 }
1986
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001987 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001988}
1989
1990// Applies the shaders and shader constants to the Direct3D 9 device
1991void Context::applyShaders()
1992{
1993 IDirect3DDevice9 *device = getDevice();
1994 Program *programObject = getCurrentProgram();
1995 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1996 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1997
1998 device->SetVertexShader(vertexShader);
1999 device->SetPixelShader(pixelShader);
2000
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002001 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002002 {
2003 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002004 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002005 }
2006
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007 programObject->applyUniforms();
2008}
2009
2010// Applies the textures and sampler states to the Direct3D 9 device
2011void Context::applyTextures()
2012{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002013 applyTextures(SAMPLER_PIXEL);
2014
2015 if (mSupportsVertexTexture)
2016 {
2017 applyTextures(SAMPLER_VERTEX);
2018 }
2019}
2020
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002021// For each Direct3D 9 sampler of either the pixel or vertex stage,
2022// looks up the corresponding OpenGL texture image unit and texture type,
2023// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002024void Context::applyTextures(SamplerType type)
2025{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026 IDirect3DDevice9 *device = getDevice();
2027 Program *programObject = getCurrentProgram();
2028
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002029 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002030
2031 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002033 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002034 int d3dSampler = (type == SAMPLER_PIXEL) ? samplerIndex : D3DVERTEXTEXTURESAMPLER0 + samplerIndex;
2035 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2036
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002037 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002039 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002040
2041 Texture *texture = getSamplerTexture(textureUnit, textureType);
2042
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002043 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002044 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002045 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2046
2047 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002048 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002049 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002050 {
2051 GLenum wrapS = texture->getWrapS();
2052 GLenum wrapT = texture->getWrapT();
2053 GLenum minFilter = texture->getMinFilter();
2054 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002056 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2057 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002059 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002060 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2061 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002062 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2063 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002064 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002066 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002067 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002068 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002069 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002070 }
2071 else
2072 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002073 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002074 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002075
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002076 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002077 texture->resetDirty();
2078 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002080 else
2081 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002082 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002083 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002084 device->SetTexture(d3dSampler, NULL);
2085 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002086 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002087 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 }
2089}
2090
2091void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2092{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002093 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002094
2095 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2096 {
2097 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2098 }
2099
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002100 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2101 {
2102 return error(GL_INVALID_OPERATION);
2103 }
2104
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002106
2107 if (!renderTarget)
2108 {
2109 return; // Context must be lost, return silently
2110 }
2111
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 IDirect3DDevice9 *device = getDevice();
2113
2114 D3DSURFACE_DESC desc;
2115 renderTarget->GetDesc(&desc);
2116
2117 IDirect3DSurface9 *systemSurface;
2118 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2119
2120 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2121 {
2122 return error(GL_OUT_OF_MEMORY);
2123 }
2124
2125 ASSERT(SUCCEEDED(result));
2126
2127 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2128 {
2129 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2130 }
2131
2132 result = device->GetRenderTargetData(renderTarget, systemSurface);
2133
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134 if (FAILED(result))
2135 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136 systemSurface->Release();
2137
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002138 switch (result)
2139 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002140 case D3DERR_DRIVERINTERNALERROR:
2141 case D3DERR_DEVICELOST:
2142 return error(GL_OUT_OF_MEMORY);
2143 default:
2144 UNREACHABLE();
2145 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002146 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147 }
2148
2149 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002150 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2151 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2152 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2153 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2154 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155
2156 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2157
2158 if (FAILED(result))
2159 {
2160 UNREACHABLE();
2161 systemSurface->Release();
2162
2163 return; // No sensible error to generate
2164 }
2165
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002166 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002168 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002169 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002170 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002171
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172 for (int j = 0; j < rect.bottom - rect.top; j++)
2173 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002174 if (desc.Format == D3DFMT_A8R8G8B8 &&
2175 format == GL_BGRA_EXT &&
2176 type == GL_UNSIGNED_BYTE)
2177 {
2178 // Fast path for EXT_read_format_bgra, given
2179 // an RGBA source buffer. Note that buffers with no
2180 // alpha go through the slow path below.
2181 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002182 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002183 (rect.right - rect.left) * 4);
2184 continue;
2185 }
2186
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002187 for (int i = 0; i < rect.right - rect.left; i++)
2188 {
2189 float r;
2190 float g;
2191 float b;
2192 float a;
2193
2194 switch (desc.Format)
2195 {
2196 case D3DFMT_R5G6B5:
2197 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002198 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199
2200 a = 1.0f;
2201 b = (rgb & 0x001F) * (1.0f / 0x001F);
2202 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2203 r = (rgb & 0xF800) * (1.0f / 0xF800);
2204 }
2205 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206 case D3DFMT_A1R5G5B5:
2207 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002208 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209
2210 a = (argb & 0x8000) ? 1.0f : 0.0f;
2211 b = (argb & 0x001F) * (1.0f / 0x001F);
2212 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2213 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2214 }
2215 break;
2216 case D3DFMT_A8R8G8B8:
2217 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002218 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002219
2220 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2221 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2222 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2223 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2224 }
2225 break;
2226 case D3DFMT_X8R8G8B8:
2227 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002228 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002229
2230 a = 1.0f;
2231 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2232 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2233 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2234 }
2235 break;
2236 case D3DFMT_A2R10G10B10:
2237 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002238 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002239
2240 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2241 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2242 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2243 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2244 }
2245 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002246 case D3DFMT_A32B32G32R32F:
2247 {
2248 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002249 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2250 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2251 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2252 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002253 }
2254 break;
2255 case D3DFMT_A16B16G16R16F:
2256 {
2257 // float formats in D3D are stored rgba, rather than the other way round
2258 float abgr[4];
2259
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002260 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002261
2262 a = abgr[3];
2263 b = abgr[2];
2264 g = abgr[1];
2265 r = abgr[0];
2266 }
2267 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002268 default:
2269 UNIMPLEMENTED(); // FIXME
2270 UNREACHABLE();
2271 }
2272
2273 switch (format)
2274 {
2275 case GL_RGBA:
2276 switch (type)
2277 {
2278 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002279 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2280 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2281 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2282 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283 break;
2284 default: UNREACHABLE();
2285 }
2286 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002287 case GL_BGRA_EXT:
2288 switch (type)
2289 {
2290 case GL_UNSIGNED_BYTE:
2291 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2292 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2293 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2294 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2295 break;
2296 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2297 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2298 // this type is packed as follows:
2299 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2300 // --------------------------------------------------------------------------------
2301 // | 4th | 3rd | 2nd | 1st component |
2302 // --------------------------------------------------------------------------------
2303 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2304 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2305 ((unsigned short)(15 * a + 0.5f) << 12)|
2306 ((unsigned short)(15 * r + 0.5f) << 8) |
2307 ((unsigned short)(15 * g + 0.5f) << 4) |
2308 ((unsigned short)(15 * b + 0.5f) << 0);
2309 break;
2310 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2311 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2312 // this type is packed as follows:
2313 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2314 // --------------------------------------------------------------------------------
2315 // | 4th | 3rd | 2nd | 1st component |
2316 // --------------------------------------------------------------------------------
2317 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2318 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2319 ((unsigned short)( a + 0.5f) << 15) |
2320 ((unsigned short)(31 * r + 0.5f) << 10) |
2321 ((unsigned short)(31 * g + 0.5f) << 5) |
2322 ((unsigned short)(31 * b + 0.5f) << 0);
2323 break;
2324 default: UNREACHABLE();
2325 }
2326 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002327 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328 switch (type)
2329 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002330 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002331 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2332 ((unsigned short)(31 * b + 0.5f) << 0) |
2333 ((unsigned short)(63 * g + 0.5f) << 5) |
2334 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335 break;
2336 default: UNREACHABLE();
2337 }
2338 break;
2339 default: UNREACHABLE();
2340 }
2341 }
2342 }
2343
2344 systemSurface->UnlockRect();
2345
2346 systemSurface->Release();
2347}
2348
2349void Context::clear(GLbitfield mask)
2350{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002351 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002352
2353 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2354 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002355 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002356 }
2357
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002358 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 IDirect3DDevice9 *device = getDevice();
2360 DWORD flags = 0;
2361
2362 if (mask & GL_COLOR_BUFFER_BIT)
2363 {
2364 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002365
2366 if (framebufferObject->getColorbufferType() != GL_NONE)
2367 {
2368 flags |= D3DCLEAR_TARGET;
2369 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 }
2371
2372 if (mask & GL_DEPTH_BUFFER_BIT)
2373 {
2374 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002375 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 {
2377 flags |= D3DCLEAR_ZBUFFER;
2378 }
2379 }
2380
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 GLuint stencilUnmasked = 0x0;
2382
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002383 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002386 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002388 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002389 if (!depthStencil)
2390 {
2391 ERR("Depth stencil pointer unexpectedly null.");
2392 return;
2393 }
2394
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002395 D3DSURFACE_DESC desc;
2396 depthStencil->GetDesc(&desc);
2397
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002398 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002399 stencilUnmasked = (0x1 << stencilSize) - 1;
2400
2401 if (stencilUnmasked != 0x0)
2402 {
2403 flags |= D3DCLEAR_STENCIL;
2404 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 }
2406 }
2407
2408 if (mask != 0)
2409 {
2410 return error(GL_INVALID_VALUE);
2411 }
2412
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002413 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2414 {
2415 return;
2416 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002418 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002419 unorm<8>(mState.colorClearValue.red),
2420 unorm<8>(mState.colorClearValue.green),
2421 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002422 float depth = clamp01(mState.depthClearValue);
2423 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424
2425 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2426
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002427 if (!renderTarget)
2428 {
2429 return; // Context must be lost, return silently
2430 }
2431
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 D3DSURFACE_DESC desc;
2433 renderTarget->GetDesc(&desc);
2434
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002435 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436
2437 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002438 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002440 !(mState.colorMaskRed && mState.colorMaskGreen &&
2441 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442
2443 if (needMaskedColorClear || needMaskedStencilClear)
2444 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002445 // State which is altered in all paths from this point to the clear call is saved.
2446 // State which is altered in only some paths will be flagged dirty in the case that
2447 // that path is taken.
2448 HRESULT hr;
2449 if (mMaskedClearSavedState == NULL)
2450 {
2451 hr = device->BeginStateBlock();
2452 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2453
2454 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2455 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2456 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2457 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2458 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2459 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2460 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2461 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2462 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2463 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2464 device->SetPixelShader(NULL);
2465 device->SetVertexShader(NULL);
2466 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002467 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2468 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2469 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2470 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2471 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2472 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2473 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002474
2475 hr = device->EndStateBlock(&mMaskedClearSavedState);
2476 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2477 }
2478
2479 ASSERT(mMaskedClearSavedState != NULL);
2480
2481 if (mMaskedClearSavedState != NULL)
2482 {
2483 hr = mMaskedClearSavedState->Capture();
2484 ASSERT(SUCCEEDED(hr));
2485 }
2486
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2488 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2489 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2490 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2491 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2492 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2493 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2494 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2495
2496 if (flags & D3DCLEAR_TARGET)
2497 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002498 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499 }
2500 else
2501 {
2502 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2503 }
2504
2505 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2506 {
2507 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2508 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2509 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2510 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002511 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002512 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002513 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2514 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002515 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516 }
2517 else
2518 {
2519 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2520 }
2521
2522 device->SetPixelShader(NULL);
2523 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002524 device->SetFVF(D3DFVF_XYZRHW);
2525 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2526 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2527 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2528 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2529 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2530 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2531 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002533 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2534 quad[0][0] = -0.5f;
2535 quad[0][1] = desc.Height - 0.5f;
2536 quad[0][2] = 0.0f;
2537 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002539 quad[1][0] = desc.Width - 0.5f;
2540 quad[1][1] = desc.Height - 0.5f;
2541 quad[1][2] = 0.0f;
2542 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002544 quad[2][0] = -0.5f;
2545 quad[2][1] = -0.5f;
2546 quad[2][2] = 0.0f;
2547 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002549 quad[3][0] = desc.Width - 0.5f;
2550 quad[3][1] = -0.5f;
2551 quad[3][2] = 0.0f;
2552 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002554 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002555 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556
2557 if (flags & D3DCLEAR_ZBUFFER)
2558 {
2559 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2560 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2561 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2562 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002563
2564 if (mMaskedClearSavedState != NULL)
2565 {
2566 mMaskedClearSavedState->Apply();
2567 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002568 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002569 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 {
2571 device->Clear(0, NULL, flags, color, depth, stencil);
2572 }
2573}
2574
2575void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2576{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002577 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002578 {
2579 return error(GL_INVALID_OPERATION);
2580 }
2581
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002582 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583 IDirect3DDevice9 *device = getDevice();
2584 D3DPRIMITIVETYPE primitiveType;
2585 int primitiveCount;
2586
2587 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2588 return error(GL_INVALID_ENUM);
2589
2590 if (primitiveCount <= 0)
2591 {
2592 return;
2593 }
2594
2595 if (!applyRenderTarget(false))
2596 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002597 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598 }
2599
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002600 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002601
daniel@transgaming.com83921382011-01-08 05:46:00 +00002602 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002603 if (err != GL_NO_ERROR)
2604 {
2605 return error(err);
2606 }
2607
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608 applyShaders();
2609 applyTextures();
2610
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002611 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002612 {
2613 return error(GL_INVALID_OPERATION);
2614 }
2615
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002616 if (!cullSkipsDraw(mode))
2617 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002618 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002619
2620 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002621
2622 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2623 {
2624 drawClosingLine(first, first + count - 1);
2625 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002626 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627}
2628
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002629void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002630{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002631 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632 {
2633 return error(GL_INVALID_OPERATION);
2634 }
2635
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002636 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637 {
2638 return error(GL_INVALID_OPERATION);
2639 }
2640
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002641 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002642 IDirect3DDevice9 *device = getDevice();
2643 D3DPRIMITIVETYPE primitiveType;
2644 int primitiveCount;
2645
2646 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2647 return error(GL_INVALID_ENUM);
2648
2649 if (primitiveCount <= 0)
2650 {
2651 return;
2652 }
2653
2654 if (!applyRenderTarget(false))
2655 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002656 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002657 }
2658
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002659 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002660
2661 TranslatedIndexData indexInfo;
2662 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2663 if (err != GL_NO_ERROR)
2664 {
2665 return error(err);
2666 }
2667
daniel@transgaming.com83921382011-01-08 05:46:00 +00002668 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2669 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002670 if (err != GL_NO_ERROR)
2671 {
2672 return error(err);
2673 }
2674
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002675 applyShaders();
2676 applyTextures();
2677
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002678 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002679 {
2680 return error(GL_INVALID_OPERATION);
2681 }
2682
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002683 if (!cullSkipsDraw(mode))
2684 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002685 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002686
daniel@transgaming.com83921382011-01-08 05:46:00 +00002687 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002688
2689 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2690 {
2691 drawClosingLine(count, type, indices);
2692 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002693 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694}
2695
2696void Context::finish()
2697{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002698 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 IDirect3DDevice9 *device = getDevice();
2700 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002701 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002703 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2704 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002706 ERR("CreateQuery failed hr=%x\n", result);
2707 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2708 {
2709 return error(GL_OUT_OF_MEMORY);
2710 }
2711 ASSERT(false);
2712 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713 }
2714
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002715 IDirect3DStateBlock9 *savedState = NULL;
2716 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2717 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002718 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002719 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002721
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002722 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002723 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002724 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002725 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002726 ASSERT(false);
2727 return;
2728 }
2729
2730 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2731 if (FAILED(result))
2732 {
2733 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2734 occlusionQuery->Release();
2735 savedState->Release();
2736 ASSERT(false);
2737 return;
2738 }
2739
2740 // Render something outside the render target
2741 device->SetPixelShader(NULL);
2742 device->SetVertexShader(NULL);
2743 device->SetFVF(D3DFVF_XYZRHW);
2744 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2745 display->startScene();
2746 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2747
2748 result = occlusionQuery->Issue(D3DISSUE_END);
2749 if (FAILED(result))
2750 {
2751 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2752 occlusionQuery->Release();
2753 savedState->Apply();
2754 savedState->Release();
2755 ASSERT(false);
2756 return;
2757 }
2758
2759 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2760 {
2761 // Keep polling, but allow other threads to do something useful first
2762 Sleep(0);
2763 }
2764
2765 occlusionQuery->Release();
2766 savedState->Apply();
2767 savedState->Release();
2768
2769 if (result == D3DERR_DEVICELOST)
2770 {
2771 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002772 }
2773}
2774
2775void Context::flush()
2776{
2777 IDirect3DDevice9 *device = getDevice();
2778 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002779 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002780
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002781 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2782 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002783 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002784 ERR("CreateQuery failed hr=%x\n", result);
2785 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2786 {
2787 return error(GL_OUT_OF_MEMORY);
2788 }
2789 ASSERT(false);
2790 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002791 }
2792
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002793 result = eventQuery->Issue(D3DISSUE_END);
2794 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002796 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2797 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002798 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002799 return;
2800 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002801
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002802 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2803 eventQuery->Release();
2804
2805 if (result == D3DERR_DEVICELOST)
2806 {
2807 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808 }
2809}
2810
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002811void Context::drawClosingLine(unsigned int first, unsigned int last)
2812{
2813 IDirect3DDevice9 *device = getDevice();
2814 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002815 bool succeeded = false;
2816 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002817
2818 if (supports32bitIndices())
2819 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002820 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002821
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002822 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002823 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002824 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2825 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002826
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002827 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2828
2829 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2830 if (data)
2831 {
2832 data[0] = last;
2833 data[1] = first;
2834 mClosingIB->unmap();
2835 offset /= 4;
2836 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002837 }
2838 }
2839 else
2840 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002841 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002842
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002843 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002844 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002845 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2846 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002847
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002848 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2849
2850 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2851 if (data)
2852 {
2853 data[0] = last;
2854 data[1] = first;
2855 mClosingIB->unmap();
2856 offset /= 2;
2857 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002858 }
2859 }
2860
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002861 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002862 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002863 device->SetIndices(mClosingIB->getBuffer());
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002864
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002865 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002866 }
2867 else
2868 {
2869 ERR("Could not create an index buffer for closing a line loop.");
2870 error(GL_OUT_OF_MEMORY);
2871 }
2872}
2873
2874void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2875{
2876 unsigned int first = 0;
2877 unsigned int last = 0;
2878
2879 if (mState.elementArrayBuffer.get())
2880 {
2881 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2882 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2883 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2884 }
2885
2886 switch (type)
2887 {
2888 case GL_UNSIGNED_BYTE:
2889 first = static_cast<const GLubyte*>(indices)[0];
2890 last = static_cast<const GLubyte*>(indices)[count - 1];
2891 break;
2892 case GL_UNSIGNED_SHORT:
2893 first = static_cast<const GLushort*>(indices)[0];
2894 last = static_cast<const GLushort*>(indices)[count - 1];
2895 break;
2896 case GL_UNSIGNED_INT:
2897 first = static_cast<const GLuint*>(indices)[0];
2898 last = static_cast<const GLuint*>(indices)[count - 1];
2899 break;
2900 default: UNREACHABLE();
2901 }
2902
2903 drawClosingLine(first, last);
2904}
2905
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002906void Context::recordInvalidEnum()
2907{
2908 mInvalidEnum = true;
2909}
2910
2911void Context::recordInvalidValue()
2912{
2913 mInvalidValue = true;
2914}
2915
2916void Context::recordInvalidOperation()
2917{
2918 mInvalidOperation = true;
2919}
2920
2921void Context::recordOutOfMemory()
2922{
2923 mOutOfMemory = true;
2924}
2925
2926void Context::recordInvalidFramebufferOperation()
2927{
2928 mInvalidFramebufferOperation = true;
2929}
2930
2931// Get one of the recorded errors and clear its flag, if any.
2932// [OpenGL ES 2.0.24] section 2.5 page 13.
2933GLenum Context::getError()
2934{
2935 if (mInvalidEnum)
2936 {
2937 mInvalidEnum = false;
2938
2939 return GL_INVALID_ENUM;
2940 }
2941
2942 if (mInvalidValue)
2943 {
2944 mInvalidValue = false;
2945
2946 return GL_INVALID_VALUE;
2947 }
2948
2949 if (mInvalidOperation)
2950 {
2951 mInvalidOperation = false;
2952
2953 return GL_INVALID_OPERATION;
2954 }
2955
2956 if (mOutOfMemory)
2957 {
2958 mOutOfMemory = false;
2959
2960 return GL_OUT_OF_MEMORY;
2961 }
2962
2963 if (mInvalidFramebufferOperation)
2964 {
2965 mInvalidFramebufferOperation = false;
2966
2967 return GL_INVALID_FRAMEBUFFER_OPERATION;
2968 }
2969
2970 return GL_NO_ERROR;
2971}
2972
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002973bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002974{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002975 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002976}
2977
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002978int Context::getMaximumVaryingVectors() const
2979{
2980 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2981}
2982
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00002983unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002984{
2985 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
2986}
2987
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00002988unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002989{
2990 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
2991}
2992
daniel@transgaming.com458da142010-11-28 02:03:02 +00002993int Context::getMaximumFragmentUniformVectors() const
2994{
2995 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2996}
2997
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002998int Context::getMaxSupportedSamples() const
2999{
3000 return mMaxSupportedSamples;
3001}
3002
3003int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3004{
3005 if (requested == 0)
3006 {
3007 return requested;
3008 }
3009
3010 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3011 if (itr == mMultiSampleSupport.end())
3012 {
3013 return -1;
3014 }
3015
3016 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3017 {
3018 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3019 {
3020 return i;
3021 }
3022 }
3023
3024 return -1;
3025}
3026
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003027bool Context::supportsEventQueries() const
3028{
3029 return mSupportsEventQueries;
3030}
3031
daniel@transgaming.com01868132010-08-24 19:21:17 +00003032bool Context::supportsCompressedTextures() const
3033{
3034 return mSupportsCompressedTextures;
3035}
3036
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003037bool Context::supportsFloatTextures() const
3038{
3039 return mSupportsFloatTextures;
3040}
3041
3042bool Context::supportsFloatLinearFilter() const
3043{
3044 return mSupportsFloatLinearFilter;
3045}
3046
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003047bool Context::supportsFloatRenderableTextures() const
3048{
3049 return mSupportsFloatRenderableTextures;
3050}
3051
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003052bool Context::supportsHalfFloatTextures() const
3053{
3054 return mSupportsHalfFloatTextures;
3055}
3056
3057bool Context::supportsHalfFloatLinearFilter() const
3058{
3059 return mSupportsHalfFloatLinearFilter;
3060}
3061
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003062bool Context::supportsHalfFloatRenderableTextures() const
3063{
3064 return mSupportsHalfFloatRenderableTextures;
3065}
3066
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003067int Context::getMaximumRenderbufferDimension() const
3068{
3069 return mMaxRenderbufferDimension;
3070}
3071
3072int Context::getMaximumTextureDimension() const
3073{
3074 return mMaxTextureDimension;
3075}
3076
3077int Context::getMaximumCubeTextureDimension() const
3078{
3079 return mMaxCubeTextureDimension;
3080}
3081
3082int Context::getMaximumTextureLevel() const
3083{
3084 return mMaxTextureLevel;
3085}
3086
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003087bool Context::supportsLuminanceTextures() const
3088{
3089 return mSupportsLuminanceTextures;
3090}
3091
3092bool Context::supportsLuminanceAlphaTextures() const
3093{
3094 return mSupportsLuminanceAlphaTextures;
3095}
3096
daniel@transgaming.com83921382011-01-08 05:46:00 +00003097bool Context::supports32bitIndices() const
3098{
3099 return mSupports32bitIndices;
3100}
3101
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003102bool Context::supportsNonPower2Texture() const
3103{
3104 return mSupportsNonPower2Texture;
3105}
3106
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003107void Context::detachBuffer(GLuint buffer)
3108{
3109 // [OpenGL ES 2.0.24] section 2.9 page 22:
3110 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3111 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3112
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003113 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003114 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003115 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003116 }
3117
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003118 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003119 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003120 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003121 }
3122
3123 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3124 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003125 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003126 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003127 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003128 }
3129 }
3130}
3131
3132void Context::detachTexture(GLuint texture)
3133{
3134 // [OpenGL ES 2.0.24] section 3.8 page 84:
3135 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3136 // rebound to texture object zero
3137
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003138 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003139 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003140 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003141 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003142 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003143 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003144 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003145 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003146 }
3147 }
3148
3149 // [OpenGL ES 2.0.24] section 4.4 page 112:
3150 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3151 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3152 // image was attached in the currently bound framebuffer.
3153
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003154 Framebuffer *readFramebuffer = getReadFramebuffer();
3155 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003156
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003157 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003158 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003159 readFramebuffer->detachTexture(texture);
3160 }
3161
3162 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3163 {
3164 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003165 }
3166}
3167
3168void Context::detachFramebuffer(GLuint framebuffer)
3169{
3170 // [OpenGL ES 2.0.24] section 4.4 page 107:
3171 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3172 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3173
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003174 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003175 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003176 bindReadFramebuffer(0);
3177 }
3178
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003179 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003180 {
3181 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003182 }
3183}
3184
3185void Context::detachRenderbuffer(GLuint renderbuffer)
3186{
3187 // [OpenGL ES 2.0.24] section 4.4 page 109:
3188 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3189 // had been executed with the target RENDERBUFFER and name of zero.
3190
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003191 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003192 {
3193 bindRenderbuffer(0);
3194 }
3195
3196 // [OpenGL ES 2.0.24] section 4.4 page 111:
3197 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3198 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3199 // point to which this image was attached in the currently bound framebuffer.
3200
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003201 Framebuffer *readFramebuffer = getReadFramebuffer();
3202 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003203
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003204 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003205 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003206 readFramebuffer->detachRenderbuffer(renderbuffer);
3207 }
3208
3209 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3210 {
3211 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003212 }
3213}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003214
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003215Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003216{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003217 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003218
3219 if (t == NULL)
3220 {
3221 static const GLubyte color[] = { 0, 0, 0, 255 };
3222
3223 switch (type)
3224 {
3225 default:
3226 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003227 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003228
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003229 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003230 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003231 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003232 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003233 t = incomplete2d;
3234 }
3235 break;
3236
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003237 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003238 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003239 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003240
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003241 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3242 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3243 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3244 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3245 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3246 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003247
3248 t = incompleteCube;
3249 }
3250 break;
3251 }
3252
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003253 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003254 }
3255
3256 return t;
3257}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003258
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003259bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003260{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003261 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003262}
3263
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003264bool Context::isTriangleMode(GLenum drawMode)
3265{
3266 switch (drawMode)
3267 {
3268 case GL_TRIANGLES:
3269 case GL_TRIANGLE_FAN:
3270 case GL_TRIANGLE_STRIP:
3271 return true;
3272 case GL_POINTS:
3273 case GL_LINES:
3274 case GL_LINE_LOOP:
3275 case GL_LINE_STRIP:
3276 return false;
3277 default: UNREACHABLE();
3278 }
3279
3280 return false;
3281}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003282
3283void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3284{
3285 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3286
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003287 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3288 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3289 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3290 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003291
daniel@transgaming.com83921382011-01-08 05:46:00 +00003292 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003293}
3294
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003295void Context::initExtensionString()
3296{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003297 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003298 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3299 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003300 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003301 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003302 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003303
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003304 if (supportsEventQueries())
3305 {
3306 mExtensionString += "GL_NV_fence ";
3307 }
3308
daniel@transgaming.com01868132010-08-24 19:21:17 +00003309 if (supportsCompressedTextures())
3310 {
3311 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3312 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003313
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003314 if (supportsFloatTextures())
3315 {
3316 mExtensionString += "GL_OES_texture_float ";
3317 }
3318
3319 if (supportsHalfFloatTextures())
3320 {
3321 mExtensionString += "GL_OES_texture_half_float ";
3322 }
3323
3324 if (supportsFloatLinearFilter())
3325 {
3326 mExtensionString += "GL_OES_texture_float_linear ";
3327 }
3328
3329 if (supportsHalfFloatLinearFilter())
3330 {
3331 mExtensionString += "GL_OES_texture_half_float_linear ";
3332 }
3333
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003334 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003335 {
3336 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3337 }
3338
daniel@transgaming.com83921382011-01-08 05:46:00 +00003339 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003340 {
3341 mExtensionString += "GL_OES_element_index_uint ";
3342 }
3343
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003344 if (supportsNonPower2Texture())
3345 {
3346 mExtensionString += "GL_OES_texture_npot ";
3347 }
3348
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003349 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3350 if (end != std::string::npos)
3351 {
3352 mExtensionString.resize(end+1);
3353 }
3354}
3355
3356const char *Context::getExtensionString() const
3357{
3358 return mExtensionString.c_str();
3359}
3360
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003361void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3362 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3363 GLbitfield mask)
3364{
3365 IDirect3DDevice9 *device = getDevice();
3366
3367 Framebuffer *readFramebuffer = getReadFramebuffer();
3368 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3369
3370 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3371 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3372 {
3373 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3374 }
3375
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003376 if (drawFramebuffer->getSamples() != 0)
3377 {
3378 return error(GL_INVALID_OPERATION);
3379 }
3380
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003381 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3382 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3383 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3384 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3385
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003386 RECT sourceRect;
3387 RECT destRect;
3388
3389 if (srcX0 < srcX1)
3390 {
3391 sourceRect.left = srcX0;
3392 sourceRect.right = srcX1;
3393 destRect.left = dstX0;
3394 destRect.right = dstX1;
3395 }
3396 else
3397 {
3398 sourceRect.left = srcX1;
3399 destRect.left = dstX1;
3400 sourceRect.right = srcX0;
3401 destRect.right = dstX0;
3402 }
3403
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003404 if (srcY0 < srcY1)
3405 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003406 sourceRect.top = readBufferHeight - srcY1;
3407 destRect.top = drawBufferHeight - dstY1;
3408 sourceRect.bottom = readBufferHeight - srcY0;
3409 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003410 }
3411 else
3412 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003413 sourceRect.top = readBufferHeight - srcY0;
3414 destRect.top = drawBufferHeight - dstY0;
3415 sourceRect.bottom = readBufferHeight - srcY1;
3416 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003417 }
3418
3419 RECT sourceScissoredRect = sourceRect;
3420 RECT destScissoredRect = destRect;
3421
3422 if (mState.scissorTest)
3423 {
3424 // Only write to parts of the destination framebuffer which pass the scissor test
3425 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3426 // rect will be checked against scissorY, rather than the bottom.
3427 if (destRect.left < mState.scissorX)
3428 {
3429 int xDiff = mState.scissorX - destRect.left;
3430 destScissoredRect.left = mState.scissorX;
3431 sourceScissoredRect.left += xDiff;
3432 }
3433
3434 if (destRect.right > mState.scissorX + mState.scissorWidth)
3435 {
3436 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3437 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3438 sourceScissoredRect.right -= xDiff;
3439 }
3440
3441 if (destRect.top < mState.scissorY)
3442 {
3443 int yDiff = mState.scissorY - destRect.top;
3444 destScissoredRect.top = mState.scissorY;
3445 sourceScissoredRect.top += yDiff;
3446 }
3447
3448 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3449 {
3450 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3451 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3452 sourceScissoredRect.bottom -= yDiff;
3453 }
3454 }
3455
3456 bool blitRenderTarget = false;
3457 bool blitDepthStencil = false;
3458
3459 RECT sourceTrimmedRect = sourceScissoredRect;
3460 RECT destTrimmedRect = destScissoredRect;
3461
3462 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3463 // the actual draw and read surfaces.
3464 if (sourceTrimmedRect.left < 0)
3465 {
3466 int xDiff = 0 - sourceTrimmedRect.left;
3467 sourceTrimmedRect.left = 0;
3468 destTrimmedRect.left += xDiff;
3469 }
3470
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003471 if (sourceTrimmedRect.right > readBufferWidth)
3472 {
3473 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3474 sourceTrimmedRect.right = readBufferWidth;
3475 destTrimmedRect.right -= xDiff;
3476 }
3477
3478 if (sourceTrimmedRect.top < 0)
3479 {
3480 int yDiff = 0 - sourceTrimmedRect.top;
3481 sourceTrimmedRect.top = 0;
3482 destTrimmedRect.top += yDiff;
3483 }
3484
3485 if (sourceTrimmedRect.bottom > readBufferHeight)
3486 {
3487 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3488 sourceTrimmedRect.bottom = readBufferHeight;
3489 destTrimmedRect.bottom -= yDiff;
3490 }
3491
3492 if (destTrimmedRect.left < 0)
3493 {
3494 int xDiff = 0 - destTrimmedRect.left;
3495 destTrimmedRect.left = 0;
3496 sourceTrimmedRect.left += xDiff;
3497 }
3498
3499 if (destTrimmedRect.right > drawBufferWidth)
3500 {
3501 int xDiff = destTrimmedRect.right - drawBufferWidth;
3502 destTrimmedRect.right = drawBufferWidth;
3503 sourceTrimmedRect.right -= xDiff;
3504 }
3505
3506 if (destTrimmedRect.top < 0)
3507 {
3508 int yDiff = 0 - destTrimmedRect.top;
3509 destTrimmedRect.top = 0;
3510 sourceTrimmedRect.top += yDiff;
3511 }
3512
3513 if (destTrimmedRect.bottom > drawBufferHeight)
3514 {
3515 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3516 destTrimmedRect.bottom = drawBufferHeight;
3517 sourceTrimmedRect.bottom -= yDiff;
3518 }
3519
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003520 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003521 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3522 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3523 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3524 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003525 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3526 {
3527 partialBufferCopy = true;
3528 }
3529
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003530 if (mask & GL_COLOR_BUFFER_BIT)
3531 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003532 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3533 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3534 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3535 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3536 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003537 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3538 {
3539 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3540 return error(GL_INVALID_OPERATION);
3541 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003542
3543 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3544 {
3545 return error(GL_INVALID_OPERATION);
3546 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003547
3548 blitRenderTarget = true;
3549
3550 }
3551
3552 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3553 {
3554 DepthStencilbuffer *readDSBuffer = NULL;
3555 DepthStencilbuffer *drawDSBuffer = NULL;
3556
3557 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3558 // both a depth and stencil buffer, it will be the same buffer.
3559
3560 if (mask & GL_DEPTH_BUFFER_BIT)
3561 {
3562 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3563 {
3564 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3565 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3566 {
3567 return error(GL_INVALID_OPERATION);
3568 }
3569
3570 blitDepthStencil = true;
3571 readDSBuffer = readFramebuffer->getDepthbuffer();
3572 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3573 }
3574 }
3575
3576 if (mask & GL_STENCIL_BUFFER_BIT)
3577 {
3578 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3579 {
3580 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3581 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3582 {
3583 return error(GL_INVALID_OPERATION);
3584 }
3585
3586 blitDepthStencil = true;
3587 readDSBuffer = readFramebuffer->getStencilbuffer();
3588 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3589 }
3590 }
3591
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003592 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003593 {
3594 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3595 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3596 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003597
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003598 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3599 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003600 {
3601 return error(GL_INVALID_OPERATION);
3602 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003603 }
3604
3605 if (blitRenderTarget || blitDepthStencil)
3606 {
3607 egl::Display *display = getDisplay();
3608 display->endScene();
3609
3610 if (blitRenderTarget)
3611 {
3612 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3613 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3614
3615 if (FAILED(result))
3616 {
3617 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3618 return;
3619 }
3620 }
3621
3622 if (blitDepthStencil)
3623 {
3624 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3625
3626 if (FAILED(result))
3627 {
3628 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3629 return;
3630 }
3631 }
3632 }
3633}
3634
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003635VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3636{
3637 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3638 {
3639 mVertexDeclCache[i].vertexDeclaration = NULL;
3640 mVertexDeclCache[i].lruCount = 0;
3641 }
3642}
3643
3644VertexDeclarationCache::~VertexDeclarationCache()
3645{
3646 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3647 {
3648 if (mVertexDeclCache[i].vertexDeclaration)
3649 {
3650 mVertexDeclCache[i].vertexDeclaration->Release();
3651 }
3652 }
3653}
3654
3655GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3656{
3657 IDirect3DDevice9 *device = getDevice();
3658
3659 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3660 D3DVERTEXELEMENT9 *element = &elements[0];
3661
3662 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3663 {
3664 if (attributes[i].active)
3665 {
3666 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3667
3668 element->Stream = i;
3669 element->Offset = 0;
3670 element->Type = attributes[i].type;
3671 element->Method = D3DDECLMETHOD_DEFAULT;
3672 element->Usage = D3DDECLUSAGE_TEXCOORD;
3673 element->UsageIndex = program->getSemanticIndex(i);
3674 element++;
3675 }
3676 }
3677
3678 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3679 *(element++) = end;
3680
3681 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3682 {
3683 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3684 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3685 {
3686 entry->lruCount = ++mMaxLru;
3687 device->SetVertexDeclaration(entry->vertexDeclaration);
3688
3689 return GL_NO_ERROR;
3690 }
3691 }
3692
3693 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3694
3695 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3696 {
3697 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3698 {
3699 lastCache = &mVertexDeclCache[i];
3700 }
3701 }
3702
3703 if (lastCache->vertexDeclaration != NULL)
3704 {
3705 lastCache->vertexDeclaration->Release();
3706 lastCache->vertexDeclaration = NULL;
3707 }
3708
3709 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3710 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3711 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3712 lastCache->lruCount = ++mMaxLru;
3713
3714 return GL_NO_ERROR;
3715}
3716
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003717}
3718
3719extern "C"
3720{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003721gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003722{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003723 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003724}
3725
3726void glDestroyContext(gl::Context *context)
3727{
3728 delete context;
3729
3730 if (context == gl::getContext())
3731 {
3732 gl::makeCurrent(NULL, NULL, NULL);
3733 }
3734}
3735
3736void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3737{
3738 gl::makeCurrent(context, display, surface);
3739}
3740
3741gl::Context *glGetCurrentContext()
3742{
3743 return gl::getContext();
3744}
3745}