blob: 1c6645072bbc1c435a2ba3b3ca40b83e9035e0ac [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000028#include "libGLESv2/VertexDataManager.h"
29#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com86487c22010-03-11 19:41:43 +000031#undef near
32#undef far
33
jbauman@chromium.org399c35f2011-04-28 23:19:51 +000034namespace
35{
36 enum { CLOSING_INDEX_BUFFER_SIZE = 4096 };
37}
38
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039namespace gl
40{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +000041Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042{
benvanik@google.com1a233342011-04-28 19:44:39 +000043 mFenceHandleAllocator.setBaseHandle(0);
44
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000046
daniel@transgaming.com428d1582010-05-04 03:35:25 +000047 mState.depthClearValue = 1.0f;
48 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049
daniel@transgaming.com428d1582010-05-04 03:35:25 +000050 mState.cullFace = false;
51 mState.cullMode = GL_BACK;
52 mState.frontFace = GL_CCW;
53 mState.depthTest = false;
54 mState.depthFunc = GL_LESS;
55 mState.blend = false;
56 mState.sourceBlendRGB = GL_ONE;
57 mState.sourceBlendAlpha = GL_ONE;
58 mState.destBlendRGB = GL_ZERO;
59 mState.destBlendAlpha = GL_ZERO;
60 mState.blendEquationRGB = GL_FUNC_ADD;
61 mState.blendEquationAlpha = GL_FUNC_ADD;
62 mState.blendColor.red = 0;
63 mState.blendColor.green = 0;
64 mState.blendColor.blue = 0;
65 mState.blendColor.alpha = 0;
66 mState.stencilTest = false;
67 mState.stencilFunc = GL_ALWAYS;
68 mState.stencilRef = 0;
69 mState.stencilMask = -1;
70 mState.stencilWritemask = -1;
71 mState.stencilBackFunc = GL_ALWAYS;
72 mState.stencilBackRef = 0;
73 mState.stencilBackMask = - 1;
74 mState.stencilBackWritemask = -1;
75 mState.stencilFail = GL_KEEP;
76 mState.stencilPassDepthFail = GL_KEEP;
77 mState.stencilPassDepthPass = GL_KEEP;
78 mState.stencilBackFail = GL_KEEP;
79 mState.stencilBackPassDepthFail = GL_KEEP;
80 mState.stencilBackPassDepthPass = GL_KEEP;
81 mState.polygonOffsetFill = false;
82 mState.polygonOffsetFactor = 0.0f;
83 mState.polygonOffsetUnits = 0.0f;
84 mState.sampleAlphaToCoverage = false;
85 mState.sampleCoverage = false;
86 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000087 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.scissorTest = false;
89 mState.dither = true;
90 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000091 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092
daniel@transgaming.com428d1582010-05-04 03:35:25 +000093 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000094
daniel@transgaming.com428d1582010-05-04 03:35:25 +000095 mState.viewportX = 0;
96 mState.viewportY = 0;
97 mState.viewportWidth = config->mDisplayMode.Width;
98 mState.viewportHeight = config->mDisplayMode.Height;
99 mState.zNear = 0.0f;
100 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000102 mState.scissorX = 0;
103 mState.scissorY = 0;
104 mState.scissorWidth = config->mDisplayMode.Width;
105 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000107 mState.colorMaskRed = true;
108 mState.colorMaskGreen = true;
109 mState.colorMaskBlue = true;
110 mState.colorMaskAlpha = true;
111 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000113 if (shareContext != NULL)
114 {
115 mResourceManager = shareContext->mResourceManager;
116 mResourceManager->addRef();
117 }
118 else
119 {
120 mResourceManager = new ResourceManager();
121 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 // [OpenGL ES 2.0.24] section 3.7 page 83:
124 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
125 // and cube map texture state vectors respectively associated with them.
126 // In order that access to these initial textures not be lost, they are treated as texture
127 // objects all of whose names are 0.
128
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000129 mTexture2DZero.set(new Texture2D(0));
130 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000132 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000133 bindArrayBuffer(0);
134 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindTextureCubeMap(0);
136 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000137 bindReadFramebuffer(0);
138 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 bindRenderbuffer(0);
140
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000141 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.packAlignment = 4;
144 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000145
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000147 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000148 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000149 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000150
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 mInvalidEnum = false;
152 mInvalidValue = false;
153 mInvalidOperation = false;
154 mOutOfMemory = false;
155 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000156
157 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000158
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000159 mSupportsCompressedTextures = false;
160 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000161 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000162 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000163 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
166Context::~Context()
167{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000168 if (mState.currentProgram != 0)
169 {
170 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
171 if (programObject)
172 {
173 programObject->release();
174 }
175 mState.currentProgram = 0;
176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000178 while (!mFramebufferMap.empty())
179 {
180 deleteFramebuffer(mFramebufferMap.begin()->first);
181 }
182
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000183 while (!mFenceMap.empty())
184 {
185 deleteFence(mFenceMap.begin()->first);
186 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000187
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000188 while (!mMultiSampleSupport.empty())
189 {
190 delete [] mMultiSampleSupport.begin()->second;
191 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
192 }
193
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000194 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000195 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000196 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000197 {
198 mState.samplerTexture[type][sampler].set(NULL);
199 }
200 }
201
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000202 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000203 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000204 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000205 }
206
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000207 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
208 {
209 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
210 }
211
212 mState.arrayBuffer.set(NULL);
213 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000214 mState.renderbuffer.set(NULL);
215
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000216 mTexture2DZero.set(NULL);
217 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000219 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000220 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000221 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000222 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000223
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000224 if (mMaskedClearSavedState)
225 {
226 mMaskedClearSavedState->Release();
227 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000228
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000229 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230}
231
232void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
233{
234 IDirect3DDevice9 *device = display->getDevice();
235
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000236 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000237 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000238 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000239
daniel@transgaming.com83921382011-01-08 05:46:00 +0000240 mVertexDataManager = new VertexDataManager(this, device);
241 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000242 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000243
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000244 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +0000245 mSupportsVertexTexture = display->getVertexTextureSupport();
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000246 mSupportsNonPower2Texture = display->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000247
248 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
249 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
250 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
251 mMaxRenderbufferDimension = mMaxTextureDimension;
252 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
253 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
254 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
255
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000256 const D3DFORMAT renderBufferFormats[] =
257 {
258 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000259 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000260 D3DFMT_R5G6B5,
261 D3DFMT_D24S8
262 };
263
264 int max = 0;
265 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
266 {
267 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
268 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
269 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
270
271 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
272 {
273 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
274 {
275 max = j;
276 }
277 }
278 }
279
280 mMaxSupportedSamples = max;
281
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000282 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000283 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000284 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
285 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000286 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
287 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000288
daniel@transgaming.com83921382011-01-08 05:46:00 +0000289 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
290
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000291 initExtensionString();
292
293 mState.viewportX = 0;
294 mState.viewportY = 0;
295 mState.viewportWidth = surface->getWidth();
296 mState.viewportHeight = surface->getHeight();
297
298 mState.scissorX = 0;
299 mState.scissorY = 0;
300 mState.scissorWidth = surface->getWidth();
301 mState.scissorHeight = surface->getHeight();
302
303 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000304 }
305
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
307 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000308 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000311 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000312 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
314 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000316 if (defaultRenderTarget)
317 {
318 defaultRenderTarget->Release();
319 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000321 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000323 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000325
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000326 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327}
328
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000329// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000330void Context::markAllStateDirty()
331{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000332 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
333 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000334 mAppliedTextureSerialPS[t] = 0;
335 }
336
337 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
338 {
339 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000340 }
341
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000342 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000343 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000344 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000345 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000346 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000347
348 mClearStateDirty = true;
349 mCullStateDirty = true;
350 mDepthStateDirty = true;
351 mMaskStateDirty = true;
352 mBlendStateDirty = true;
353 mStencilStateDirty = true;
354 mPolygonOffsetStateDirty = true;
355 mScissorStateDirty = true;
356 mSampleStateDirty = true;
357 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000358 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000359}
360
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361void Context::setClearColor(float red, float green, float blue, float alpha)
362{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000363 mState.colorClearValue.red = red;
364 mState.colorClearValue.green = green;
365 mState.colorClearValue.blue = blue;
366 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367}
368
369void Context::setClearDepth(float depth)
370{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000371 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372}
373
374void Context::setClearStencil(int stencil)
375{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000376 mState.stencilClearValue = stencil;
377}
378
379void Context::setCullFace(bool enabled)
380{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000381 if (mState.cullFace != enabled)
382 {
383 mState.cullFace = enabled;
384 mCullStateDirty = true;
385 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000386}
387
388bool Context::isCullFaceEnabled() const
389{
390 return mState.cullFace;
391}
392
393void Context::setCullMode(GLenum mode)
394{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000395 if (mState.cullMode != mode)
396 {
397 mState.cullMode = mode;
398 mCullStateDirty = true;
399 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000400}
401
402void Context::setFrontFace(GLenum front)
403{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000404 if (mState.frontFace != front)
405 {
406 mState.frontFace = front;
407 mFrontFaceDirty = true;
408 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000409}
410
411void Context::setDepthTest(bool enabled)
412{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000413 if (mState.depthTest != enabled)
414 {
415 mState.depthTest = enabled;
416 mDepthStateDirty = true;
417 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000418}
419
420bool Context::isDepthTestEnabled() const
421{
422 return mState.depthTest;
423}
424
425void Context::setDepthFunc(GLenum depthFunc)
426{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000427 if (mState.depthFunc != depthFunc)
428 {
429 mState.depthFunc = depthFunc;
430 mDepthStateDirty = true;
431 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000432}
433
434void Context::setDepthRange(float zNear, float zFar)
435{
436 mState.zNear = zNear;
437 mState.zFar = zFar;
438}
439
440void Context::setBlend(bool enabled)
441{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000442 if (mState.blend != enabled)
443 {
444 mState.blend = enabled;
445 mBlendStateDirty = true;
446 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000447}
448
449bool Context::isBlendEnabled() const
450{
451 return mState.blend;
452}
453
454void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
455{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000456 if (mState.sourceBlendRGB != sourceRGB ||
457 mState.sourceBlendAlpha != sourceAlpha ||
458 mState.destBlendRGB != destRGB ||
459 mState.destBlendAlpha != destAlpha)
460 {
461 mState.sourceBlendRGB = sourceRGB;
462 mState.destBlendRGB = destRGB;
463 mState.sourceBlendAlpha = sourceAlpha;
464 mState.destBlendAlpha = destAlpha;
465 mBlendStateDirty = true;
466 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000467}
468
469void Context::setBlendColor(float red, float green, float blue, float alpha)
470{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000471 if (mState.blendColor.red != red ||
472 mState.blendColor.green != green ||
473 mState.blendColor.blue != blue ||
474 mState.blendColor.alpha != alpha)
475 {
476 mState.blendColor.red = red;
477 mState.blendColor.green = green;
478 mState.blendColor.blue = blue;
479 mState.blendColor.alpha = alpha;
480 mBlendStateDirty = true;
481 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000482}
483
484void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
485{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000486 if (mState.blendEquationRGB != rgbEquation ||
487 mState.blendEquationAlpha != alphaEquation)
488 {
489 mState.blendEquationRGB = rgbEquation;
490 mState.blendEquationAlpha = alphaEquation;
491 mBlendStateDirty = true;
492 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000493}
494
495void Context::setStencilTest(bool enabled)
496{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 if (mState.stencilTest != enabled)
498 {
499 mState.stencilTest = enabled;
500 mStencilStateDirty = true;
501 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000502}
503
504bool Context::isStencilTestEnabled() const
505{
506 return mState.stencilTest;
507}
508
509void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
510{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000511 if (mState.stencilFunc != stencilFunc ||
512 mState.stencilRef != stencilRef ||
513 mState.stencilMask != stencilMask)
514 {
515 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000516 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 mState.stencilMask = stencilMask;
518 mStencilStateDirty = true;
519 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000520}
521
522void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
523{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000524 if (mState.stencilBackFunc != stencilBackFunc ||
525 mState.stencilBackRef != stencilBackRef ||
526 mState.stencilBackMask != stencilBackMask)
527 {
528 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000529 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000530 mState.stencilBackMask = stencilBackMask;
531 mStencilStateDirty = true;
532 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000533}
534
535void Context::setStencilWritemask(GLuint stencilWritemask)
536{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000537 if (mState.stencilWritemask != stencilWritemask)
538 {
539 mState.stencilWritemask = stencilWritemask;
540 mStencilStateDirty = true;
541 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000542}
543
544void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
545{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000546 if (mState.stencilBackWritemask != stencilBackWritemask)
547 {
548 mState.stencilBackWritemask = stencilBackWritemask;
549 mStencilStateDirty = true;
550 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000551}
552
553void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
554{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000555 if (mState.stencilFail != stencilFail ||
556 mState.stencilPassDepthFail != stencilPassDepthFail ||
557 mState.stencilPassDepthPass != stencilPassDepthPass)
558 {
559 mState.stencilFail = stencilFail;
560 mState.stencilPassDepthFail = stencilPassDepthFail;
561 mState.stencilPassDepthPass = stencilPassDepthPass;
562 mStencilStateDirty = true;
563 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000564}
565
566void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
567{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000568 if (mState.stencilBackFail != stencilBackFail ||
569 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
570 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
571 {
572 mState.stencilBackFail = stencilBackFail;
573 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
574 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
575 mStencilStateDirty = true;
576 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000577}
578
579void Context::setPolygonOffsetFill(bool enabled)
580{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000581 if (mState.polygonOffsetFill != enabled)
582 {
583 mState.polygonOffsetFill = enabled;
584 mPolygonOffsetStateDirty = true;
585 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000586}
587
588bool Context::isPolygonOffsetFillEnabled() const
589{
590 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000591
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000592}
593
594void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
595{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000596 if (mState.polygonOffsetFactor != factor ||
597 mState.polygonOffsetUnits != units)
598 {
599 mState.polygonOffsetFactor = factor;
600 mState.polygonOffsetUnits = units;
601 mPolygonOffsetStateDirty = true;
602 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000603}
604
605void Context::setSampleAlphaToCoverage(bool enabled)
606{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000607 if (mState.sampleAlphaToCoverage != enabled)
608 {
609 mState.sampleAlphaToCoverage = enabled;
610 mSampleStateDirty = true;
611 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000612}
613
614bool Context::isSampleAlphaToCoverageEnabled() const
615{
616 return mState.sampleAlphaToCoverage;
617}
618
619void Context::setSampleCoverage(bool enabled)
620{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000621 if (mState.sampleCoverage != enabled)
622 {
623 mState.sampleCoverage = enabled;
624 mSampleStateDirty = true;
625 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000626}
627
628bool Context::isSampleCoverageEnabled() const
629{
630 return mState.sampleCoverage;
631}
632
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000633void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000634{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000635 if (mState.sampleCoverageValue != value ||
636 mState.sampleCoverageInvert != invert)
637 {
638 mState.sampleCoverageValue = value;
639 mState.sampleCoverageInvert = invert;
640 mSampleStateDirty = true;
641 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000642}
643
644void Context::setScissorTest(bool enabled)
645{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000646 if (mState.scissorTest != enabled)
647 {
648 mState.scissorTest = enabled;
649 mScissorStateDirty = true;
650 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000651}
652
653bool Context::isScissorTestEnabled() const
654{
655 return mState.scissorTest;
656}
657
658void Context::setDither(bool enabled)
659{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000660 if (mState.dither != enabled)
661 {
662 mState.dither = enabled;
663 mDitherStateDirty = true;
664 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000665}
666
667bool Context::isDitherEnabled() const
668{
669 return mState.dither;
670}
671
672void Context::setLineWidth(GLfloat width)
673{
674 mState.lineWidth = width;
675}
676
677void Context::setGenerateMipmapHint(GLenum hint)
678{
679 mState.generateMipmapHint = hint;
680}
681
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000682void Context::setFragmentShaderDerivativeHint(GLenum hint)
683{
684 mState.fragmentShaderDerivativeHint = hint;
685 // TODO: Propagate the hint to shader translator so we can write
686 // ddx, ddx_coarse, or ddx_fine depending on the hint.
687 // Ignore for now. It is valid for implementations to ignore hint.
688}
689
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000690void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
691{
692 mState.viewportX = x;
693 mState.viewportY = y;
694 mState.viewportWidth = width;
695 mState.viewportHeight = height;
696}
697
698void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
699{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000700 if (mState.scissorX != x || mState.scissorY != y ||
701 mState.scissorWidth != width || mState.scissorHeight != height)
702 {
703 mState.scissorX = x;
704 mState.scissorY = y;
705 mState.scissorWidth = width;
706 mState.scissorHeight = height;
707 mScissorStateDirty = true;
708 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000709}
710
711void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
712{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000713 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
714 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
715 {
716 mState.colorMaskRed = red;
717 mState.colorMaskGreen = green;
718 mState.colorMaskBlue = blue;
719 mState.colorMaskAlpha = alpha;
720 mMaskStateDirty = true;
721 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000722}
723
724void Context::setDepthMask(bool mask)
725{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000726 if (mState.depthMask != mask)
727 {
728 mState.depthMask = mask;
729 mMaskStateDirty = true;
730 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000731}
732
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000733void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734{
735 mState.activeSampler = active;
736}
737
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000738GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000740 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000741}
742
743GLuint Context::getDrawFramebufferHandle() const
744{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000745 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000746}
747
748GLuint Context::getRenderbufferHandle() const
749{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000750 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000751}
752
753GLuint Context::getArrayBufferHandle() const
754{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000755 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000756}
757
daniel@transgaming.com83921382011-01-08 05:46:00 +0000758void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000759{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000760 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000761}
762
daniel@transgaming.com83921382011-01-08 05:46:00 +0000763const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000764{
765 return mState.vertexAttribute[attribNum];
766}
767
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000768void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000769 GLsizei stride, const void *pointer)
770{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000771 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000772 mState.vertexAttribute[attribNum].mSize = size;
773 mState.vertexAttribute[attribNum].mType = type;
774 mState.vertexAttribute[attribNum].mNormalized = normalized;
775 mState.vertexAttribute[attribNum].mStride = stride;
776 mState.vertexAttribute[attribNum].mPointer = pointer;
777}
778
779const void *Context::getVertexAttribPointer(unsigned int attribNum) const
780{
781 return mState.vertexAttribute[attribNum].mPointer;
782}
783
daniel@transgaming.com83921382011-01-08 05:46:00 +0000784const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000785{
786 return mState.vertexAttribute;
787}
788
789void Context::setPackAlignment(GLint alignment)
790{
791 mState.packAlignment = alignment;
792}
793
794GLint Context::getPackAlignment() const
795{
796 return mState.packAlignment;
797}
798
799void Context::setUnpackAlignment(GLint alignment)
800{
801 mState.unpackAlignment = alignment;
802}
803
804GLint Context::getUnpackAlignment() const
805{
806 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807}
808
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809GLuint Context::createBuffer()
810{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000811 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814GLuint Context::createProgram()
815{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000816 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817}
818
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000819GLuint Context::createShader(GLenum type)
820{
821 return mResourceManager->createShader(type);
822}
823
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824GLuint Context::createTexture()
825{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000826 return mResourceManager->createTexture();
827}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000829GLuint Context::createRenderbuffer()
830{
831 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000834// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835GLuint Context::createFramebuffer()
836{
benvanik@google.com1a233342011-04-28 19:44:39 +0000837 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000838
839 mFramebufferMap[handle] = NULL;
840
841 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842}
843
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000844GLuint Context::createFence()
845{
benvanik@google.com1a233342011-04-28 19:44:39 +0000846 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000847
848 mFenceMap[handle] = new Fence;
849
850 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000851}
852
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853void Context::deleteBuffer(GLuint buffer)
854{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000855 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856 {
857 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000859
860 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
863void Context::deleteShader(GLuint shader)
864{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000865 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866}
867
868void Context::deleteProgram(GLuint program)
869{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000870 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871}
872
873void Context::deleteTexture(GLuint texture)
874{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000875 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876 {
877 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000879
880 mResourceManager->deleteTexture(texture);
881}
882
883void Context::deleteRenderbuffer(GLuint renderbuffer)
884{
885 if (mResourceManager->getRenderbuffer(renderbuffer))
886 {
887 detachRenderbuffer(renderbuffer);
888 }
889
890 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
893void Context::deleteFramebuffer(GLuint framebuffer)
894{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000895 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
896
897 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898 {
899 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000900
benvanik@google.com1a233342011-04-28 19:44:39 +0000901 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000902 delete framebufferObject->second;
903 mFramebufferMap.erase(framebufferObject);
904 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000906
907void Context::deleteFence(GLuint fence)
908{
909 FenceMap::iterator fenceObject = mFenceMap.find(fence);
910
911 if (fenceObject != mFenceMap.end())
912 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000913 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000914 delete fenceObject->second;
915 mFenceMap.erase(fenceObject);
916 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000917}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000919Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000921 return mResourceManager->getBuffer(handle);
922}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000924Shader *Context::getShader(GLuint handle)
925{
926 return mResourceManager->getShader(handle);
927}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000929Program *Context::getProgram(GLuint handle)
930{
931 return mResourceManager->getProgram(handle);
932}
933
934Texture *Context::getTexture(GLuint handle)
935{
936 return mResourceManager->getTexture(handle);
937}
938
939Renderbuffer *Context::getRenderbuffer(GLuint handle)
940{
941 return mResourceManager->getRenderbuffer(handle);
942}
943
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000944Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000945{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000946 return getFramebuffer(mState.readFramebuffer);
947}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000948
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000949Framebuffer *Context::getDrawFramebuffer()
950{
951 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
954void Context::bindArrayBuffer(unsigned int buffer)
955{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000956 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000958 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959}
960
961void Context::bindElementArrayBuffer(unsigned int buffer)
962{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000963 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000965 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
968void Context::bindTexture2D(GLuint texture)
969{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000970 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000972 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
975void Context::bindTextureCubeMap(GLuint texture)
976{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000977 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000979 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980}
981
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000982void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000984 if (!getFramebuffer(framebuffer))
985 {
986 mFramebufferMap[framebuffer] = new Framebuffer();
987 }
988
989 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000990}
991
992void Context::bindDrawFramebuffer(GLuint framebuffer)
993{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000994 if (!getFramebuffer(framebuffer))
995 {
996 mFramebufferMap[framebuffer] = new Framebuffer();
997 }
998
999 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000}
1001
1002void Context::bindRenderbuffer(GLuint renderbuffer)
1003{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001004 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1005
1006 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007}
1008
1009void Context::useProgram(GLuint program)
1010{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001011 GLuint priorProgram = mState.currentProgram;
1012 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001013
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001014 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001016 Program *newProgram = mResourceManager->getProgram(program);
1017 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1018
1019 if (newProgram)
1020 {
1021 newProgram->addRef();
1022 }
1023
1024 if (oldProgram)
1025 {
1026 oldProgram->release();
1027 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
1031void Context::setFramebufferZero(Framebuffer *buffer)
1032{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001033 delete mFramebufferMap[0];
1034 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035}
1036
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001037void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001039 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1040 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041}
1042
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001043Framebuffer *Context::getFramebuffer(unsigned int handle)
1044{
1045 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1046
1047 if (framebuffer == mFramebufferMap.end())
1048 {
1049 return NULL;
1050 }
1051 else
1052 {
1053 return framebuffer->second;
1054 }
1055}
1056
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001057Fence *Context::getFence(unsigned int handle)
1058{
1059 FenceMap::iterator fence = mFenceMap.find(handle);
1060
1061 if (fence == mFenceMap.end())
1062 {
1063 return NULL;
1064 }
1065 else
1066 {
1067 return fence->second;
1068 }
1069}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001070
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071Buffer *Context::getArrayBuffer()
1072{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001073 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074}
1075
1076Buffer *Context::getElementArrayBuffer()
1077{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001078 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079}
1080
1081Program *Context::getCurrentProgram()
1082{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001083 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084}
1085
1086Texture2D *Context::getTexture2D()
1087{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001088 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089}
1090
1091TextureCubeMap *Context::getTextureCubeMap()
1092{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001093 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001096Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001098 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001099
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001100 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001101 {
1102 switch (type)
1103 {
1104 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001105 case TEXTURE_2D: return mTexture2DZero.get();
1106 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001107 }
1108 }
1109
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001110 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111}
1112
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001113bool Context::getBooleanv(GLenum pname, GLboolean *params)
1114{
1115 switch (pname)
1116 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001117 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1118 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1119 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001120 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001121 params[0] = mState.colorMaskRed;
1122 params[1] = mState.colorMaskGreen;
1123 params[2] = mState.colorMaskBlue;
1124 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001125 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001126 case GL_CULL_FACE: *params = mState.cullFace; break;
1127 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1128 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1129 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1130 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1131 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1132 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1133 case GL_BLEND: *params = mState.blend; break;
1134 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001135 default:
1136 return false;
1137 }
1138
1139 return true;
1140}
1141
1142bool Context::getFloatv(GLenum pname, GLfloat *params)
1143{
1144 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1145 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1146 // GetIntegerv as its native query function. As it would require conversion in any
1147 // case, this should make no difference to the calling application.
1148 switch (pname)
1149 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001150 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1151 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1152 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1153 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1154 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001155 case GL_ALIASED_LINE_WIDTH_RANGE:
1156 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1157 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1158 break;
1159 case GL_ALIASED_POINT_SIZE_RANGE:
1160 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001161 params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001162 break;
1163 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001164 params[0] = mState.zNear;
1165 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001166 break;
1167 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001168 params[0] = mState.colorClearValue.red;
1169 params[1] = mState.colorClearValue.green;
1170 params[2] = mState.colorClearValue.blue;
1171 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001172 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001173 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001174 params[0] = mState.blendColor.red;
1175 params[1] = mState.blendColor.green;
1176 params[2] = mState.blendColor.blue;
1177 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001178 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001179 default:
1180 return false;
1181 }
1182
1183 return true;
1184}
1185
1186bool Context::getIntegerv(GLenum pname, GLint *params)
1187{
1188 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1189 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1190 // GetIntegerv as its native query function. As it would require conversion in any
1191 // case, this should make no difference to the calling application. You may find it in
1192 // Context::getFloatv.
1193 switch (pname)
1194 {
1195 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1196 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001197 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001198 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1199 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001200 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001201 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001202 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001203 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001204 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001205 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1206 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001207 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1208 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1209 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001210 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001211 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1212 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1213 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1214 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001215 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001216 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1217 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1218 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1219 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1220 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1221 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1222 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1223 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1224 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1225 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1226 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1227 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1228 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1229 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1230 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1231 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1232 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1233 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1234 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1235 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1236 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1237 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1238 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1239 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001240 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1241 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001242 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1243 {
1244 if (supportsCompressedTextures())
1245 {
1246 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1247 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1248 *params = 2;
1249 }
1250 else
1251 {
1252 *params = 0;
1253 }
1254 }
1255 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001256 case GL_MAX_SAMPLES_ANGLE:
1257 {
1258 GLsizei maxSamples = getMaxSupportedSamples();
1259 if (maxSamples != 0)
1260 {
1261 *params = maxSamples;
1262 }
1263 else
1264 {
1265 return false;
1266 }
1267
1268 break;
1269 }
1270 case GL_SAMPLE_BUFFERS:
1271 case GL_SAMPLES:
1272 {
1273 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1274 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1275 {
1276 switch (pname)
1277 {
1278 case GL_SAMPLE_BUFFERS:
1279 if (framebuffer->getSamples() != 0)
1280 {
1281 *params = 1;
1282 }
1283 else
1284 {
1285 *params = 0;
1286 }
1287 break;
1288 case GL_SAMPLES:
1289 *params = framebuffer->getSamples();
1290 break;
1291 }
1292 }
1293 else
1294 {
1295 *params = 0;
1296 }
1297 }
1298 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001299 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1300 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1301 case GL_MAX_VIEWPORT_DIMS:
1302 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001303 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001304 params[0] = maxDimension;
1305 params[1] = maxDimension;
1306 }
1307 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001308 case GL_COMPRESSED_TEXTURE_FORMATS:
1309 {
1310 if (supportsCompressedTextures())
1311 {
1312 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1313 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1314 }
1315 }
1316 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001317 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001318 params[0] = mState.viewportX;
1319 params[1] = mState.viewportY;
1320 params[2] = mState.viewportWidth;
1321 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001322 break;
1323 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001324 params[0] = mState.scissorX;
1325 params[1] = mState.scissorY;
1326 params[2] = mState.scissorWidth;
1327 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001328 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001329 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1330 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001331 case GL_RED_BITS:
1332 case GL_GREEN_BITS:
1333 case GL_BLUE_BITS:
1334 case GL_ALPHA_BITS:
1335 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001336 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001337 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1338
1339 if (colorbuffer)
1340 {
1341 switch (pname)
1342 {
1343 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1344 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1345 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1346 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1347 }
1348 }
1349 else
1350 {
1351 *params = 0;
1352 }
1353 }
1354 break;
1355 case GL_DEPTH_BITS:
1356 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001357 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001358 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359
1360 if (depthbuffer)
1361 {
1362 *params = depthbuffer->getDepthSize();
1363 }
1364 else
1365 {
1366 *params = 0;
1367 }
1368 }
1369 break;
1370 case GL_STENCIL_BITS:
1371 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001372 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001373 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001374
1375 if (stencilbuffer)
1376 {
1377 *params = stencilbuffer->getStencilSize();
1378 }
1379 else
1380 {
1381 *params = 0;
1382 }
1383 }
1384 break;
1385 case GL_TEXTURE_BINDING_2D:
1386 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001387 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001388 {
1389 error(GL_INVALID_OPERATION);
1390 return false;
1391 }
1392
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001393 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001394 }
1395 break;
1396 case GL_TEXTURE_BINDING_CUBE_MAP:
1397 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001398 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001399 {
1400 error(GL_INVALID_OPERATION);
1401 return false;
1402 }
1403
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001404 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001405 }
1406 break;
1407 default:
1408 return false;
1409 }
1410
1411 return true;
1412}
1413
1414bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1415{
1416 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1417 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1418 // to the fact that it is stored internally as a float, and so would require conversion
1419 // if returned from Context::getIntegerv. Since this conversion is already implemented
1420 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1421 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1422 // application.
1423 switch (pname)
1424 {
1425 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1426 case GL_SHADER_BINARY_FORMATS:
1427 {
1428 *type = GL_INT;
1429 *numParams = 0;
1430 }
1431 break;
1432 case GL_MAX_VERTEX_ATTRIBS:
1433 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1434 case GL_MAX_VARYING_VECTORS:
1435 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1436 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1437 case GL_MAX_TEXTURE_IMAGE_UNITS:
1438 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1439 case GL_MAX_RENDERBUFFER_SIZE:
1440 case GL_NUM_SHADER_BINARY_FORMATS:
1441 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1442 case GL_ARRAY_BUFFER_BINDING:
1443 case GL_FRAMEBUFFER_BINDING:
1444 case GL_RENDERBUFFER_BINDING:
1445 case GL_CURRENT_PROGRAM:
1446 case GL_PACK_ALIGNMENT:
1447 case GL_UNPACK_ALIGNMENT:
1448 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001449 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001450 case GL_RED_BITS:
1451 case GL_GREEN_BITS:
1452 case GL_BLUE_BITS:
1453 case GL_ALPHA_BITS:
1454 case GL_DEPTH_BITS:
1455 case GL_STENCIL_BITS:
1456 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1457 case GL_CULL_FACE_MODE:
1458 case GL_FRONT_FACE:
1459 case GL_ACTIVE_TEXTURE:
1460 case GL_STENCIL_FUNC:
1461 case GL_STENCIL_VALUE_MASK:
1462 case GL_STENCIL_REF:
1463 case GL_STENCIL_FAIL:
1464 case GL_STENCIL_PASS_DEPTH_FAIL:
1465 case GL_STENCIL_PASS_DEPTH_PASS:
1466 case GL_STENCIL_BACK_FUNC:
1467 case GL_STENCIL_BACK_VALUE_MASK:
1468 case GL_STENCIL_BACK_REF:
1469 case GL_STENCIL_BACK_FAIL:
1470 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1471 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1472 case GL_DEPTH_FUNC:
1473 case GL_BLEND_SRC_RGB:
1474 case GL_BLEND_SRC_ALPHA:
1475 case GL_BLEND_DST_RGB:
1476 case GL_BLEND_DST_ALPHA:
1477 case GL_BLEND_EQUATION_RGB:
1478 case GL_BLEND_EQUATION_ALPHA:
1479 case GL_STENCIL_WRITEMASK:
1480 case GL_STENCIL_BACK_WRITEMASK:
1481 case GL_STENCIL_CLEAR_VALUE:
1482 case GL_SUBPIXEL_BITS:
1483 case GL_MAX_TEXTURE_SIZE:
1484 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1485 case GL_SAMPLE_BUFFERS:
1486 case GL_SAMPLES:
1487 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1488 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1489 case GL_TEXTURE_BINDING_2D:
1490 case GL_TEXTURE_BINDING_CUBE_MAP:
1491 {
1492 *type = GL_INT;
1493 *numParams = 1;
1494 }
1495 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001496 case GL_MAX_SAMPLES_ANGLE:
1497 {
1498 if (getMaxSupportedSamples() != 0)
1499 {
1500 *type = GL_INT;
1501 *numParams = 1;
1502 }
1503 else
1504 {
1505 return false;
1506 }
1507 }
1508 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001509 case GL_MAX_VIEWPORT_DIMS:
1510 {
1511 *type = GL_INT;
1512 *numParams = 2;
1513 }
1514 break;
1515 case GL_VIEWPORT:
1516 case GL_SCISSOR_BOX:
1517 {
1518 *type = GL_INT;
1519 *numParams = 4;
1520 }
1521 break;
1522 case GL_SHADER_COMPILER:
1523 case GL_SAMPLE_COVERAGE_INVERT:
1524 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001525 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1526 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1527 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1528 case GL_SAMPLE_COVERAGE:
1529 case GL_SCISSOR_TEST:
1530 case GL_STENCIL_TEST:
1531 case GL_DEPTH_TEST:
1532 case GL_BLEND:
1533 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001534 {
1535 *type = GL_BOOL;
1536 *numParams = 1;
1537 }
1538 break;
1539 case GL_COLOR_WRITEMASK:
1540 {
1541 *type = GL_BOOL;
1542 *numParams = 4;
1543 }
1544 break;
1545 case GL_POLYGON_OFFSET_FACTOR:
1546 case GL_POLYGON_OFFSET_UNITS:
1547 case GL_SAMPLE_COVERAGE_VALUE:
1548 case GL_DEPTH_CLEAR_VALUE:
1549 case GL_LINE_WIDTH:
1550 {
1551 *type = GL_FLOAT;
1552 *numParams = 1;
1553 }
1554 break;
1555 case GL_ALIASED_LINE_WIDTH_RANGE:
1556 case GL_ALIASED_POINT_SIZE_RANGE:
1557 case GL_DEPTH_RANGE:
1558 {
1559 *type = GL_FLOAT;
1560 *numParams = 2;
1561 }
1562 break;
1563 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001564 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001565 {
1566 *type = GL_FLOAT;
1567 *numParams = 4;
1568 }
1569 break;
1570 default:
1571 return false;
1572 }
1573
1574 return true;
1575}
1576
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577// Applies the render target surface, depth stencil surface, viewport rectangle and
1578// scissor rectangle to the Direct3D 9 device
1579bool Context::applyRenderTarget(bool ignoreViewport)
1580{
1581 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001582
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001583 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001584
1585 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1586 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001587 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588 }
1589
1590 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001591
1592 if (!renderTarget)
1593 {
1594 return false; // Context must be lost
1595 }
1596
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001597 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001599 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1600 if (renderTargetSerial != mAppliedRenderTargetSerial)
1601 {
1602 device->SetRenderTarget(0, renderTarget);
1603 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001604 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001605 }
1606
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001607 unsigned int depthbufferSerial = 0;
1608 unsigned int stencilbufferSerial = 0;
1609 if (framebufferObject->getDepthbufferType() != GL_NONE)
1610 {
1611 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001612 if (!depthStencil)
1613 {
1614 ERR("Depth stencil pointer unexpectedly null.");
1615 return false;
1616 }
1617
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001618 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1619 }
1620 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1621 {
1622 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001623 if (!depthStencil)
1624 {
1625 ERR("Depth stencil pointer unexpectedly null.");
1626 return false;
1627 }
1628
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001629 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1630 }
1631
1632 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001633 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001634 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001635 {
1636 device->SetDepthStencilSurface(depthStencil);
1637 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001638 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001639 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001640 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641
1642 D3DVIEWPORT9 viewport;
1643 D3DSURFACE_DESC desc;
1644 renderTarget->GetDesc(&desc);
1645
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001646 float zNear = clamp01(mState.zNear);
1647 float zFar = clamp01(mState.zFar);
1648
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649 if (ignoreViewport)
1650 {
1651 viewport.X = 0;
1652 viewport.Y = 0;
1653 viewport.Width = desc.Width;
1654 viewport.Height = desc.Height;
1655 viewport.MinZ = 0.0f;
1656 viewport.MaxZ = 1.0f;
1657 }
1658 else
1659 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001660 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1661 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1662 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1663 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1664 viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(desc.Height) - static_cast<LONG>(viewport.Y));
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001665 viewport.MinZ = zNear;
1666 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 }
1668
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001669 if (viewport.Width <= 0 || viewport.Height <= 0)
1670 {
1671 return false; // Nothing to render
1672 }
1673
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674 device->SetViewport(&viewport);
1675
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001676 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001678 if (mState.scissorTest)
1679 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001680 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1681 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1682 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1683 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1684 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001685 device->SetScissorRect(&rect);
1686 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1687 }
1688 else
1689 {
1690 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1691 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001692
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001693 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694 }
1695
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001696 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001698 Program *programObject = getCurrentProgram();
1699
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001700 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001701 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001702 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001703
daniel@transgaming.com31754962010-11-28 02:02:52 +00001704 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001705 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1706 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1707 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001708 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001709
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001710 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001711 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001712 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001713
daniel@transgaming.com31754962010-11-28 02:02:52 +00001714 GLint depthRange = programObject->getDxDepthRangeLocation();
1715 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1716 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717 }
1718
1719 return true;
1720}
1721
1722// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001723void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724{
1725 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001726 Program *programObject = getCurrentProgram();
1727
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001728 Framebuffer *framebufferObject = getDrawFramebuffer();
1729
1730 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1731
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001732 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001733 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001734 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001736 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001737 GLint alwaysFront = !isTriangleMode(drawMode);
1738 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1739
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001740 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001741 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001742 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001744 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745 }
1746 else
1747 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749 }
1750
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 mCullStateDirty = false;
1752 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001754 if (mDepthStateDirty)
1755 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001756 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001758 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1759 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 }
1761 else
1762 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765
1766 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767 }
1768
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001769 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001771 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001772 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001773 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1774
1775 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1776 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1777 {
1778 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1779 }
1780 else
1781 {
1782 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1783 unorm<8>(mState.blendColor.alpha),
1784 unorm<8>(mState.blendColor.alpha),
1785 unorm<8>(mState.blendColor.alpha)));
1786 }
1787
1788 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1789 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1790 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1791
1792 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1793 mState.destBlendRGB != mState.destBlendAlpha ||
1794 mState.blendEquationRGB != mState.blendEquationAlpha)
1795 {
1796 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1797
1798 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1799 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1800 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001801 }
1802 else
1803 {
1804 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1805 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001806 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001807 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001808 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001809 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001810 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001811
1812 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 }
1814
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001815 if (mStencilStateDirty || mFrontFaceDirty)
1816 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001817 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001818 {
1819 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1820 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1821
1822 // FIXME: Unsupported by D3D9
1823 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1824 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1825 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1826 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1827 mState.stencilRef != mState.stencilBackRef ||
1828 mState.stencilMask != mState.stencilBackMask)
1829 {
1830 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1831 return error(GL_INVALID_OPERATION);
1832 }
1833
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001834 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001835 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001836 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1837
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001838 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1839 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001840 es2dx::ConvertComparison(mState.stencilFunc));
1841
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001842 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1843 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001844
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001845 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001846 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001847 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001849 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001850 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1851
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001852 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1853 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001854 es2dx::ConvertComparison(mState.stencilBackFunc));
1855
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001856 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1857 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001858
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001859 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001860 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001861 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001862 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001863 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001864 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1865 }
1866 else
1867 {
1868 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1869 }
1870
1871 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001872 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001873 }
1874
1875 if (mMaskStateDirty)
1876 {
1877 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1878 mState.colorMaskBlue, mState.colorMaskAlpha));
1879 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1880
1881 mMaskStateDirty = false;
1882 }
1883
1884 if (mPolygonOffsetStateDirty)
1885 {
1886 if (mState.polygonOffsetFill)
1887 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001888 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001889 if (depthbuffer)
1890 {
1891 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1892 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1893 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1894 }
1895 }
1896 else
1897 {
1898 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1899 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1900 }
1901
1902 mPolygonOffsetStateDirty = false;
1903 }
1904
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001905 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001907 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001908 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001909 FIXME("Sample alpha to coverage is unimplemented.");
1910 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001911
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001912 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1913 if (mState.sampleCoverage)
1914 {
1915 unsigned int mask = 0;
1916 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001917 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001918 float threshold = 0.5f;
1919
1920 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001921 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001922 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001923
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001924 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001925 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001926 threshold += 1.0f;
1927 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001928 }
1929 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001930 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001931
1932 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001933 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001934 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001935 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001936
1937 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001938 }
1939 else
1940 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001941 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001942 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001943
1944 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945 }
1946
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001947 if (mDitherStateDirty)
1948 {
1949 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1950
1951 mDitherStateDirty = false;
1952 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953}
1954
daniel@transgaming.com83921382011-01-08 05:46:00 +00001955GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001956{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001957 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001958
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001959 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001960 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001961 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001962 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001963 }
1964
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00001965 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001966}
1967
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001968// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001969GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001970{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001971 IDirect3DDevice9 *device = getDevice();
1972 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001973
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001974 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001975 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001976 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001977 }
1978
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001979 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980}
1981
1982// Applies the shaders and shader constants to the Direct3D 9 device
1983void Context::applyShaders()
1984{
1985 IDirect3DDevice9 *device = getDevice();
1986 Program *programObject = getCurrentProgram();
1987 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1988 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1989
1990 device->SetVertexShader(vertexShader);
1991 device->SetPixelShader(pixelShader);
1992
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001993 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001994 {
1995 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001996 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001997 }
1998
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 programObject->applyUniforms();
2000}
2001
2002// Applies the textures and sampler states to the Direct3D 9 device
2003void Context::applyTextures()
2004{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002005 applyTextures(SAMPLER_PIXEL);
2006
2007 if (mSupportsVertexTexture)
2008 {
2009 applyTextures(SAMPLER_VERTEX);
2010 }
2011}
2012
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002013// For each Direct3D 9 sampler of either the pixel or vertex stage,
2014// looks up the corresponding OpenGL texture image unit and texture type,
2015// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002016void Context::applyTextures(SamplerType type)
2017{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018 IDirect3DDevice9 *device = getDevice();
2019 Program *programObject = getCurrentProgram();
2020
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002021 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 +00002022
2023 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002025 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002026 int d3dSampler = (type == SAMPLER_PIXEL) ? samplerIndex : D3DVERTEXTEXTURESAMPLER0 + samplerIndex;
2027 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2028
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002029 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002031 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002032
2033 Texture *texture = getSamplerTexture(textureUnit, textureType);
2034
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002035 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002036 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002037 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2038
2039 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002040 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002041 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002042 {
2043 GLenum wrapS = texture->getWrapS();
2044 GLenum wrapT = texture->getWrapT();
2045 GLenum minFilter = texture->getMinFilter();
2046 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002047
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002048 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2049 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002051 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002052 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2053 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002054 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2055 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002056 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002058 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002059 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002060 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002061 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002062 }
2063 else
2064 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002065 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002066 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002067
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002068 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002069 texture->resetDirty();
2070 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002072 else
2073 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002074 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002075 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002076 device->SetTexture(d3dSampler, NULL);
2077 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002078 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002079 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080 }
2081}
2082
2083void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2084{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002085 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002086
2087 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2088 {
2089 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2090 }
2091
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002092 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2093 {
2094 return error(GL_INVALID_OPERATION);
2095 }
2096
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002098
2099 if (!renderTarget)
2100 {
2101 return; // Context must be lost, return silently
2102 }
2103
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 IDirect3DDevice9 *device = getDevice();
2105
2106 D3DSURFACE_DESC desc;
2107 renderTarget->GetDesc(&desc);
2108
2109 IDirect3DSurface9 *systemSurface;
2110 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2111
2112 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2113 {
2114 return error(GL_OUT_OF_MEMORY);
2115 }
2116
2117 ASSERT(SUCCEEDED(result));
2118
2119 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2120 {
2121 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2122 }
2123
2124 result = device->GetRenderTargetData(renderTarget, systemSurface);
2125
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 if (FAILED(result))
2127 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 systemSurface->Release();
2129
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002130 switch (result)
2131 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002132 case D3DERR_DRIVERINTERNALERROR:
2133 case D3DERR_DEVICELOST:
2134 return error(GL_OUT_OF_MEMORY);
2135 default:
2136 UNREACHABLE();
2137 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002138 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139 }
2140
2141 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002142 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2143 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2144 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2145 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2146 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147
2148 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2149
2150 if (FAILED(result))
2151 {
2152 UNREACHABLE();
2153 systemSurface->Release();
2154
2155 return; // No sensible error to generate
2156 }
2157
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002158 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002159 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002160 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002161 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002162 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002163
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002164 for (int j = 0; j < rect.bottom - rect.top; j++)
2165 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002166 if (desc.Format == D3DFMT_A8R8G8B8 &&
2167 format == GL_BGRA_EXT &&
2168 type == GL_UNSIGNED_BYTE)
2169 {
2170 // Fast path for EXT_read_format_bgra, given
2171 // an RGBA source buffer. Note that buffers with no
2172 // alpha go through the slow path below.
2173 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002174 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002175 (rect.right - rect.left) * 4);
2176 continue;
2177 }
2178
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 for (int i = 0; i < rect.right - rect.left; i++)
2180 {
2181 float r;
2182 float g;
2183 float b;
2184 float a;
2185
2186 switch (desc.Format)
2187 {
2188 case D3DFMT_R5G6B5:
2189 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002190 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002191
2192 a = 1.0f;
2193 b = (rgb & 0x001F) * (1.0f / 0x001F);
2194 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2195 r = (rgb & 0xF800) * (1.0f / 0xF800);
2196 }
2197 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002198 case D3DFMT_A1R5G5B5:
2199 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002200 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201
2202 a = (argb & 0x8000) ? 1.0f : 0.0f;
2203 b = (argb & 0x001F) * (1.0f / 0x001F);
2204 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2205 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2206 }
2207 break;
2208 case D3DFMT_A8R8G8B8:
2209 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002210 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211
2212 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2213 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2214 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2215 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2216 }
2217 break;
2218 case D3DFMT_X8R8G8B8:
2219 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002220 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002221
2222 a = 1.0f;
2223 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2224 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2225 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2226 }
2227 break;
2228 case D3DFMT_A2R10G10B10:
2229 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002230 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002231
2232 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2233 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2234 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2235 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2236 }
2237 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002238 case D3DFMT_A32B32G32R32F:
2239 {
2240 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002241 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2242 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2243 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2244 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002245 }
2246 break;
2247 case D3DFMT_A16B16G16R16F:
2248 {
2249 // float formats in D3D are stored rgba, rather than the other way round
2250 float abgr[4];
2251
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002252 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002253
2254 a = abgr[3];
2255 b = abgr[2];
2256 g = abgr[1];
2257 r = abgr[0];
2258 }
2259 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002260 default:
2261 UNIMPLEMENTED(); // FIXME
2262 UNREACHABLE();
2263 }
2264
2265 switch (format)
2266 {
2267 case GL_RGBA:
2268 switch (type)
2269 {
2270 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002271 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2272 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2273 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2274 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275 break;
2276 default: UNREACHABLE();
2277 }
2278 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002279 case GL_BGRA_EXT:
2280 switch (type)
2281 {
2282 case GL_UNSIGNED_BYTE:
2283 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2284 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2285 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2286 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2287 break;
2288 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2289 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2290 // this type is packed as follows:
2291 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2292 // --------------------------------------------------------------------------------
2293 // | 4th | 3rd | 2nd | 1st component |
2294 // --------------------------------------------------------------------------------
2295 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2296 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2297 ((unsigned short)(15 * a + 0.5f) << 12)|
2298 ((unsigned short)(15 * r + 0.5f) << 8) |
2299 ((unsigned short)(15 * g + 0.5f) << 4) |
2300 ((unsigned short)(15 * b + 0.5f) << 0);
2301 break;
2302 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2303 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2304 // this type is packed as follows:
2305 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2306 // --------------------------------------------------------------------------------
2307 // | 4th | 3rd | 2nd | 1st component |
2308 // --------------------------------------------------------------------------------
2309 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2310 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2311 ((unsigned short)( a + 0.5f) << 15) |
2312 ((unsigned short)(31 * r + 0.5f) << 10) |
2313 ((unsigned short)(31 * g + 0.5f) << 5) |
2314 ((unsigned short)(31 * b + 0.5f) << 0);
2315 break;
2316 default: UNREACHABLE();
2317 }
2318 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002319 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002320 switch (type)
2321 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002322 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002323 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2324 ((unsigned short)(31 * b + 0.5f) << 0) |
2325 ((unsigned short)(63 * g + 0.5f) << 5) |
2326 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327 break;
2328 default: UNREACHABLE();
2329 }
2330 break;
2331 default: UNREACHABLE();
2332 }
2333 }
2334 }
2335
2336 systemSurface->UnlockRect();
2337
2338 systemSurface->Release();
2339}
2340
2341void Context::clear(GLbitfield mask)
2342{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002343 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002344
2345 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2346 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002347 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002348 }
2349
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002350 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351 IDirect3DDevice9 *device = getDevice();
2352 DWORD flags = 0;
2353
2354 if (mask & GL_COLOR_BUFFER_BIT)
2355 {
2356 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002357
2358 if (framebufferObject->getColorbufferType() != GL_NONE)
2359 {
2360 flags |= D3DCLEAR_TARGET;
2361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362 }
2363
2364 if (mask & GL_DEPTH_BUFFER_BIT)
2365 {
2366 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002367 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368 {
2369 flags |= D3DCLEAR_ZBUFFER;
2370 }
2371 }
2372
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373 GLuint stencilUnmasked = 0x0;
2374
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002375 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002378 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002380 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002381 if (!depthStencil)
2382 {
2383 ERR("Depth stencil pointer unexpectedly null.");
2384 return;
2385 }
2386
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002387 D3DSURFACE_DESC desc;
2388 depthStencil->GetDesc(&desc);
2389
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002390 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002391 stencilUnmasked = (0x1 << stencilSize) - 1;
2392
2393 if (stencilUnmasked != 0x0)
2394 {
2395 flags |= D3DCLEAR_STENCIL;
2396 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397 }
2398 }
2399
2400 if (mask != 0)
2401 {
2402 return error(GL_INVALID_VALUE);
2403 }
2404
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002405 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2406 {
2407 return;
2408 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002410 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002411 unorm<8>(mState.colorClearValue.red),
2412 unorm<8>(mState.colorClearValue.green),
2413 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002414 float depth = clamp01(mState.depthClearValue);
2415 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416
2417 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2418
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002419 if (!renderTarget)
2420 {
2421 return; // Context must be lost, return silently
2422 }
2423
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424 D3DSURFACE_DESC desc;
2425 renderTarget->GetDesc(&desc);
2426
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002427 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428
2429 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002430 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002432 !(mState.colorMaskRed && mState.colorMaskGreen &&
2433 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434
2435 if (needMaskedColorClear || needMaskedStencilClear)
2436 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002437 // State which is altered in all paths from this point to the clear call is saved.
2438 // State which is altered in only some paths will be flagged dirty in the case that
2439 // that path is taken.
2440 HRESULT hr;
2441 if (mMaskedClearSavedState == NULL)
2442 {
2443 hr = device->BeginStateBlock();
2444 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2445
2446 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2447 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2448 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2449 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2450 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2451 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2452 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2453 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2454 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2455 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2456 device->SetPixelShader(NULL);
2457 device->SetVertexShader(NULL);
2458 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002459 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2460 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2461 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2462 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2463 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2464 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2465 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002466
2467 hr = device->EndStateBlock(&mMaskedClearSavedState);
2468 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2469 }
2470
2471 ASSERT(mMaskedClearSavedState != NULL);
2472
2473 if (mMaskedClearSavedState != NULL)
2474 {
2475 hr = mMaskedClearSavedState->Capture();
2476 ASSERT(SUCCEEDED(hr));
2477 }
2478
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2480 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2481 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2482 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2483 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2484 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2485 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2486 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2487
2488 if (flags & D3DCLEAR_TARGET)
2489 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002490 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 }
2492 else
2493 {
2494 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2495 }
2496
2497 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2498 {
2499 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2500 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2501 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2502 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002503 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002504 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2506 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002507 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508 }
2509 else
2510 {
2511 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2512 }
2513
2514 device->SetPixelShader(NULL);
2515 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002516 device->SetFVF(D3DFVF_XYZRHW);
2517 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2518 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2519 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2520 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2521 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2522 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2523 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002525 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2526 quad[0][0] = -0.5f;
2527 quad[0][1] = desc.Height - 0.5f;
2528 quad[0][2] = 0.0f;
2529 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002531 quad[1][0] = desc.Width - 0.5f;
2532 quad[1][1] = desc.Height - 0.5f;
2533 quad[1][2] = 0.0f;
2534 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002535
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002536 quad[2][0] = -0.5f;
2537 quad[2][1] = -0.5f;
2538 quad[2][2] = 0.0f;
2539 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002541 quad[3][0] = desc.Width - 0.5f;
2542 quad[3][1] = -0.5f;
2543 quad[3][2] = 0.0f;
2544 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002546 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002547 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548
2549 if (flags & D3DCLEAR_ZBUFFER)
2550 {
2551 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2552 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2553 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2554 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002555
2556 if (mMaskedClearSavedState != NULL)
2557 {
2558 mMaskedClearSavedState->Apply();
2559 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002560 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002561 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 {
2563 device->Clear(0, NULL, flags, color, depth, stencil);
2564 }
2565}
2566
2567void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2568{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002569 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 {
2571 return error(GL_INVALID_OPERATION);
2572 }
2573
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002574 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002575 IDirect3DDevice9 *device = getDevice();
2576 D3DPRIMITIVETYPE primitiveType;
2577 int primitiveCount;
2578
2579 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2580 return error(GL_INVALID_ENUM);
2581
2582 if (primitiveCount <= 0)
2583 {
2584 return;
2585 }
2586
2587 if (!applyRenderTarget(false))
2588 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002589 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002590 }
2591
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002592 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002593
daniel@transgaming.com83921382011-01-08 05:46:00 +00002594 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002595 if (err != GL_NO_ERROR)
2596 {
2597 return error(err);
2598 }
2599
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600 applyShaders();
2601 applyTextures();
2602
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002603 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002604 {
2605 return error(GL_INVALID_OPERATION);
2606 }
2607
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002608 if (!cullSkipsDraw(mode))
2609 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002610 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002611
2612 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002613
2614 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2615 {
2616 drawClosingLine(first, first + count - 1);
2617 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002618 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619}
2620
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002621void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002623 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 {
2625 return error(GL_INVALID_OPERATION);
2626 }
2627
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002628 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 {
2630 return error(GL_INVALID_OPERATION);
2631 }
2632
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002633 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634 IDirect3DDevice9 *device = getDevice();
2635 D3DPRIMITIVETYPE primitiveType;
2636 int primitiveCount;
2637
2638 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2639 return error(GL_INVALID_ENUM);
2640
2641 if (primitiveCount <= 0)
2642 {
2643 return;
2644 }
2645
2646 if (!applyRenderTarget(false))
2647 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002648 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649 }
2650
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002651 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002652
2653 TranslatedIndexData indexInfo;
2654 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2655 if (err != GL_NO_ERROR)
2656 {
2657 return error(err);
2658 }
2659
daniel@transgaming.com83921382011-01-08 05:46:00 +00002660 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2661 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002662 if (err != GL_NO_ERROR)
2663 {
2664 return error(err);
2665 }
2666
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002667 applyShaders();
2668 applyTextures();
2669
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002670 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002671 {
2672 return error(GL_INVALID_OPERATION);
2673 }
2674
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002675 if (!cullSkipsDraw(mode))
2676 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002677 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002678
daniel@transgaming.com83921382011-01-08 05:46:00 +00002679 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002680
2681 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2682 {
2683 drawClosingLine(count, type, indices);
2684 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686}
2687
2688void Context::finish()
2689{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002690 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002691 IDirect3DDevice9 *device = getDevice();
2692 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002693 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002695 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2696 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002698 ERR("CreateQuery failed hr=%x\n", result);
2699 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2700 {
2701 return error(GL_OUT_OF_MEMORY);
2702 }
2703 ASSERT(false);
2704 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 }
2706
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002707 IDirect3DStateBlock9 *savedState = NULL;
2708 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2709 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002711 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002712 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002713
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002714 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002715 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002716 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002717 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002718 ASSERT(false);
2719 return;
2720 }
2721
2722 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2723 if (FAILED(result))
2724 {
2725 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2726 occlusionQuery->Release();
2727 savedState->Release();
2728 ASSERT(false);
2729 return;
2730 }
2731
2732 // Render something outside the render target
2733 device->SetPixelShader(NULL);
2734 device->SetVertexShader(NULL);
2735 device->SetFVF(D3DFVF_XYZRHW);
2736 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2737 display->startScene();
2738 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2739
2740 result = occlusionQuery->Issue(D3DISSUE_END);
2741 if (FAILED(result))
2742 {
2743 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2744 occlusionQuery->Release();
2745 savedState->Apply();
2746 savedState->Release();
2747 ASSERT(false);
2748 return;
2749 }
2750
2751 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2752 {
2753 // Keep polling, but allow other threads to do something useful first
2754 Sleep(0);
2755 }
2756
2757 occlusionQuery->Release();
2758 savedState->Apply();
2759 savedState->Release();
2760
2761 if (result == D3DERR_DEVICELOST)
2762 {
2763 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764 }
2765}
2766
2767void Context::flush()
2768{
2769 IDirect3DDevice9 *device = getDevice();
2770 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002771 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002772
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002773 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2774 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002776 ERR("CreateQuery failed hr=%x\n", result);
2777 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2778 {
2779 return error(GL_OUT_OF_MEMORY);
2780 }
2781 ASSERT(false);
2782 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002783 }
2784
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002785 result = eventQuery->Issue(D3DISSUE_END);
2786 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002787 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002788 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2789 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002790 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002791 return;
2792 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002793
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002794 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2795 eventQuery->Release();
2796
2797 if (result == D3DERR_DEVICELOST)
2798 {
2799 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002800 }
2801}
2802
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002803void Context::drawClosingLine(unsigned int first, unsigned int last)
2804{
2805 IDirect3DDevice9 *device = getDevice();
2806 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002807 bool succeeded = false;
2808 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002809
2810 if (supports32bitIndices())
2811 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002812 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002813
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002814 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002815 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002816 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2817 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002818
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002819 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2820
2821 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2822 if (data)
2823 {
2824 data[0] = last;
2825 data[1] = first;
2826 mClosingIB->unmap();
2827 offset /= 4;
2828 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002829 }
2830 }
2831 else
2832 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002833 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002834
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002835 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002836 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002837 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2838 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002839
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002840 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2841
2842 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2843 if (data)
2844 {
2845 data[0] = last;
2846 data[1] = first;
2847 mClosingIB->unmap();
2848 offset /= 2;
2849 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002850 }
2851 }
2852
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002853 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002854 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002855 device->SetIndices(mClosingIB->getBuffer());
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002856
jbauman@chromium.org2c199b12011-06-13 22:20:06 +00002857 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002858 }
2859 else
2860 {
2861 ERR("Could not create an index buffer for closing a line loop.");
2862 error(GL_OUT_OF_MEMORY);
2863 }
2864}
2865
2866void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2867{
2868 unsigned int first = 0;
2869 unsigned int last = 0;
2870
2871 if (mState.elementArrayBuffer.get())
2872 {
2873 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2874 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2875 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2876 }
2877
2878 switch (type)
2879 {
2880 case GL_UNSIGNED_BYTE:
2881 first = static_cast<const GLubyte*>(indices)[0];
2882 last = static_cast<const GLubyte*>(indices)[count - 1];
2883 break;
2884 case GL_UNSIGNED_SHORT:
2885 first = static_cast<const GLushort*>(indices)[0];
2886 last = static_cast<const GLushort*>(indices)[count - 1];
2887 break;
2888 case GL_UNSIGNED_INT:
2889 first = static_cast<const GLuint*>(indices)[0];
2890 last = static_cast<const GLuint*>(indices)[count - 1];
2891 break;
2892 default: UNREACHABLE();
2893 }
2894
2895 drawClosingLine(first, last);
2896}
2897
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002898void Context::recordInvalidEnum()
2899{
2900 mInvalidEnum = true;
2901}
2902
2903void Context::recordInvalidValue()
2904{
2905 mInvalidValue = true;
2906}
2907
2908void Context::recordInvalidOperation()
2909{
2910 mInvalidOperation = true;
2911}
2912
2913void Context::recordOutOfMemory()
2914{
2915 mOutOfMemory = true;
2916}
2917
2918void Context::recordInvalidFramebufferOperation()
2919{
2920 mInvalidFramebufferOperation = true;
2921}
2922
2923// Get one of the recorded errors and clear its flag, if any.
2924// [OpenGL ES 2.0.24] section 2.5 page 13.
2925GLenum Context::getError()
2926{
2927 if (mInvalidEnum)
2928 {
2929 mInvalidEnum = false;
2930
2931 return GL_INVALID_ENUM;
2932 }
2933
2934 if (mInvalidValue)
2935 {
2936 mInvalidValue = false;
2937
2938 return GL_INVALID_VALUE;
2939 }
2940
2941 if (mInvalidOperation)
2942 {
2943 mInvalidOperation = false;
2944
2945 return GL_INVALID_OPERATION;
2946 }
2947
2948 if (mOutOfMemory)
2949 {
2950 mOutOfMemory = false;
2951
2952 return GL_OUT_OF_MEMORY;
2953 }
2954
2955 if (mInvalidFramebufferOperation)
2956 {
2957 mInvalidFramebufferOperation = false;
2958
2959 return GL_INVALID_FRAMEBUFFER_OPERATION;
2960 }
2961
2962 return GL_NO_ERROR;
2963}
2964
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002965bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002966{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002967 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002968}
2969
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002970int Context::getMaximumVaryingVectors() const
2971{
2972 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2973}
2974
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00002975unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002976{
2977 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
2978}
2979
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00002980unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002981{
2982 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
2983}
2984
daniel@transgaming.com458da142010-11-28 02:03:02 +00002985int Context::getMaximumFragmentUniformVectors() const
2986{
2987 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2988}
2989
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002990int Context::getMaxSupportedSamples() const
2991{
2992 return mMaxSupportedSamples;
2993}
2994
2995int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2996{
2997 if (requested == 0)
2998 {
2999 return requested;
3000 }
3001
3002 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3003 if (itr == mMultiSampleSupport.end())
3004 {
3005 return -1;
3006 }
3007
3008 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3009 {
3010 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3011 {
3012 return i;
3013 }
3014 }
3015
3016 return -1;
3017}
3018
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003019bool Context::supportsEventQueries() const
3020{
3021 return mSupportsEventQueries;
3022}
3023
daniel@transgaming.com01868132010-08-24 19:21:17 +00003024bool Context::supportsCompressedTextures() const
3025{
3026 return mSupportsCompressedTextures;
3027}
3028
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003029bool Context::supportsFloatTextures() const
3030{
3031 return mSupportsFloatTextures;
3032}
3033
3034bool Context::supportsFloatLinearFilter() const
3035{
3036 return mSupportsFloatLinearFilter;
3037}
3038
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003039bool Context::supportsFloatRenderableTextures() const
3040{
3041 return mSupportsFloatRenderableTextures;
3042}
3043
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003044bool Context::supportsHalfFloatTextures() const
3045{
3046 return mSupportsHalfFloatTextures;
3047}
3048
3049bool Context::supportsHalfFloatLinearFilter() const
3050{
3051 return mSupportsHalfFloatLinearFilter;
3052}
3053
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003054bool Context::supportsHalfFloatRenderableTextures() const
3055{
3056 return mSupportsHalfFloatRenderableTextures;
3057}
3058
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003059int Context::getMaximumRenderbufferDimension() const
3060{
3061 return mMaxRenderbufferDimension;
3062}
3063
3064int Context::getMaximumTextureDimension() const
3065{
3066 return mMaxTextureDimension;
3067}
3068
3069int Context::getMaximumCubeTextureDimension() const
3070{
3071 return mMaxCubeTextureDimension;
3072}
3073
3074int Context::getMaximumTextureLevel() const
3075{
3076 return mMaxTextureLevel;
3077}
3078
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003079bool Context::supportsLuminanceTextures() const
3080{
3081 return mSupportsLuminanceTextures;
3082}
3083
3084bool Context::supportsLuminanceAlphaTextures() const
3085{
3086 return mSupportsLuminanceAlphaTextures;
3087}
3088
daniel@transgaming.com83921382011-01-08 05:46:00 +00003089bool Context::supports32bitIndices() const
3090{
3091 return mSupports32bitIndices;
3092}
3093
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003094bool Context::supportsNonPower2Texture() const
3095{
3096 return mSupportsNonPower2Texture;
3097}
3098
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003099void Context::detachBuffer(GLuint buffer)
3100{
3101 // [OpenGL ES 2.0.24] section 2.9 page 22:
3102 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3103 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3104
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003105 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003106 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003107 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003108 }
3109
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003110 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003111 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003112 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003113 }
3114
3115 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3116 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003117 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003118 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003119 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003120 }
3121 }
3122}
3123
3124void Context::detachTexture(GLuint texture)
3125{
3126 // [OpenGL ES 2.0.24] section 3.8 page 84:
3127 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3128 // rebound to texture object zero
3129
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003130 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003131 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003132 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003133 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003134 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003135 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003136 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003137 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003138 }
3139 }
3140
3141 // [OpenGL ES 2.0.24] section 4.4 page 112:
3142 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3143 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3144 // image was attached in the currently bound framebuffer.
3145
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003146 Framebuffer *readFramebuffer = getReadFramebuffer();
3147 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003148
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003149 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003150 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003151 readFramebuffer->detachTexture(texture);
3152 }
3153
3154 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3155 {
3156 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003157 }
3158}
3159
3160void Context::detachFramebuffer(GLuint framebuffer)
3161{
3162 // [OpenGL ES 2.0.24] section 4.4 page 107:
3163 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3164 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3165
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003166 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003167 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003168 bindReadFramebuffer(0);
3169 }
3170
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003171 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003172 {
3173 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003174 }
3175}
3176
3177void Context::detachRenderbuffer(GLuint renderbuffer)
3178{
3179 // [OpenGL ES 2.0.24] section 4.4 page 109:
3180 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3181 // had been executed with the target RENDERBUFFER and name of zero.
3182
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003183 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003184 {
3185 bindRenderbuffer(0);
3186 }
3187
3188 // [OpenGL ES 2.0.24] section 4.4 page 111:
3189 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3190 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3191 // point to which this image was attached in the currently bound framebuffer.
3192
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003193 Framebuffer *readFramebuffer = getReadFramebuffer();
3194 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003195
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003196 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003197 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003198 readFramebuffer->detachRenderbuffer(renderbuffer);
3199 }
3200
3201 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3202 {
3203 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003204 }
3205}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003206
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003207Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003208{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003209 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003210
3211 if (t == NULL)
3212 {
3213 static const GLubyte color[] = { 0, 0, 0, 255 };
3214
3215 switch (type)
3216 {
3217 default:
3218 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003219 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003220
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003221 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003222 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003223 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003224 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003225 t = incomplete2d;
3226 }
3227 break;
3228
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003229 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003230 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003231 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003232
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003233 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3234 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3235 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3236 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3237 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3238 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003239
3240 t = incompleteCube;
3241 }
3242 break;
3243 }
3244
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003245 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003246 }
3247
3248 return t;
3249}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003250
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003251bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003252{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003253 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003254}
3255
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003256bool Context::isTriangleMode(GLenum drawMode)
3257{
3258 switch (drawMode)
3259 {
3260 case GL_TRIANGLES:
3261 case GL_TRIANGLE_FAN:
3262 case GL_TRIANGLE_STRIP:
3263 return true;
3264 case GL_POINTS:
3265 case GL_LINES:
3266 case GL_LINE_LOOP:
3267 case GL_LINE_STRIP:
3268 return false;
3269 default: UNREACHABLE();
3270 }
3271
3272 return false;
3273}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003274
3275void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3276{
3277 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3278
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003279 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3280 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3281 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3282 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003283
daniel@transgaming.com83921382011-01-08 05:46:00 +00003284 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003285}
3286
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003287void Context::initExtensionString()
3288{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003289 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003290 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3291 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003292 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003293 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003294 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003295
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003296 if (supportsEventQueries())
3297 {
3298 mExtensionString += "GL_NV_fence ";
3299 }
3300
daniel@transgaming.com01868132010-08-24 19:21:17 +00003301 if (supportsCompressedTextures())
3302 {
3303 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3304 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003305
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003306 if (supportsFloatTextures())
3307 {
3308 mExtensionString += "GL_OES_texture_float ";
3309 }
3310
3311 if (supportsHalfFloatTextures())
3312 {
3313 mExtensionString += "GL_OES_texture_half_float ";
3314 }
3315
3316 if (supportsFloatLinearFilter())
3317 {
3318 mExtensionString += "GL_OES_texture_float_linear ";
3319 }
3320
3321 if (supportsHalfFloatLinearFilter())
3322 {
3323 mExtensionString += "GL_OES_texture_half_float_linear ";
3324 }
3325
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003326 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003327 {
3328 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3329 }
3330
daniel@transgaming.com83921382011-01-08 05:46:00 +00003331 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003332 {
3333 mExtensionString += "GL_OES_element_index_uint ";
3334 }
3335
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003336 if (supportsNonPower2Texture())
3337 {
3338 mExtensionString += "GL_OES_texture_npot ";
3339 }
3340
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003341 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3342 if (end != std::string::npos)
3343 {
3344 mExtensionString.resize(end+1);
3345 }
3346}
3347
3348const char *Context::getExtensionString() const
3349{
3350 return mExtensionString.c_str();
3351}
3352
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003353void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3354 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3355 GLbitfield mask)
3356{
3357 IDirect3DDevice9 *device = getDevice();
3358
3359 Framebuffer *readFramebuffer = getReadFramebuffer();
3360 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3361
3362 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3363 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3364 {
3365 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3366 }
3367
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003368 if (drawFramebuffer->getSamples() != 0)
3369 {
3370 return error(GL_INVALID_OPERATION);
3371 }
3372
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003373 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3374 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3375 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3376 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3377
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003378 RECT sourceRect;
3379 RECT destRect;
3380
3381 if (srcX0 < srcX1)
3382 {
3383 sourceRect.left = srcX0;
3384 sourceRect.right = srcX1;
3385 destRect.left = dstX0;
3386 destRect.right = dstX1;
3387 }
3388 else
3389 {
3390 sourceRect.left = srcX1;
3391 destRect.left = dstX1;
3392 sourceRect.right = srcX0;
3393 destRect.right = dstX0;
3394 }
3395
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003396 if (srcY0 < srcY1)
3397 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003398 sourceRect.top = readBufferHeight - srcY1;
3399 destRect.top = drawBufferHeight - dstY1;
3400 sourceRect.bottom = readBufferHeight - srcY0;
3401 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003402 }
3403 else
3404 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003405 sourceRect.top = readBufferHeight - srcY0;
3406 destRect.top = drawBufferHeight - dstY0;
3407 sourceRect.bottom = readBufferHeight - srcY1;
3408 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003409 }
3410
3411 RECT sourceScissoredRect = sourceRect;
3412 RECT destScissoredRect = destRect;
3413
3414 if (mState.scissorTest)
3415 {
3416 // Only write to parts of the destination framebuffer which pass the scissor test
3417 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3418 // rect will be checked against scissorY, rather than the bottom.
3419 if (destRect.left < mState.scissorX)
3420 {
3421 int xDiff = mState.scissorX - destRect.left;
3422 destScissoredRect.left = mState.scissorX;
3423 sourceScissoredRect.left += xDiff;
3424 }
3425
3426 if (destRect.right > mState.scissorX + mState.scissorWidth)
3427 {
3428 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3429 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3430 sourceScissoredRect.right -= xDiff;
3431 }
3432
3433 if (destRect.top < mState.scissorY)
3434 {
3435 int yDiff = mState.scissorY - destRect.top;
3436 destScissoredRect.top = mState.scissorY;
3437 sourceScissoredRect.top += yDiff;
3438 }
3439
3440 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3441 {
3442 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3443 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3444 sourceScissoredRect.bottom -= yDiff;
3445 }
3446 }
3447
3448 bool blitRenderTarget = false;
3449 bool blitDepthStencil = false;
3450
3451 RECT sourceTrimmedRect = sourceScissoredRect;
3452 RECT destTrimmedRect = destScissoredRect;
3453
3454 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3455 // the actual draw and read surfaces.
3456 if (sourceTrimmedRect.left < 0)
3457 {
3458 int xDiff = 0 - sourceTrimmedRect.left;
3459 sourceTrimmedRect.left = 0;
3460 destTrimmedRect.left += xDiff;
3461 }
3462
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003463 if (sourceTrimmedRect.right > readBufferWidth)
3464 {
3465 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3466 sourceTrimmedRect.right = readBufferWidth;
3467 destTrimmedRect.right -= xDiff;
3468 }
3469
3470 if (sourceTrimmedRect.top < 0)
3471 {
3472 int yDiff = 0 - sourceTrimmedRect.top;
3473 sourceTrimmedRect.top = 0;
3474 destTrimmedRect.top += yDiff;
3475 }
3476
3477 if (sourceTrimmedRect.bottom > readBufferHeight)
3478 {
3479 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3480 sourceTrimmedRect.bottom = readBufferHeight;
3481 destTrimmedRect.bottom -= yDiff;
3482 }
3483
3484 if (destTrimmedRect.left < 0)
3485 {
3486 int xDiff = 0 - destTrimmedRect.left;
3487 destTrimmedRect.left = 0;
3488 sourceTrimmedRect.left += xDiff;
3489 }
3490
3491 if (destTrimmedRect.right > drawBufferWidth)
3492 {
3493 int xDiff = destTrimmedRect.right - drawBufferWidth;
3494 destTrimmedRect.right = drawBufferWidth;
3495 sourceTrimmedRect.right -= xDiff;
3496 }
3497
3498 if (destTrimmedRect.top < 0)
3499 {
3500 int yDiff = 0 - destTrimmedRect.top;
3501 destTrimmedRect.top = 0;
3502 sourceTrimmedRect.top += yDiff;
3503 }
3504
3505 if (destTrimmedRect.bottom > drawBufferHeight)
3506 {
3507 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3508 destTrimmedRect.bottom = drawBufferHeight;
3509 sourceTrimmedRect.bottom -= yDiff;
3510 }
3511
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003512 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003513 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3514 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3515 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3516 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003517 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3518 {
3519 partialBufferCopy = true;
3520 }
3521
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003522 if (mask & GL_COLOR_BUFFER_BIT)
3523 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003524 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3525 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3526 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3527 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3528 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003529 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3530 {
3531 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3532 return error(GL_INVALID_OPERATION);
3533 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003534
3535 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3536 {
3537 return error(GL_INVALID_OPERATION);
3538 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003539
3540 blitRenderTarget = true;
3541
3542 }
3543
3544 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3545 {
3546 DepthStencilbuffer *readDSBuffer = NULL;
3547 DepthStencilbuffer *drawDSBuffer = NULL;
3548
3549 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3550 // both a depth and stencil buffer, it will be the same buffer.
3551
3552 if (mask & GL_DEPTH_BUFFER_BIT)
3553 {
3554 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3555 {
3556 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3557 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3558 {
3559 return error(GL_INVALID_OPERATION);
3560 }
3561
3562 blitDepthStencil = true;
3563 readDSBuffer = readFramebuffer->getDepthbuffer();
3564 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3565 }
3566 }
3567
3568 if (mask & GL_STENCIL_BUFFER_BIT)
3569 {
3570 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3571 {
3572 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3573 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3574 {
3575 return error(GL_INVALID_OPERATION);
3576 }
3577
3578 blitDepthStencil = true;
3579 readDSBuffer = readFramebuffer->getStencilbuffer();
3580 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3581 }
3582 }
3583
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003584 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003585 {
3586 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3587 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3588 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003589
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003590 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3591 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003592 {
3593 return error(GL_INVALID_OPERATION);
3594 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003595 }
3596
3597 if (blitRenderTarget || blitDepthStencil)
3598 {
3599 egl::Display *display = getDisplay();
3600 display->endScene();
3601
3602 if (blitRenderTarget)
3603 {
3604 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3605 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3606
3607 if (FAILED(result))
3608 {
3609 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3610 return;
3611 }
3612 }
3613
3614 if (blitDepthStencil)
3615 {
3616 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3617
3618 if (FAILED(result))
3619 {
3620 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3621 return;
3622 }
3623 }
3624 }
3625}
3626
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003627VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3628{
3629 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3630 {
3631 mVertexDeclCache[i].vertexDeclaration = NULL;
3632 mVertexDeclCache[i].lruCount = 0;
3633 }
3634}
3635
3636VertexDeclarationCache::~VertexDeclarationCache()
3637{
3638 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3639 {
3640 if (mVertexDeclCache[i].vertexDeclaration)
3641 {
3642 mVertexDeclCache[i].vertexDeclaration->Release();
3643 }
3644 }
3645}
3646
3647GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3648{
3649 IDirect3DDevice9 *device = getDevice();
3650
3651 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3652 D3DVERTEXELEMENT9 *element = &elements[0];
3653
3654 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3655 {
3656 if (attributes[i].active)
3657 {
3658 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3659
3660 element->Stream = i;
3661 element->Offset = 0;
3662 element->Type = attributes[i].type;
3663 element->Method = D3DDECLMETHOD_DEFAULT;
3664 element->Usage = D3DDECLUSAGE_TEXCOORD;
3665 element->UsageIndex = program->getSemanticIndex(i);
3666 element++;
3667 }
3668 }
3669
3670 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3671 *(element++) = end;
3672
3673 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3674 {
3675 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3676 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3677 {
3678 entry->lruCount = ++mMaxLru;
3679 device->SetVertexDeclaration(entry->vertexDeclaration);
3680
3681 return GL_NO_ERROR;
3682 }
3683 }
3684
3685 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3686
3687 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3688 {
3689 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3690 {
3691 lastCache = &mVertexDeclCache[i];
3692 }
3693 }
3694
3695 if (lastCache->vertexDeclaration != NULL)
3696 {
3697 lastCache->vertexDeclaration->Release();
3698 lastCache->vertexDeclaration = NULL;
3699 }
3700
3701 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3702 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3703 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3704 lastCache->lruCount = ++mMaxLru;
3705
3706 return GL_NO_ERROR;
3707}
3708
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003709}
3710
3711extern "C"
3712{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003713gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003714{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003715 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003716}
3717
3718void glDestroyContext(gl::Context *context)
3719{
3720 delete context;
3721
3722 if (context == gl::getContext())
3723 {
3724 gl::makeCurrent(NULL, NULL, NULL);
3725 }
3726}
3727
3728void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3729{
3730 gl::makeCurrent(context, display, surface);
3731}
3732
3733gl::Context *glGetCurrentContext()
3734{
3735 return gl::getContext();
3736}
3737}