blob: 1bb3e807bf5d6890310787b85cac5138317bd0a4 [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
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001740 egl::Display *display = getDisplay();
1741 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
1742 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1743 // Apparently some ATI cards have a bug where a draw with a zero color
1744 // write mask can cause later draws to have incorrect results. Instead,
1745 // set a nonzero color write mask but modify the blend state so that no
1746 // drawing is done.
1747 // http://code.google.com/p/angleproject/issues/detail?id=169
1748
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001749 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001753 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 }
1755 else
1756 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 }
1759
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001760 mCullStateDirty = false;
1761 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 if (mDepthStateDirty)
1764 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001765 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1768 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769 }
1770 else
1771 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001772 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001774
1775 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776 }
1777
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001778 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1779 {
1780 mBlendStateDirty = true;
1781 mMaskStateDirty = true;
1782 }
1783
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001784 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001786 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001787 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001788 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1789
1790 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1791 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1792 {
1793 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1794 }
1795 else
1796 {
1797 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1798 unorm<8>(mState.blendColor.alpha),
1799 unorm<8>(mState.blendColor.alpha),
1800 unorm<8>(mState.blendColor.alpha)));
1801 }
1802
1803 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1804 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1805 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1806
1807 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1808 mState.destBlendRGB != mState.destBlendAlpha ||
1809 mState.blendEquationRGB != mState.blendEquationAlpha)
1810 {
1811 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1812
1813 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1814 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1815 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001816 }
1817 else
1818 {
1819 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1820 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001821 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001822 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001823 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001824 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001825 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826
1827 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828 }
1829
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001830 if (mStencilStateDirty || mFrontFaceDirty)
1831 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001832 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001833 {
1834 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1835 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1836
1837 // FIXME: Unsupported by D3D9
1838 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1839 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1840 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1841 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1842 mState.stencilRef != mState.stencilBackRef ||
1843 mState.stencilMask != mState.stencilBackMask)
1844 {
1845 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1846 return error(GL_INVALID_OPERATION);
1847 }
1848
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001849 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001850 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001851 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1852
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001853 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1854 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001855 es2dx::ConvertComparison(mState.stencilFunc));
1856
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001857 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1858 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001859
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001860 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001861 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001862 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001863 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001864 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001865 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1866
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001867 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1868 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001869 es2dx::ConvertComparison(mState.stencilBackFunc));
1870
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001871 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1872 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001873
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001874 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001875 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001876 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001877 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001878 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001879 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1880 }
1881 else
1882 {
1883 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1884 }
1885
1886 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001887 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001888 }
1889
1890 if (mMaskStateDirty)
1891 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001892 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1893 mState.colorMaskBlue, mState.colorMaskAlpha);
1894 if (colorMask == 0 && !zeroColorMaskAllowed)
1895 {
1896 // Enable green channel, but set blending so nothing will be drawn.
1897 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1898 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1899
1900 device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1901 device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1902 device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
1903 }
1904 else
1905 {
1906 device->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
1907 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001908 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1909
1910 mMaskStateDirty = false;
1911 }
1912
1913 if (mPolygonOffsetStateDirty)
1914 {
1915 if (mState.polygonOffsetFill)
1916 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001917 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001918 if (depthbuffer)
1919 {
1920 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1921 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1922 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1923 }
1924 }
1925 else
1926 {
1927 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1928 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1929 }
1930
1931 mPolygonOffsetStateDirty = false;
1932 }
1933
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001934 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001936 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001937 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001938 FIXME("Sample alpha to coverage is unimplemented.");
1939 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001940
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001941 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1942 if (mState.sampleCoverage)
1943 {
1944 unsigned int mask = 0;
1945 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001946 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001947 float threshold = 0.5f;
1948
1949 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001950 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001951 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001952
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001953 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001954 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001955 threshold += 1.0f;
1956 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001957 }
1958 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001959 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001960
1961 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001962 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001963 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001964 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001965
1966 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001967 }
1968 else
1969 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001970 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001971 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001972
1973 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974 }
1975
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001976 if (mDitherStateDirty)
1977 {
1978 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1979
1980 mDitherStateDirty = false;
1981 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982}
1983
daniel@transgaming.com83921382011-01-08 05:46:00 +00001984GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001985{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001986 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001987
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001988 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001989 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001990 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001991 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001992 }
1993
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00001994 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001995}
1996
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001997// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001998GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001999{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002000 IDirect3DDevice9 *device = getDevice();
2001 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002002
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002003 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002004 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00002005 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002006 }
2007
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002008 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009}
2010
2011// Applies the shaders and shader constants to the Direct3D 9 device
2012void Context::applyShaders()
2013{
2014 IDirect3DDevice9 *device = getDevice();
2015 Program *programObject = getCurrentProgram();
2016 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2017 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2018
2019 device->SetVertexShader(vertexShader);
2020 device->SetPixelShader(pixelShader);
2021
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002022 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002023 {
2024 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002025 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002026 }
2027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028 programObject->applyUniforms();
2029}
2030
2031// Applies the textures and sampler states to the Direct3D 9 device
2032void Context::applyTextures()
2033{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002034 applyTextures(SAMPLER_PIXEL);
2035
2036 if (mSupportsVertexTexture)
2037 {
2038 applyTextures(SAMPLER_VERTEX);
2039 }
2040}
2041
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002042// For each Direct3D 9 sampler of either the pixel or vertex stage,
2043// looks up the corresponding OpenGL texture image unit and texture type,
2044// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002045void Context::applyTextures(SamplerType type)
2046{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002047 IDirect3DDevice9 *device = getDevice();
2048 Program *programObject = getCurrentProgram();
2049
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002050 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 +00002051
2052 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002054 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002055 int d3dSampler = (type == SAMPLER_PIXEL) ? samplerIndex : D3DVERTEXTEXTURESAMPLER0 + samplerIndex;
2056 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2057
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002058 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002060 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002061
2062 Texture *texture = getSamplerTexture(textureUnit, textureType);
2063
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002064 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002065 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002066 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2067
2068 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002069 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002070 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002071 {
2072 GLenum wrapS = texture->getWrapS();
2073 GLenum wrapT = texture->getWrapT();
2074 GLenum minFilter = texture->getMinFilter();
2075 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002077 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2078 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002080 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002081 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2082 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002083 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2084 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002085 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002087 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002088 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002089 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002090 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002091 }
2092 else
2093 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002094 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002095 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002096
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002097 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002098 texture->resetDirty();
2099 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002101 else
2102 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002103 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002104 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002105 device->SetTexture(d3dSampler, NULL);
2106 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002107 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 }
2110}
2111
2112void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2113{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002114 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002115
2116 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2117 {
2118 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2119 }
2120
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002121 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2122 {
2123 return error(GL_INVALID_OPERATION);
2124 }
2125
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002127
2128 if (!renderTarget)
2129 {
2130 return; // Context must be lost, return silently
2131 }
2132
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 IDirect3DDevice9 *device = getDevice();
2134
2135 D3DSURFACE_DESC desc;
2136 renderTarget->GetDesc(&desc);
2137
2138 IDirect3DSurface9 *systemSurface;
2139 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2140
2141 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2142 {
2143 return error(GL_OUT_OF_MEMORY);
2144 }
2145
2146 ASSERT(SUCCEEDED(result));
2147
2148 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2149 {
2150 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2151 }
2152
2153 result = device->GetRenderTargetData(renderTarget, systemSurface);
2154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 if (FAILED(result))
2156 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157 systemSurface->Release();
2158
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002159 switch (result)
2160 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002161 // It turns out that D3D will sometimes produce more error
2162 // codes than those documented.
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002163 case D3DERR_DRIVERINTERNALERROR:
2164 case D3DERR_DEVICELOST:
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002165 case D3DERR_DEVICEHUNG:
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002166 return error(GL_OUT_OF_MEMORY);
2167 default:
2168 UNREACHABLE();
2169 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002170 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002171 }
2172
2173 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002174 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2175 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2176 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2177 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2178 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179
2180 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2181
2182 if (FAILED(result))
2183 {
2184 UNREACHABLE();
2185 systemSurface->Release();
2186
2187 return; // No sensible error to generate
2188 }
2189
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002190 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002191 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002192 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002193 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002194 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002195
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002196 for (int j = 0; j < rect.bottom - rect.top; j++)
2197 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002198 if (desc.Format == D3DFMT_A8R8G8B8 &&
2199 format == GL_BGRA_EXT &&
2200 type == GL_UNSIGNED_BYTE)
2201 {
2202 // Fast path for EXT_read_format_bgra, given
2203 // an RGBA source buffer. Note that buffers with no
2204 // alpha go through the slow path below.
2205 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002206 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002207 (rect.right - rect.left) * 4);
2208 continue;
2209 }
2210
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211 for (int i = 0; i < rect.right - rect.left; i++)
2212 {
2213 float r;
2214 float g;
2215 float b;
2216 float a;
2217
2218 switch (desc.Format)
2219 {
2220 case D3DFMT_R5G6B5:
2221 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002222 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002223
2224 a = 1.0f;
2225 b = (rgb & 0x001F) * (1.0f / 0x001F);
2226 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2227 r = (rgb & 0xF800) * (1.0f / 0xF800);
2228 }
2229 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002230 case D3DFMT_A1R5G5B5:
2231 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002232 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233
2234 a = (argb & 0x8000) ? 1.0f : 0.0f;
2235 b = (argb & 0x001F) * (1.0f / 0x001F);
2236 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2237 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2238 }
2239 break;
2240 case D3DFMT_A8R8G8B8:
2241 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002242 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002243
2244 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2245 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2246 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2247 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2248 }
2249 break;
2250 case D3DFMT_X8R8G8B8:
2251 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002252 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253
2254 a = 1.0f;
2255 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2256 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2257 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2258 }
2259 break;
2260 case D3DFMT_A2R10G10B10:
2261 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002262 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263
2264 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2265 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2266 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2267 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2268 }
2269 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002270 case D3DFMT_A32B32G32R32F:
2271 {
2272 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002273 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2274 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2275 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2276 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002277 }
2278 break;
2279 case D3DFMT_A16B16G16R16F:
2280 {
2281 // float formats in D3D are stored rgba, rather than the other way round
2282 float abgr[4];
2283
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002284 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002285
2286 a = abgr[3];
2287 b = abgr[2];
2288 g = abgr[1];
2289 r = abgr[0];
2290 }
2291 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 default:
2293 UNIMPLEMENTED(); // FIXME
2294 UNREACHABLE();
2295 }
2296
2297 switch (format)
2298 {
2299 case GL_RGBA:
2300 switch (type)
2301 {
2302 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002303 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2304 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2305 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2306 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 break;
2308 default: UNREACHABLE();
2309 }
2310 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002311 case GL_BGRA_EXT:
2312 switch (type)
2313 {
2314 case GL_UNSIGNED_BYTE:
2315 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2316 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2317 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2318 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2319 break;
2320 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2321 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2322 // this type is packed as follows:
2323 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2324 // --------------------------------------------------------------------------------
2325 // | 4th | 3rd | 2nd | 1st component |
2326 // --------------------------------------------------------------------------------
2327 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2328 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2329 ((unsigned short)(15 * a + 0.5f) << 12)|
2330 ((unsigned short)(15 * r + 0.5f) << 8) |
2331 ((unsigned short)(15 * g + 0.5f) << 4) |
2332 ((unsigned short)(15 * b + 0.5f) << 0);
2333 break;
2334 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2335 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2336 // this type is packed as follows:
2337 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2338 // --------------------------------------------------------------------------------
2339 // | 4th | 3rd | 2nd | 1st component |
2340 // --------------------------------------------------------------------------------
2341 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2342 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2343 ((unsigned short)( a + 0.5f) << 15) |
2344 ((unsigned short)(31 * r + 0.5f) << 10) |
2345 ((unsigned short)(31 * g + 0.5f) << 5) |
2346 ((unsigned short)(31 * b + 0.5f) << 0);
2347 break;
2348 default: UNREACHABLE();
2349 }
2350 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002351 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 switch (type)
2353 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002354 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002355 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2356 ((unsigned short)(31 * b + 0.5f) << 0) |
2357 ((unsigned short)(63 * g + 0.5f) << 5) |
2358 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 break;
2360 default: UNREACHABLE();
2361 }
2362 break;
2363 default: UNREACHABLE();
2364 }
2365 }
2366 }
2367
2368 systemSurface->UnlockRect();
2369
2370 systemSurface->Release();
2371}
2372
2373void Context::clear(GLbitfield mask)
2374{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002375 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002376
2377 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2378 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002379 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002380 }
2381
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002382 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383 IDirect3DDevice9 *device = getDevice();
2384 DWORD flags = 0;
2385
2386 if (mask & GL_COLOR_BUFFER_BIT)
2387 {
2388 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002389
2390 if (framebufferObject->getColorbufferType() != GL_NONE)
2391 {
2392 flags |= D3DCLEAR_TARGET;
2393 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394 }
2395
2396 if (mask & GL_DEPTH_BUFFER_BIT)
2397 {
2398 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002399 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 {
2401 flags |= D3DCLEAR_ZBUFFER;
2402 }
2403 }
2404
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 GLuint stencilUnmasked = 0x0;
2406
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002407 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002410 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002412 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002413 if (!depthStencil)
2414 {
2415 ERR("Depth stencil pointer unexpectedly null.");
2416 return;
2417 }
2418
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002419 D3DSURFACE_DESC desc;
2420 depthStencil->GetDesc(&desc);
2421
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002422 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002423 stencilUnmasked = (0x1 << stencilSize) - 1;
2424
2425 if (stencilUnmasked != 0x0)
2426 {
2427 flags |= D3DCLEAR_STENCIL;
2428 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 }
2430 }
2431
2432 if (mask != 0)
2433 {
2434 return error(GL_INVALID_VALUE);
2435 }
2436
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002437 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2438 {
2439 return;
2440 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002442 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002443 unorm<8>(mState.colorClearValue.red),
2444 unorm<8>(mState.colorClearValue.green),
2445 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002446 float depth = clamp01(mState.depthClearValue);
2447 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448
2449 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2450
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002451 if (!renderTarget)
2452 {
2453 return; // Context must be lost, return silently
2454 }
2455
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 D3DSURFACE_DESC desc;
2457 renderTarget->GetDesc(&desc);
2458
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002459 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460
2461 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002462 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002464 !(mState.colorMaskRed && mState.colorMaskGreen &&
2465 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466
2467 if (needMaskedColorClear || needMaskedStencilClear)
2468 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002469 // State which is altered in all paths from this point to the clear call is saved.
2470 // State which is altered in only some paths will be flagged dirty in the case that
2471 // that path is taken.
2472 HRESULT hr;
2473 if (mMaskedClearSavedState == NULL)
2474 {
2475 hr = device->BeginStateBlock();
2476 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2477
2478 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2479 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2480 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2481 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2482 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2483 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2484 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2485 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2486 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2487 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2488 device->SetPixelShader(NULL);
2489 device->SetVertexShader(NULL);
2490 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002491 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2492 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2493 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2494 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2495 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2496 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2497 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002498
2499 hr = device->EndStateBlock(&mMaskedClearSavedState);
2500 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2501 }
2502
2503 ASSERT(mMaskedClearSavedState != NULL);
2504
2505 if (mMaskedClearSavedState != NULL)
2506 {
2507 hr = mMaskedClearSavedState->Capture();
2508 ASSERT(SUCCEEDED(hr));
2509 }
2510
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002511 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2512 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2513 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2514 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2515 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2516 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2517 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2518 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2519
2520 if (flags & D3DCLEAR_TARGET)
2521 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002522 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523 }
2524 else
2525 {
2526 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2527 }
2528
2529 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2530 {
2531 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2532 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2533 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2534 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002535 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002536 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2538 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002539 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540 }
2541 else
2542 {
2543 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2544 }
2545
2546 device->SetPixelShader(NULL);
2547 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002548 device->SetFVF(D3DFVF_XYZRHW);
2549 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2550 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2551 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2552 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2553 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2554 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2555 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002557 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2558 quad[0][0] = -0.5f;
2559 quad[0][1] = desc.Height - 0.5f;
2560 quad[0][2] = 0.0f;
2561 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002563 quad[1][0] = desc.Width - 0.5f;
2564 quad[1][1] = desc.Height - 0.5f;
2565 quad[1][2] = 0.0f;
2566 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002568 quad[2][0] = -0.5f;
2569 quad[2][1] = -0.5f;
2570 quad[2][2] = 0.0f;
2571 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002572
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002573 quad[3][0] = desc.Width - 0.5f;
2574 quad[3][1] = -0.5f;
2575 quad[3][2] = 0.0f;
2576 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002578 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002579 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580
2581 if (flags & D3DCLEAR_ZBUFFER)
2582 {
2583 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2584 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2585 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2586 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002587
2588 if (mMaskedClearSavedState != NULL)
2589 {
2590 mMaskedClearSavedState->Apply();
2591 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002593 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594 {
2595 device->Clear(0, NULL, flags, color, depth, stencil);
2596 }
2597}
2598
2599void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2600{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002601 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602 {
2603 return error(GL_INVALID_OPERATION);
2604 }
2605
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002606 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002607 IDirect3DDevice9 *device = getDevice();
2608 D3DPRIMITIVETYPE primitiveType;
2609 int primitiveCount;
2610
2611 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2612 return error(GL_INVALID_ENUM);
2613
2614 if (primitiveCount <= 0)
2615 {
2616 return;
2617 }
2618
2619 if (!applyRenderTarget(false))
2620 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002621 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622 }
2623
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002624 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002625
daniel@transgaming.com83921382011-01-08 05:46:00 +00002626 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002627 if (err != GL_NO_ERROR)
2628 {
2629 return error(err);
2630 }
2631
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632 applyShaders();
2633 applyTextures();
2634
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002635 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002636 {
2637 return error(GL_INVALID_OPERATION);
2638 }
2639
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002640 if (!cullSkipsDraw(mode))
2641 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002642 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002643
2644 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002645
2646 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2647 {
2648 drawClosingLine(first, first + count - 1);
2649 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651}
2652
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002653void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002654{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002655 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656 {
2657 return error(GL_INVALID_OPERATION);
2658 }
2659
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002660 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661 {
2662 return error(GL_INVALID_OPERATION);
2663 }
2664
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002665 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 IDirect3DDevice9 *device = getDevice();
2667 D3DPRIMITIVETYPE primitiveType;
2668 int primitiveCount;
2669
2670 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2671 return error(GL_INVALID_ENUM);
2672
2673 if (primitiveCount <= 0)
2674 {
2675 return;
2676 }
2677
2678 if (!applyRenderTarget(false))
2679 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002680 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 }
2682
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002683 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002684
2685 TranslatedIndexData indexInfo;
2686 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2687 if (err != GL_NO_ERROR)
2688 {
2689 return error(err);
2690 }
2691
daniel@transgaming.com83921382011-01-08 05:46:00 +00002692 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2693 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002694 if (err != GL_NO_ERROR)
2695 {
2696 return error(err);
2697 }
2698
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 applyShaders();
2700 applyTextures();
2701
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002702 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002703 {
2704 return error(GL_INVALID_OPERATION);
2705 }
2706
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002707 if (!cullSkipsDraw(mode))
2708 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002709 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002710
daniel@transgaming.com83921382011-01-08 05:46:00 +00002711 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002712
2713 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2714 {
2715 drawClosingLine(count, type, indices);
2716 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002717 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002718}
2719
2720void Context::finish()
2721{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002722 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002723 IDirect3DDevice9 *device = getDevice();
2724 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002725 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002727 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2728 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002729 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002730 ERR("CreateQuery failed hr=%x\n", result);
2731 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2732 {
2733 return error(GL_OUT_OF_MEMORY);
2734 }
2735 ASSERT(false);
2736 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737 }
2738
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002739 IDirect3DStateBlock9 *savedState = NULL;
2740 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2741 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002743 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002744 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002745
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002746 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002747 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002748 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002749 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002750 ASSERT(false);
2751 return;
2752 }
2753
2754 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2755 if (FAILED(result))
2756 {
2757 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2758 occlusionQuery->Release();
2759 savedState->Release();
2760 ASSERT(false);
2761 return;
2762 }
2763
2764 // Render something outside the render target
2765 device->SetPixelShader(NULL);
2766 device->SetVertexShader(NULL);
2767 device->SetFVF(D3DFVF_XYZRHW);
2768 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2769 display->startScene();
2770 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2771
2772 result = occlusionQuery->Issue(D3DISSUE_END);
2773 if (FAILED(result))
2774 {
2775 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2776 occlusionQuery->Release();
2777 savedState->Apply();
2778 savedState->Release();
2779 ASSERT(false);
2780 return;
2781 }
2782
2783 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2784 {
2785 // Keep polling, but allow other threads to do something useful first
2786 Sleep(0);
2787 }
2788
2789 occlusionQuery->Release();
2790 savedState->Apply();
2791 savedState->Release();
2792
2793 if (result == D3DERR_DEVICELOST)
2794 {
2795 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796 }
2797}
2798
2799void Context::flush()
2800{
2801 IDirect3DDevice9 *device = getDevice();
2802 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002803 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002805 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2806 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002808 ERR("CreateQuery failed hr=%x\n", result);
2809 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2810 {
2811 return error(GL_OUT_OF_MEMORY);
2812 }
2813 ASSERT(false);
2814 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 }
2816
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002817 result = eventQuery->Issue(D3DISSUE_END);
2818 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002819 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002820 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2821 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002822 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002823 return;
2824 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002825
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002826 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2827 eventQuery->Release();
2828
2829 if (result == D3DERR_DEVICELOST)
2830 {
2831 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002832 }
2833}
2834
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002835void Context::drawClosingLine(unsigned int first, unsigned int last)
2836{
2837 IDirect3DDevice9 *device = getDevice();
2838 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002839 bool succeeded = false;
2840 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002841
2842 if (supports32bitIndices())
2843 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002844 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002845
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002846 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002847 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002848 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2849 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002850
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002851 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2852
2853 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2854 if (data)
2855 {
2856 data[0] = last;
2857 data[1] = first;
2858 mClosingIB->unmap();
2859 offset /= 4;
2860 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002861 }
2862 }
2863 else
2864 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002865 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002866
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002867 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002868 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002869 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2870 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002871
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002872 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2873
2874 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2875 if (data)
2876 {
2877 data[0] = last;
2878 data[1] = first;
2879 mClosingIB->unmap();
2880 offset /= 2;
2881 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002882 }
2883 }
2884
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002885 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002886 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002887 device->SetIndices(mClosingIB->getBuffer());
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002888
jbauman@chromium.org2c199b12011-06-13 22:20:06 +00002889 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002890 }
2891 else
2892 {
2893 ERR("Could not create an index buffer for closing a line loop.");
2894 error(GL_OUT_OF_MEMORY);
2895 }
2896}
2897
2898void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2899{
2900 unsigned int first = 0;
2901 unsigned int last = 0;
2902
2903 if (mState.elementArrayBuffer.get())
2904 {
2905 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2906 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2907 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2908 }
2909
2910 switch (type)
2911 {
2912 case GL_UNSIGNED_BYTE:
2913 first = static_cast<const GLubyte*>(indices)[0];
2914 last = static_cast<const GLubyte*>(indices)[count - 1];
2915 break;
2916 case GL_UNSIGNED_SHORT:
2917 first = static_cast<const GLushort*>(indices)[0];
2918 last = static_cast<const GLushort*>(indices)[count - 1];
2919 break;
2920 case GL_UNSIGNED_INT:
2921 first = static_cast<const GLuint*>(indices)[0];
2922 last = static_cast<const GLuint*>(indices)[count - 1];
2923 break;
2924 default: UNREACHABLE();
2925 }
2926
2927 drawClosingLine(first, last);
2928}
2929
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002930void Context::recordInvalidEnum()
2931{
2932 mInvalidEnum = true;
2933}
2934
2935void Context::recordInvalidValue()
2936{
2937 mInvalidValue = true;
2938}
2939
2940void Context::recordInvalidOperation()
2941{
2942 mInvalidOperation = true;
2943}
2944
2945void Context::recordOutOfMemory()
2946{
2947 mOutOfMemory = true;
2948}
2949
2950void Context::recordInvalidFramebufferOperation()
2951{
2952 mInvalidFramebufferOperation = true;
2953}
2954
2955// Get one of the recorded errors and clear its flag, if any.
2956// [OpenGL ES 2.0.24] section 2.5 page 13.
2957GLenum Context::getError()
2958{
2959 if (mInvalidEnum)
2960 {
2961 mInvalidEnum = false;
2962
2963 return GL_INVALID_ENUM;
2964 }
2965
2966 if (mInvalidValue)
2967 {
2968 mInvalidValue = false;
2969
2970 return GL_INVALID_VALUE;
2971 }
2972
2973 if (mInvalidOperation)
2974 {
2975 mInvalidOperation = false;
2976
2977 return GL_INVALID_OPERATION;
2978 }
2979
2980 if (mOutOfMemory)
2981 {
2982 mOutOfMemory = false;
2983
2984 return GL_OUT_OF_MEMORY;
2985 }
2986
2987 if (mInvalidFramebufferOperation)
2988 {
2989 mInvalidFramebufferOperation = false;
2990
2991 return GL_INVALID_FRAMEBUFFER_OPERATION;
2992 }
2993
2994 return GL_NO_ERROR;
2995}
2996
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002997bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002998{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002999 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003000}
3001
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003002int Context::getMaximumVaryingVectors() const
3003{
3004 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3005}
3006
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003007unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003008{
3009 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3010}
3011
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003012unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003013{
3014 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3015}
3016
daniel@transgaming.com458da142010-11-28 02:03:02 +00003017int Context::getMaximumFragmentUniformVectors() const
3018{
3019 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3020}
3021
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003022int Context::getMaxSupportedSamples() const
3023{
3024 return mMaxSupportedSamples;
3025}
3026
3027int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3028{
3029 if (requested == 0)
3030 {
3031 return requested;
3032 }
3033
3034 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3035 if (itr == mMultiSampleSupport.end())
3036 {
3037 return -1;
3038 }
3039
3040 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3041 {
3042 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3043 {
3044 return i;
3045 }
3046 }
3047
3048 return -1;
3049}
3050
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003051bool Context::supportsEventQueries() const
3052{
3053 return mSupportsEventQueries;
3054}
3055
daniel@transgaming.com01868132010-08-24 19:21:17 +00003056bool Context::supportsCompressedTextures() const
3057{
3058 return mSupportsCompressedTextures;
3059}
3060
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003061bool Context::supportsFloatTextures() const
3062{
3063 return mSupportsFloatTextures;
3064}
3065
3066bool Context::supportsFloatLinearFilter() const
3067{
3068 return mSupportsFloatLinearFilter;
3069}
3070
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003071bool Context::supportsFloatRenderableTextures() const
3072{
3073 return mSupportsFloatRenderableTextures;
3074}
3075
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003076bool Context::supportsHalfFloatTextures() const
3077{
3078 return mSupportsHalfFloatTextures;
3079}
3080
3081bool Context::supportsHalfFloatLinearFilter() const
3082{
3083 return mSupportsHalfFloatLinearFilter;
3084}
3085
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003086bool Context::supportsHalfFloatRenderableTextures() const
3087{
3088 return mSupportsHalfFloatRenderableTextures;
3089}
3090
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003091int Context::getMaximumRenderbufferDimension() const
3092{
3093 return mMaxRenderbufferDimension;
3094}
3095
3096int Context::getMaximumTextureDimension() const
3097{
3098 return mMaxTextureDimension;
3099}
3100
3101int Context::getMaximumCubeTextureDimension() const
3102{
3103 return mMaxCubeTextureDimension;
3104}
3105
3106int Context::getMaximumTextureLevel() const
3107{
3108 return mMaxTextureLevel;
3109}
3110
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003111bool Context::supportsLuminanceTextures() const
3112{
3113 return mSupportsLuminanceTextures;
3114}
3115
3116bool Context::supportsLuminanceAlphaTextures() const
3117{
3118 return mSupportsLuminanceAlphaTextures;
3119}
3120
daniel@transgaming.com83921382011-01-08 05:46:00 +00003121bool Context::supports32bitIndices() const
3122{
3123 return mSupports32bitIndices;
3124}
3125
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003126bool Context::supportsNonPower2Texture() const
3127{
3128 return mSupportsNonPower2Texture;
3129}
3130
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003131void Context::detachBuffer(GLuint buffer)
3132{
3133 // [OpenGL ES 2.0.24] section 2.9 page 22:
3134 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3135 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3136
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003137 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003138 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003139 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003140 }
3141
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003142 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003143 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003144 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003145 }
3146
3147 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3148 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003149 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003150 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003151 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003152 }
3153 }
3154}
3155
3156void Context::detachTexture(GLuint texture)
3157{
3158 // [OpenGL ES 2.0.24] section 3.8 page 84:
3159 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3160 // rebound to texture object zero
3161
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003162 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003163 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003164 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003165 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003166 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003167 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003168 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003169 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003170 }
3171 }
3172
3173 // [OpenGL ES 2.0.24] section 4.4 page 112:
3174 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3175 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3176 // image was attached in the currently bound framebuffer.
3177
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003178 Framebuffer *readFramebuffer = getReadFramebuffer();
3179 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003180
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003181 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003182 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003183 readFramebuffer->detachTexture(texture);
3184 }
3185
3186 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3187 {
3188 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003189 }
3190}
3191
3192void Context::detachFramebuffer(GLuint framebuffer)
3193{
3194 // [OpenGL ES 2.0.24] section 4.4 page 107:
3195 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3196 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3197
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003198 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003199 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003200 bindReadFramebuffer(0);
3201 }
3202
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003203 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003204 {
3205 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003206 }
3207}
3208
3209void Context::detachRenderbuffer(GLuint renderbuffer)
3210{
3211 // [OpenGL ES 2.0.24] section 4.4 page 109:
3212 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3213 // had been executed with the target RENDERBUFFER and name of zero.
3214
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003215 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003216 {
3217 bindRenderbuffer(0);
3218 }
3219
3220 // [OpenGL ES 2.0.24] section 4.4 page 111:
3221 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3222 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3223 // point to which this image was attached in the currently bound framebuffer.
3224
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003225 Framebuffer *readFramebuffer = getReadFramebuffer();
3226 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003227
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003228 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003229 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003230 readFramebuffer->detachRenderbuffer(renderbuffer);
3231 }
3232
3233 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3234 {
3235 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003236 }
3237}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003238
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003239Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003240{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003241 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003242
3243 if (t == NULL)
3244 {
3245 static const GLubyte color[] = { 0, 0, 0, 255 };
3246
3247 switch (type)
3248 {
3249 default:
3250 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003251 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003252
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003253 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003254 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003255 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003256 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003257 t = incomplete2d;
3258 }
3259 break;
3260
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003261 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003262 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003263 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003264
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003265 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3266 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3267 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3268 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3269 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3270 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003271
3272 t = incompleteCube;
3273 }
3274 break;
3275 }
3276
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003277 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003278 }
3279
3280 return t;
3281}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003282
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003283bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003284{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003285 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003286}
3287
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003288bool Context::isTriangleMode(GLenum drawMode)
3289{
3290 switch (drawMode)
3291 {
3292 case GL_TRIANGLES:
3293 case GL_TRIANGLE_FAN:
3294 case GL_TRIANGLE_STRIP:
3295 return true;
3296 case GL_POINTS:
3297 case GL_LINES:
3298 case GL_LINE_LOOP:
3299 case GL_LINE_STRIP:
3300 return false;
3301 default: UNREACHABLE();
3302 }
3303
3304 return false;
3305}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003306
3307void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3308{
3309 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3310
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003311 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3312 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3313 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3314 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003315
daniel@transgaming.com83921382011-01-08 05:46:00 +00003316 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003317}
3318
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003319void Context::initExtensionString()
3320{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003321 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003322 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3323 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003324 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003325 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003326 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003327
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003328 if (supportsEventQueries())
3329 {
3330 mExtensionString += "GL_NV_fence ";
3331 }
3332
daniel@transgaming.com01868132010-08-24 19:21:17 +00003333 if (supportsCompressedTextures())
3334 {
3335 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3336 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003337
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003338 if (supportsFloatTextures())
3339 {
3340 mExtensionString += "GL_OES_texture_float ";
3341 }
3342
3343 if (supportsHalfFloatTextures())
3344 {
3345 mExtensionString += "GL_OES_texture_half_float ";
3346 }
3347
3348 if (supportsFloatLinearFilter())
3349 {
3350 mExtensionString += "GL_OES_texture_float_linear ";
3351 }
3352
3353 if (supportsHalfFloatLinearFilter())
3354 {
3355 mExtensionString += "GL_OES_texture_half_float_linear ";
3356 }
3357
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003358 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003359 {
3360 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3361 }
3362
daniel@transgaming.com83921382011-01-08 05:46:00 +00003363 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003364 {
3365 mExtensionString += "GL_OES_element_index_uint ";
3366 }
3367
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003368 if (supportsNonPower2Texture())
3369 {
3370 mExtensionString += "GL_OES_texture_npot ";
3371 }
3372
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003373 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3374 if (end != std::string::npos)
3375 {
3376 mExtensionString.resize(end+1);
3377 }
3378}
3379
3380const char *Context::getExtensionString() const
3381{
3382 return mExtensionString.c_str();
3383}
3384
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003385void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3386 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3387 GLbitfield mask)
3388{
3389 IDirect3DDevice9 *device = getDevice();
3390
3391 Framebuffer *readFramebuffer = getReadFramebuffer();
3392 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3393
3394 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3395 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3396 {
3397 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3398 }
3399
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003400 if (drawFramebuffer->getSamples() != 0)
3401 {
3402 return error(GL_INVALID_OPERATION);
3403 }
3404
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003405 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3406 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3407 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3408 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3409
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003410 RECT sourceRect;
3411 RECT destRect;
3412
3413 if (srcX0 < srcX1)
3414 {
3415 sourceRect.left = srcX0;
3416 sourceRect.right = srcX1;
3417 destRect.left = dstX0;
3418 destRect.right = dstX1;
3419 }
3420 else
3421 {
3422 sourceRect.left = srcX1;
3423 destRect.left = dstX1;
3424 sourceRect.right = srcX0;
3425 destRect.right = dstX0;
3426 }
3427
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003428 if (srcY0 < srcY1)
3429 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003430 sourceRect.top = readBufferHeight - srcY1;
3431 destRect.top = drawBufferHeight - dstY1;
3432 sourceRect.bottom = readBufferHeight - srcY0;
3433 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003434 }
3435 else
3436 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003437 sourceRect.top = readBufferHeight - srcY0;
3438 destRect.top = drawBufferHeight - dstY0;
3439 sourceRect.bottom = readBufferHeight - srcY1;
3440 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003441 }
3442
3443 RECT sourceScissoredRect = sourceRect;
3444 RECT destScissoredRect = destRect;
3445
3446 if (mState.scissorTest)
3447 {
3448 // Only write to parts of the destination framebuffer which pass the scissor test
3449 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3450 // rect will be checked against scissorY, rather than the bottom.
3451 if (destRect.left < mState.scissorX)
3452 {
3453 int xDiff = mState.scissorX - destRect.left;
3454 destScissoredRect.left = mState.scissorX;
3455 sourceScissoredRect.left += xDiff;
3456 }
3457
3458 if (destRect.right > mState.scissorX + mState.scissorWidth)
3459 {
3460 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3461 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3462 sourceScissoredRect.right -= xDiff;
3463 }
3464
3465 if (destRect.top < mState.scissorY)
3466 {
3467 int yDiff = mState.scissorY - destRect.top;
3468 destScissoredRect.top = mState.scissorY;
3469 sourceScissoredRect.top += yDiff;
3470 }
3471
3472 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3473 {
3474 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3475 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3476 sourceScissoredRect.bottom -= yDiff;
3477 }
3478 }
3479
3480 bool blitRenderTarget = false;
3481 bool blitDepthStencil = false;
3482
3483 RECT sourceTrimmedRect = sourceScissoredRect;
3484 RECT destTrimmedRect = destScissoredRect;
3485
3486 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3487 // the actual draw and read surfaces.
3488 if (sourceTrimmedRect.left < 0)
3489 {
3490 int xDiff = 0 - sourceTrimmedRect.left;
3491 sourceTrimmedRect.left = 0;
3492 destTrimmedRect.left += xDiff;
3493 }
3494
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003495 if (sourceTrimmedRect.right > readBufferWidth)
3496 {
3497 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3498 sourceTrimmedRect.right = readBufferWidth;
3499 destTrimmedRect.right -= xDiff;
3500 }
3501
3502 if (sourceTrimmedRect.top < 0)
3503 {
3504 int yDiff = 0 - sourceTrimmedRect.top;
3505 sourceTrimmedRect.top = 0;
3506 destTrimmedRect.top += yDiff;
3507 }
3508
3509 if (sourceTrimmedRect.bottom > readBufferHeight)
3510 {
3511 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3512 sourceTrimmedRect.bottom = readBufferHeight;
3513 destTrimmedRect.bottom -= yDiff;
3514 }
3515
3516 if (destTrimmedRect.left < 0)
3517 {
3518 int xDiff = 0 - destTrimmedRect.left;
3519 destTrimmedRect.left = 0;
3520 sourceTrimmedRect.left += xDiff;
3521 }
3522
3523 if (destTrimmedRect.right > drawBufferWidth)
3524 {
3525 int xDiff = destTrimmedRect.right - drawBufferWidth;
3526 destTrimmedRect.right = drawBufferWidth;
3527 sourceTrimmedRect.right -= xDiff;
3528 }
3529
3530 if (destTrimmedRect.top < 0)
3531 {
3532 int yDiff = 0 - destTrimmedRect.top;
3533 destTrimmedRect.top = 0;
3534 sourceTrimmedRect.top += yDiff;
3535 }
3536
3537 if (destTrimmedRect.bottom > drawBufferHeight)
3538 {
3539 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3540 destTrimmedRect.bottom = drawBufferHeight;
3541 sourceTrimmedRect.bottom -= yDiff;
3542 }
3543
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003544 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003545 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3546 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3547 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3548 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003549 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3550 {
3551 partialBufferCopy = true;
3552 }
3553
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003554 if (mask & GL_COLOR_BUFFER_BIT)
3555 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003556 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3557 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3558 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3559 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3560 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003561 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3562 {
3563 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3564 return error(GL_INVALID_OPERATION);
3565 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003566
3567 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3568 {
3569 return error(GL_INVALID_OPERATION);
3570 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003571
3572 blitRenderTarget = true;
3573
3574 }
3575
3576 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3577 {
3578 DepthStencilbuffer *readDSBuffer = NULL;
3579 DepthStencilbuffer *drawDSBuffer = NULL;
3580
3581 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3582 // both a depth and stencil buffer, it will be the same buffer.
3583
3584 if (mask & GL_DEPTH_BUFFER_BIT)
3585 {
3586 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3587 {
3588 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3589 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3590 {
3591 return error(GL_INVALID_OPERATION);
3592 }
3593
3594 blitDepthStencil = true;
3595 readDSBuffer = readFramebuffer->getDepthbuffer();
3596 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3597 }
3598 }
3599
3600 if (mask & GL_STENCIL_BUFFER_BIT)
3601 {
3602 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3603 {
3604 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3605 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3606 {
3607 return error(GL_INVALID_OPERATION);
3608 }
3609
3610 blitDepthStencil = true;
3611 readDSBuffer = readFramebuffer->getStencilbuffer();
3612 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3613 }
3614 }
3615
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003616 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003617 {
3618 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3619 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3620 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003621
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003622 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3623 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003624 {
3625 return error(GL_INVALID_OPERATION);
3626 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003627 }
3628
3629 if (blitRenderTarget || blitDepthStencil)
3630 {
3631 egl::Display *display = getDisplay();
3632 display->endScene();
3633
3634 if (blitRenderTarget)
3635 {
3636 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3637 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3638
3639 if (FAILED(result))
3640 {
3641 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3642 return;
3643 }
3644 }
3645
3646 if (blitDepthStencil)
3647 {
3648 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3649
3650 if (FAILED(result))
3651 {
3652 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3653 return;
3654 }
3655 }
3656 }
3657}
3658
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003659VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3660{
3661 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3662 {
3663 mVertexDeclCache[i].vertexDeclaration = NULL;
3664 mVertexDeclCache[i].lruCount = 0;
3665 }
3666}
3667
3668VertexDeclarationCache::~VertexDeclarationCache()
3669{
3670 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3671 {
3672 if (mVertexDeclCache[i].vertexDeclaration)
3673 {
3674 mVertexDeclCache[i].vertexDeclaration->Release();
3675 }
3676 }
3677}
3678
3679GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3680{
3681 IDirect3DDevice9 *device = getDevice();
3682
3683 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3684 D3DVERTEXELEMENT9 *element = &elements[0];
3685
3686 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3687 {
3688 if (attributes[i].active)
3689 {
3690 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3691
3692 element->Stream = i;
3693 element->Offset = 0;
3694 element->Type = attributes[i].type;
3695 element->Method = D3DDECLMETHOD_DEFAULT;
3696 element->Usage = D3DDECLUSAGE_TEXCOORD;
3697 element->UsageIndex = program->getSemanticIndex(i);
3698 element++;
3699 }
3700 }
3701
3702 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3703 *(element++) = end;
3704
3705 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3706 {
3707 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3708 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3709 {
3710 entry->lruCount = ++mMaxLru;
3711 device->SetVertexDeclaration(entry->vertexDeclaration);
3712
3713 return GL_NO_ERROR;
3714 }
3715 }
3716
3717 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3718
3719 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3720 {
3721 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3722 {
3723 lastCache = &mVertexDeclCache[i];
3724 }
3725 }
3726
3727 if (lastCache->vertexDeclaration != NULL)
3728 {
3729 lastCache->vertexDeclaration->Release();
3730 lastCache->vertexDeclaration = NULL;
3731 }
3732
3733 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3734 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3735 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3736 lastCache->lruCount = ++mMaxLru;
3737
3738 return GL_NO_ERROR;
3739}
3740
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003741}
3742
3743extern "C"
3744{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003745gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003746{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003747 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003748}
3749
3750void glDestroyContext(gl::Context *context)
3751{
3752 delete context;
3753
3754 if (context == gl::getContext())
3755 {
3756 gl::makeCurrent(NULL, NULL, NULL);
3757 }
3758}
3759
3760void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3761{
3762 gl::makeCurrent(context, display, surface);
3763}
3764
3765gl::Context *glGetCurrentContext()
3766{
3767 return gl::getContext();
3768}
3769}