blob: e3878aee0ff0a38ba6c1b28210f92b287194c45f [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
gman@chromium.org50c526d2011-08-10 05:19:44 +0000159 mSupportsDXT1Textures = false;
160 mSupportsDXT3Textures = false;
161 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000162 mSupportsEventQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000163 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000164 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000165 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000166 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167}
168
169Context::~Context()
170{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000171 if (mState.currentProgram != 0)
172 {
173 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
174 if (programObject)
175 {
176 programObject->release();
177 }
178 mState.currentProgram = 0;
179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000181 while (!mFramebufferMap.empty())
182 {
183 deleteFramebuffer(mFramebufferMap.begin()->first);
184 }
185
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000186 while (!mFenceMap.empty())
187 {
188 deleteFence(mFenceMap.begin()->first);
189 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000190
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000191 while (!mMultiSampleSupport.empty())
192 {
193 delete [] mMultiSampleSupport.begin()->second;
194 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
195 }
196
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000197 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000198 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000199 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000200 {
201 mState.samplerTexture[type][sampler].set(NULL);
202 }
203 }
204
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000205 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000206 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000207 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000208 }
209
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
211 {
212 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
213 }
214
215 mState.arrayBuffer.set(NULL);
216 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000217 mState.renderbuffer.set(NULL);
218
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000219 mTexture2DZero.set(NULL);
220 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000222 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000223 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000224 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000225 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000226
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000227 if (mMaskedClearSavedState)
228 {
229 mMaskedClearSavedState->Release();
230 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000231
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000232 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233}
234
235void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
236{
237 IDirect3DDevice9 *device = display->getDevice();
238
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000239 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000240 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000241 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000242
daniel@transgaming.com83921382011-01-08 05:46:00 +0000243 mVertexDataManager = new VertexDataManager(this, device);
244 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000245 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000246
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000247 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +0000248 mSupportsVertexTexture = display->getVertexTextureSupport();
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000249 mSupportsNonPower2Texture = display->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000250
251 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
252 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
253 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
254 mMaxRenderbufferDimension = mMaxTextureDimension;
255 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
256 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
257 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
258
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000259 const D3DFORMAT renderBufferFormats[] =
260 {
261 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000262 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000263 D3DFMT_R5G6B5,
264 D3DFMT_D24S8
265 };
266
267 int max = 0;
268 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
269 {
270 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
271 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
272 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
273
274 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
275 {
276 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
277 {
278 max = j;
279 }
280 }
281 }
282
283 mMaxSupportedSamples = max;
284
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000285 mSupportsEventQueries = display->getEventQuerySupport();
gman@chromium.org50c526d2011-08-10 05:19:44 +0000286 mSupportsDXT1Textures = display->getDXT1TextureSupport();
287 mSupportsDXT3Textures = display->getDXT3TextureSupport();
288 mSupportsDXT5Textures = display->getDXT5TextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000289 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
290 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000291 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
292 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000293
daniel@transgaming.com83921382011-01-08 05:46:00 +0000294 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
295
gman@chromium.org50c526d2011-08-10 05:19:44 +0000296 mNumCompressedTextureFormats = 0;
297 if (supportsDXT1Textures())
298 {
299 mNumCompressedTextureFormats += 2;
300 }
301 if (supportsDXT3Textures())
302 {
303 mNumCompressedTextureFormats += 1;
304 }
305 if (supportsDXT5Textures())
306 {
307 mNumCompressedTextureFormats += 1;
308 }
309
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000310 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000311 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000312
313 mState.viewportX = 0;
314 mState.viewportY = 0;
315 mState.viewportWidth = surface->getWidth();
316 mState.viewportHeight = surface->getHeight();
317
318 mState.scissorX = 0;
319 mState.scissorY = 0;
320 mState.scissorWidth = surface->getWidth();
321 mState.scissorHeight = surface->getHeight();
322
323 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000324 }
325
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
327 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000328 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000331 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000332 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
334 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000336 if (defaultRenderTarget)
337 {
338 defaultRenderTarget->Release();
339 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000341 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000343 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000345
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000346 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347}
348
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000349// 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 +0000350void Context::markAllStateDirty()
351{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000352 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
353 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000354 mAppliedTextureSerialPS[t] = 0;
355 }
356
357 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
358 {
359 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000360 }
361
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000362 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000363 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000364 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000365 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000366 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000367 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000368 mViewportInitialized = false;
369 mRenderTargetDescInitialized = false;
370
371 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000372
373 mClearStateDirty = true;
374 mCullStateDirty = true;
375 mDepthStateDirty = true;
376 mMaskStateDirty = true;
377 mBlendStateDirty = true;
378 mStencilStateDirty = true;
379 mPolygonOffsetStateDirty = true;
380 mScissorStateDirty = true;
381 mSampleStateDirty = true;
382 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000383 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000384 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000385 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000386}
387
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388void Context::setClearColor(float red, float green, float blue, float alpha)
389{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000390 mState.colorClearValue.red = red;
391 mState.colorClearValue.green = green;
392 mState.colorClearValue.blue = blue;
393 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394}
395
396void Context::setClearDepth(float depth)
397{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000398 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399}
400
401void Context::setClearStencil(int stencil)
402{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000403 mState.stencilClearValue = stencil;
404}
405
406void Context::setCullFace(bool enabled)
407{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000408 if (mState.cullFace != enabled)
409 {
410 mState.cullFace = enabled;
411 mCullStateDirty = true;
412 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000413}
414
415bool Context::isCullFaceEnabled() const
416{
417 return mState.cullFace;
418}
419
420void Context::setCullMode(GLenum mode)
421{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000422 if (mState.cullMode != mode)
423 {
424 mState.cullMode = mode;
425 mCullStateDirty = true;
426 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000427}
428
429void Context::setFrontFace(GLenum front)
430{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000431 if (mState.frontFace != front)
432 {
433 mState.frontFace = front;
434 mFrontFaceDirty = true;
435 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000436}
437
438void Context::setDepthTest(bool enabled)
439{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000440 if (mState.depthTest != enabled)
441 {
442 mState.depthTest = enabled;
443 mDepthStateDirty = true;
444 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000445}
446
447bool Context::isDepthTestEnabled() const
448{
449 return mState.depthTest;
450}
451
452void Context::setDepthFunc(GLenum depthFunc)
453{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000454 if (mState.depthFunc != depthFunc)
455 {
456 mState.depthFunc = depthFunc;
457 mDepthStateDirty = true;
458 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000459}
460
461void Context::setDepthRange(float zNear, float zFar)
462{
463 mState.zNear = zNear;
464 mState.zFar = zFar;
465}
466
467void Context::setBlend(bool enabled)
468{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000469 if (mState.blend != enabled)
470 {
471 mState.blend = enabled;
472 mBlendStateDirty = true;
473 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000474}
475
476bool Context::isBlendEnabled() const
477{
478 return mState.blend;
479}
480
481void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
482{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000483 if (mState.sourceBlendRGB != sourceRGB ||
484 mState.sourceBlendAlpha != sourceAlpha ||
485 mState.destBlendRGB != destRGB ||
486 mState.destBlendAlpha != destAlpha)
487 {
488 mState.sourceBlendRGB = sourceRGB;
489 mState.destBlendRGB = destRGB;
490 mState.sourceBlendAlpha = sourceAlpha;
491 mState.destBlendAlpha = destAlpha;
492 mBlendStateDirty = true;
493 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000494}
495
496void Context::setBlendColor(float red, float green, float blue, float alpha)
497{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000498 if (mState.blendColor.red != red ||
499 mState.blendColor.green != green ||
500 mState.blendColor.blue != blue ||
501 mState.blendColor.alpha != alpha)
502 {
503 mState.blendColor.red = red;
504 mState.blendColor.green = green;
505 mState.blendColor.blue = blue;
506 mState.blendColor.alpha = alpha;
507 mBlendStateDirty = true;
508 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000509}
510
511void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
512{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000513 if (mState.blendEquationRGB != rgbEquation ||
514 mState.blendEquationAlpha != alphaEquation)
515 {
516 mState.blendEquationRGB = rgbEquation;
517 mState.blendEquationAlpha = alphaEquation;
518 mBlendStateDirty = true;
519 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000520}
521
522void Context::setStencilTest(bool enabled)
523{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000524 if (mState.stencilTest != enabled)
525 {
526 mState.stencilTest = enabled;
527 mStencilStateDirty = true;
528 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000529}
530
531bool Context::isStencilTestEnabled() const
532{
533 return mState.stencilTest;
534}
535
536void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
537{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000538 if (mState.stencilFunc != stencilFunc ||
539 mState.stencilRef != stencilRef ||
540 mState.stencilMask != stencilMask)
541 {
542 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000543 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000544 mState.stencilMask = stencilMask;
545 mStencilStateDirty = true;
546 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000547}
548
549void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
550{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000551 if (mState.stencilBackFunc != stencilBackFunc ||
552 mState.stencilBackRef != stencilBackRef ||
553 mState.stencilBackMask != stencilBackMask)
554 {
555 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000556 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000557 mState.stencilBackMask = stencilBackMask;
558 mStencilStateDirty = true;
559 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000560}
561
562void Context::setStencilWritemask(GLuint stencilWritemask)
563{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000564 if (mState.stencilWritemask != stencilWritemask)
565 {
566 mState.stencilWritemask = stencilWritemask;
567 mStencilStateDirty = true;
568 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000569}
570
571void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
572{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000573 if (mState.stencilBackWritemask != stencilBackWritemask)
574 {
575 mState.stencilBackWritemask = stencilBackWritemask;
576 mStencilStateDirty = true;
577 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000578}
579
580void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
581{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000582 if (mState.stencilFail != stencilFail ||
583 mState.stencilPassDepthFail != stencilPassDepthFail ||
584 mState.stencilPassDepthPass != stencilPassDepthPass)
585 {
586 mState.stencilFail = stencilFail;
587 mState.stencilPassDepthFail = stencilPassDepthFail;
588 mState.stencilPassDepthPass = stencilPassDepthPass;
589 mStencilStateDirty = true;
590 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.stencilBackFail != stencilBackFail ||
596 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
597 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
598 {
599 mState.stencilBackFail = stencilBackFail;
600 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
601 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
602 mStencilStateDirty = true;
603 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000604}
605
606void Context::setPolygonOffsetFill(bool enabled)
607{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000608 if (mState.polygonOffsetFill != enabled)
609 {
610 mState.polygonOffsetFill = enabled;
611 mPolygonOffsetStateDirty = true;
612 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000613}
614
615bool Context::isPolygonOffsetFillEnabled() const
616{
617 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000618
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000619}
620
621void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
622{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000623 if (mState.polygonOffsetFactor != factor ||
624 mState.polygonOffsetUnits != units)
625 {
626 mState.polygonOffsetFactor = factor;
627 mState.polygonOffsetUnits = units;
628 mPolygonOffsetStateDirty = true;
629 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000630}
631
632void Context::setSampleAlphaToCoverage(bool enabled)
633{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000634 if (mState.sampleAlphaToCoverage != enabled)
635 {
636 mState.sampleAlphaToCoverage = enabled;
637 mSampleStateDirty = true;
638 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000639}
640
641bool Context::isSampleAlphaToCoverageEnabled() const
642{
643 return mState.sampleAlphaToCoverage;
644}
645
646void Context::setSampleCoverage(bool enabled)
647{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000648 if (mState.sampleCoverage != enabled)
649 {
650 mState.sampleCoverage = enabled;
651 mSampleStateDirty = true;
652 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000653}
654
655bool Context::isSampleCoverageEnabled() const
656{
657 return mState.sampleCoverage;
658}
659
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000660void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000661{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000662 if (mState.sampleCoverageValue != value ||
663 mState.sampleCoverageInvert != invert)
664 {
665 mState.sampleCoverageValue = value;
666 mState.sampleCoverageInvert = invert;
667 mSampleStateDirty = true;
668 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000669}
670
671void Context::setScissorTest(bool enabled)
672{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000673 if (mState.scissorTest != enabled)
674 {
675 mState.scissorTest = enabled;
676 mScissorStateDirty = true;
677 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000678}
679
680bool Context::isScissorTestEnabled() const
681{
682 return mState.scissorTest;
683}
684
685void Context::setDither(bool enabled)
686{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000687 if (mState.dither != enabled)
688 {
689 mState.dither = enabled;
690 mDitherStateDirty = true;
691 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000692}
693
694bool Context::isDitherEnabled() const
695{
696 return mState.dither;
697}
698
699void Context::setLineWidth(GLfloat width)
700{
701 mState.lineWidth = width;
702}
703
704void Context::setGenerateMipmapHint(GLenum hint)
705{
706 mState.generateMipmapHint = hint;
707}
708
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000709void Context::setFragmentShaderDerivativeHint(GLenum hint)
710{
711 mState.fragmentShaderDerivativeHint = hint;
712 // TODO: Propagate the hint to shader translator so we can write
713 // ddx, ddx_coarse, or ddx_fine depending on the hint.
714 // Ignore for now. It is valid for implementations to ignore hint.
715}
716
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000717void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
718{
719 mState.viewportX = x;
720 mState.viewportY = y;
721 mState.viewportWidth = width;
722 mState.viewportHeight = height;
723}
724
725void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
726{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000727 if (mState.scissorX != x || mState.scissorY != y ||
728 mState.scissorWidth != width || mState.scissorHeight != height)
729 {
730 mState.scissorX = x;
731 mState.scissorY = y;
732 mState.scissorWidth = width;
733 mState.scissorHeight = height;
734 mScissorStateDirty = true;
735 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000736}
737
738void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
739{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000740 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
741 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
742 {
743 mState.colorMaskRed = red;
744 mState.colorMaskGreen = green;
745 mState.colorMaskBlue = blue;
746 mState.colorMaskAlpha = alpha;
747 mMaskStateDirty = true;
748 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749}
750
751void Context::setDepthMask(bool mask)
752{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000753 if (mState.depthMask != mask)
754 {
755 mState.depthMask = mask;
756 mMaskStateDirty = true;
757 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000758}
759
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000760void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000761{
762 mState.activeSampler = active;
763}
764
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000765GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000766{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000767 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000768}
769
770GLuint Context::getDrawFramebufferHandle() const
771{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000772 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000773}
774
775GLuint Context::getRenderbufferHandle() const
776{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000777 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000778}
779
780GLuint Context::getArrayBufferHandle() const
781{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000782 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000783}
784
daniel@transgaming.com83921382011-01-08 05:46:00 +0000785void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000786{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000787 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000788}
789
daniel@transgaming.com83921382011-01-08 05:46:00 +0000790const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000791{
792 return mState.vertexAttribute[attribNum];
793}
794
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000795void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000796 GLsizei stride, const void *pointer)
797{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000798 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000799 mState.vertexAttribute[attribNum].mSize = size;
800 mState.vertexAttribute[attribNum].mType = type;
801 mState.vertexAttribute[attribNum].mNormalized = normalized;
802 mState.vertexAttribute[attribNum].mStride = stride;
803 mState.vertexAttribute[attribNum].mPointer = pointer;
804}
805
806const void *Context::getVertexAttribPointer(unsigned int attribNum) const
807{
808 return mState.vertexAttribute[attribNum].mPointer;
809}
810
daniel@transgaming.com83921382011-01-08 05:46:00 +0000811const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000812{
813 return mState.vertexAttribute;
814}
815
816void Context::setPackAlignment(GLint alignment)
817{
818 mState.packAlignment = alignment;
819}
820
821GLint Context::getPackAlignment() const
822{
823 return mState.packAlignment;
824}
825
826void Context::setUnpackAlignment(GLint alignment)
827{
828 mState.unpackAlignment = alignment;
829}
830
831GLint Context::getUnpackAlignment() const
832{
833 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834}
835
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836GLuint Context::createBuffer()
837{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000838 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839}
840
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841GLuint Context::createProgram()
842{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000843 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844}
845
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000846GLuint Context::createShader(GLenum type)
847{
848 return mResourceManager->createShader(type);
849}
850
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851GLuint Context::createTexture()
852{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000853 return mResourceManager->createTexture();
854}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000856GLuint Context::createRenderbuffer()
857{
858 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000861// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862GLuint Context::createFramebuffer()
863{
benvanik@google.com1a233342011-04-28 19:44:39 +0000864 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000865
866 mFramebufferMap[handle] = NULL;
867
868 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869}
870
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000871GLuint Context::createFence()
872{
benvanik@google.com1a233342011-04-28 19:44:39 +0000873 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000874
875 mFenceMap[handle] = new Fence;
876
877 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000878}
879
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880void Context::deleteBuffer(GLuint buffer)
881{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000882 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883 {
884 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000886
887 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888}
889
890void Context::deleteShader(GLuint shader)
891{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000892 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893}
894
895void Context::deleteProgram(GLuint program)
896{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000897 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000898 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
900
901void Context::deleteTexture(GLuint texture)
902{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000903 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904 {
905 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907
908 mResourceManager->deleteTexture(texture);
909}
910
911void Context::deleteRenderbuffer(GLuint renderbuffer)
912{
913 if (mResourceManager->getRenderbuffer(renderbuffer))
914 {
915 detachRenderbuffer(renderbuffer);
916 }
917
918 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
921void Context::deleteFramebuffer(GLuint framebuffer)
922{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000923 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
924
925 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926 {
927 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000928
benvanik@google.com1a233342011-04-28 19:44:39 +0000929 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000930 delete framebufferObject->second;
931 mFramebufferMap.erase(framebufferObject);
932 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000934
935void Context::deleteFence(GLuint fence)
936{
937 FenceMap::iterator fenceObject = mFenceMap.find(fence);
938
939 if (fenceObject != mFenceMap.end())
940 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000941 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000942 delete fenceObject->second;
943 mFenceMap.erase(fenceObject);
944 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000945}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000947Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000949 return mResourceManager->getBuffer(handle);
950}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000952Shader *Context::getShader(GLuint handle)
953{
954 return mResourceManager->getShader(handle);
955}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000957Program *Context::getProgram(GLuint handle)
958{
959 return mResourceManager->getProgram(handle);
960}
961
962Texture *Context::getTexture(GLuint handle)
963{
964 return mResourceManager->getTexture(handle);
965}
966
967Renderbuffer *Context::getRenderbuffer(GLuint handle)
968{
969 return mResourceManager->getRenderbuffer(handle);
970}
971
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000972Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000973{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000974 return getFramebuffer(mState.readFramebuffer);
975}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000976
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000977Framebuffer *Context::getDrawFramebuffer()
978{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +0000979 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980}
981
982void Context::bindArrayBuffer(unsigned int buffer)
983{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000984 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000986 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987}
988
989void Context::bindElementArrayBuffer(unsigned int buffer)
990{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000991 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000993 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994}
995
996void Context::bindTexture2D(GLuint texture)
997{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000998 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001000 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001}
1002
1003void Context::bindTextureCubeMap(GLuint texture)
1004{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001005 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001007 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008}
1009
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001010void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001012 if (!getFramebuffer(framebuffer))
1013 {
1014 mFramebufferMap[framebuffer] = new Framebuffer();
1015 }
1016
1017 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001018}
1019
1020void Context::bindDrawFramebuffer(GLuint framebuffer)
1021{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001022 if (!getFramebuffer(framebuffer))
1023 {
1024 mFramebufferMap[framebuffer] = new Framebuffer();
1025 }
1026
1027 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001028
1029 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030}
1031
1032void Context::bindRenderbuffer(GLuint renderbuffer)
1033{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001034 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1035
1036 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037}
1038
1039void Context::useProgram(GLuint program)
1040{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001041 GLuint priorProgram = mState.currentProgram;
1042 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001043
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001044 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001046 Program *newProgram = mResourceManager->getProgram(program);
1047 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001048 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001049 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001050
1051 if (newProgram)
1052 {
1053 newProgram->addRef();
1054 }
1055
1056 if (oldProgram)
1057 {
1058 oldProgram->release();
1059 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061}
1062
1063void Context::setFramebufferZero(Framebuffer *buffer)
1064{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001065 delete mFramebufferMap[0];
1066 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001067 if (mState.drawFramebuffer == 0)
1068 {
1069 mBoundDrawFramebuffer = buffer;
1070 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071}
1072
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001073void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001075 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1076 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001079Framebuffer *Context::getFramebuffer(unsigned int handle)
1080{
1081 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1082
1083 if (framebuffer == mFramebufferMap.end())
1084 {
1085 return NULL;
1086 }
1087 else
1088 {
1089 return framebuffer->second;
1090 }
1091}
1092
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001093Fence *Context::getFence(unsigned int handle)
1094{
1095 FenceMap::iterator fence = mFenceMap.find(handle);
1096
1097 if (fence == mFenceMap.end())
1098 {
1099 return NULL;
1100 }
1101 else
1102 {
1103 return fence->second;
1104 }
1105}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001106
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107Buffer *Context::getArrayBuffer()
1108{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001109 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110}
1111
1112Buffer *Context::getElementArrayBuffer()
1113{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001114 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115}
1116
1117Program *Context::getCurrentProgram()
1118{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001119 if (!mCachedCurrentProgram)
1120 {
1121 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1122 }
1123 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124}
1125
1126Texture2D *Context::getTexture2D()
1127{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001128 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129}
1130
1131TextureCubeMap *Context::getTextureCubeMap()
1132{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001133 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134}
1135
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001136Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001137{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001138 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001139
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001140 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001141 {
1142 switch (type)
1143 {
1144 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001145 case TEXTURE_2D: return mTexture2DZero.get();
1146 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001147 }
1148 }
1149
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001150 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151}
1152
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001153bool Context::getBooleanv(GLenum pname, GLboolean *params)
1154{
1155 switch (pname)
1156 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001157 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1158 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1159 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001160 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 params[0] = mState.colorMaskRed;
1162 params[1] = mState.colorMaskGreen;
1163 params[2] = mState.colorMaskBlue;
1164 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001165 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001166 case GL_CULL_FACE: *params = mState.cullFace; break;
1167 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1168 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1169 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1170 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1171 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1172 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1173 case GL_BLEND: *params = mState.blend; break;
1174 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001175 default:
1176 return false;
1177 }
1178
1179 return true;
1180}
1181
1182bool Context::getFloatv(GLenum pname, GLfloat *params)
1183{
1184 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1185 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1186 // GetIntegerv as its native query function. As it would require conversion in any
1187 // case, this should make no difference to the calling application.
1188 switch (pname)
1189 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001190 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1191 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1192 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1193 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1194 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001195 case GL_ALIASED_LINE_WIDTH_RANGE:
1196 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1197 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1198 break;
1199 case GL_ALIASED_POINT_SIZE_RANGE:
1200 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001201 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 +00001202 break;
1203 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001204 params[0] = mState.zNear;
1205 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001206 break;
1207 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001208 params[0] = mState.colorClearValue.red;
1209 params[1] = mState.colorClearValue.green;
1210 params[2] = mState.colorClearValue.blue;
1211 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001212 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001213 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001214 params[0] = mState.blendColor.red;
1215 params[1] = mState.blendColor.green;
1216 params[2] = mState.blendColor.blue;
1217 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001218 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001219 default:
1220 return false;
1221 }
1222
1223 return true;
1224}
1225
1226bool Context::getIntegerv(GLenum pname, GLint *params)
1227{
1228 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1229 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1230 // GetIntegerv as its native query function. As it would require conversion in any
1231 // case, this should make no difference to the calling application. You may find it in
1232 // Context::getFloatv.
1233 switch (pname)
1234 {
1235 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1236 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001237 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001238 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1239 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001240 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001241 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001242 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001243 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001244 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001245 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1246 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001247 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1248 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1249 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001250 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001251 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1252 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1253 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1254 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001255 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001256 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1257 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1258 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1259 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1260 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1261 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1262 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1263 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1264 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1265 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1266 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1267 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1268 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1269 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1270 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1271 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1272 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1273 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1274 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1275 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1276 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1277 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1278 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1279 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001280 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1281 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001282 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001283 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001284 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001285 case GL_MAX_SAMPLES_ANGLE:
1286 {
1287 GLsizei maxSamples = getMaxSupportedSamples();
1288 if (maxSamples != 0)
1289 {
1290 *params = maxSamples;
1291 }
1292 else
1293 {
1294 return false;
1295 }
1296
1297 break;
1298 }
1299 case GL_SAMPLE_BUFFERS:
1300 case GL_SAMPLES:
1301 {
1302 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1303 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1304 {
1305 switch (pname)
1306 {
1307 case GL_SAMPLE_BUFFERS:
1308 if (framebuffer->getSamples() != 0)
1309 {
1310 *params = 1;
1311 }
1312 else
1313 {
1314 *params = 0;
1315 }
1316 break;
1317 case GL_SAMPLES:
1318 *params = framebuffer->getSamples();
1319 break;
1320 }
1321 }
1322 else
1323 {
1324 *params = 0;
1325 }
1326 }
1327 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001328 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1329 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1330 case GL_MAX_VIEWPORT_DIMS:
1331 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001332 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001333 params[0] = maxDimension;
1334 params[1] = maxDimension;
1335 }
1336 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001337 case GL_COMPRESSED_TEXTURE_FORMATS:
1338 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001339 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001340 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001341 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1342 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1343 }
1344 if (supportsDXT3Textures())
1345 {
1346 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1347 }
1348 if (supportsDXT5Textures())
1349 {
1350 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001351 }
1352 }
1353 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001354 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001355 params[0] = mState.viewportX;
1356 params[1] = mState.viewportY;
1357 params[2] = mState.viewportWidth;
1358 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359 break;
1360 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001361 params[0] = mState.scissorX;
1362 params[1] = mState.scissorY;
1363 params[2] = mState.scissorWidth;
1364 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001365 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001366 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1367 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001368 case GL_RED_BITS:
1369 case GL_GREEN_BITS:
1370 case GL_BLUE_BITS:
1371 case GL_ALPHA_BITS:
1372 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001373 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001374 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1375
1376 if (colorbuffer)
1377 {
1378 switch (pname)
1379 {
1380 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1381 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1382 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1383 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1384 }
1385 }
1386 else
1387 {
1388 *params = 0;
1389 }
1390 }
1391 break;
1392 case GL_DEPTH_BITS:
1393 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001394 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001395 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001396
1397 if (depthbuffer)
1398 {
1399 *params = depthbuffer->getDepthSize();
1400 }
1401 else
1402 {
1403 *params = 0;
1404 }
1405 }
1406 break;
1407 case GL_STENCIL_BITS:
1408 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001409 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001410 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001411
1412 if (stencilbuffer)
1413 {
1414 *params = stencilbuffer->getStencilSize();
1415 }
1416 else
1417 {
1418 *params = 0;
1419 }
1420 }
1421 break;
1422 case GL_TEXTURE_BINDING_2D:
1423 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001424 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001425 {
1426 error(GL_INVALID_OPERATION);
1427 return false;
1428 }
1429
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001430 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001431 }
1432 break;
1433 case GL_TEXTURE_BINDING_CUBE_MAP:
1434 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001435 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001436 {
1437 error(GL_INVALID_OPERATION);
1438 return false;
1439 }
1440
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001441 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001442 }
1443 break;
1444 default:
1445 return false;
1446 }
1447
1448 return true;
1449}
1450
1451bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1452{
1453 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1454 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1455 // to the fact that it is stored internally as a float, and so would require conversion
1456 // if returned from Context::getIntegerv. Since this conversion is already implemented
1457 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1458 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1459 // application.
1460 switch (pname)
1461 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001462 case GL_COMPRESSED_TEXTURE_FORMATS:
1463 {
1464 *type = GL_INT;
1465 *numParams = mNumCompressedTextureFormats;
1466 }
1467 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001468 case GL_SHADER_BINARY_FORMATS:
1469 {
1470 *type = GL_INT;
1471 *numParams = 0;
1472 }
1473 break;
1474 case GL_MAX_VERTEX_ATTRIBS:
1475 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1476 case GL_MAX_VARYING_VECTORS:
1477 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1478 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1479 case GL_MAX_TEXTURE_IMAGE_UNITS:
1480 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1481 case GL_MAX_RENDERBUFFER_SIZE:
1482 case GL_NUM_SHADER_BINARY_FORMATS:
1483 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1484 case GL_ARRAY_BUFFER_BINDING:
1485 case GL_FRAMEBUFFER_BINDING:
1486 case GL_RENDERBUFFER_BINDING:
1487 case GL_CURRENT_PROGRAM:
1488 case GL_PACK_ALIGNMENT:
1489 case GL_UNPACK_ALIGNMENT:
1490 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001491 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001492 case GL_RED_BITS:
1493 case GL_GREEN_BITS:
1494 case GL_BLUE_BITS:
1495 case GL_ALPHA_BITS:
1496 case GL_DEPTH_BITS:
1497 case GL_STENCIL_BITS:
1498 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1499 case GL_CULL_FACE_MODE:
1500 case GL_FRONT_FACE:
1501 case GL_ACTIVE_TEXTURE:
1502 case GL_STENCIL_FUNC:
1503 case GL_STENCIL_VALUE_MASK:
1504 case GL_STENCIL_REF:
1505 case GL_STENCIL_FAIL:
1506 case GL_STENCIL_PASS_DEPTH_FAIL:
1507 case GL_STENCIL_PASS_DEPTH_PASS:
1508 case GL_STENCIL_BACK_FUNC:
1509 case GL_STENCIL_BACK_VALUE_MASK:
1510 case GL_STENCIL_BACK_REF:
1511 case GL_STENCIL_BACK_FAIL:
1512 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1513 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1514 case GL_DEPTH_FUNC:
1515 case GL_BLEND_SRC_RGB:
1516 case GL_BLEND_SRC_ALPHA:
1517 case GL_BLEND_DST_RGB:
1518 case GL_BLEND_DST_ALPHA:
1519 case GL_BLEND_EQUATION_RGB:
1520 case GL_BLEND_EQUATION_ALPHA:
1521 case GL_STENCIL_WRITEMASK:
1522 case GL_STENCIL_BACK_WRITEMASK:
1523 case GL_STENCIL_CLEAR_VALUE:
1524 case GL_SUBPIXEL_BITS:
1525 case GL_MAX_TEXTURE_SIZE:
1526 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1527 case GL_SAMPLE_BUFFERS:
1528 case GL_SAMPLES:
1529 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1530 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1531 case GL_TEXTURE_BINDING_2D:
1532 case GL_TEXTURE_BINDING_CUBE_MAP:
1533 {
1534 *type = GL_INT;
1535 *numParams = 1;
1536 }
1537 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001538 case GL_MAX_SAMPLES_ANGLE:
1539 {
1540 if (getMaxSupportedSamples() != 0)
1541 {
1542 *type = GL_INT;
1543 *numParams = 1;
1544 }
1545 else
1546 {
1547 return false;
1548 }
1549 }
1550 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001551 case GL_MAX_VIEWPORT_DIMS:
1552 {
1553 *type = GL_INT;
1554 *numParams = 2;
1555 }
1556 break;
1557 case GL_VIEWPORT:
1558 case GL_SCISSOR_BOX:
1559 {
1560 *type = GL_INT;
1561 *numParams = 4;
1562 }
1563 break;
1564 case GL_SHADER_COMPILER:
1565 case GL_SAMPLE_COVERAGE_INVERT:
1566 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001567 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1568 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1569 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1570 case GL_SAMPLE_COVERAGE:
1571 case GL_SCISSOR_TEST:
1572 case GL_STENCIL_TEST:
1573 case GL_DEPTH_TEST:
1574 case GL_BLEND:
1575 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001576 {
1577 *type = GL_BOOL;
1578 *numParams = 1;
1579 }
1580 break;
1581 case GL_COLOR_WRITEMASK:
1582 {
1583 *type = GL_BOOL;
1584 *numParams = 4;
1585 }
1586 break;
1587 case GL_POLYGON_OFFSET_FACTOR:
1588 case GL_POLYGON_OFFSET_UNITS:
1589 case GL_SAMPLE_COVERAGE_VALUE:
1590 case GL_DEPTH_CLEAR_VALUE:
1591 case GL_LINE_WIDTH:
1592 {
1593 *type = GL_FLOAT;
1594 *numParams = 1;
1595 }
1596 break;
1597 case GL_ALIASED_LINE_WIDTH_RANGE:
1598 case GL_ALIASED_POINT_SIZE_RANGE:
1599 case GL_DEPTH_RANGE:
1600 {
1601 *type = GL_FLOAT;
1602 *numParams = 2;
1603 }
1604 break;
1605 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001606 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001607 {
1608 *type = GL_FLOAT;
1609 *numParams = 4;
1610 }
1611 break;
1612 default:
1613 return false;
1614 }
1615
1616 return true;
1617}
1618
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619// Applies the render target surface, depth stencil surface, viewport rectangle and
1620// scissor rectangle to the Direct3D 9 device
1621bool Context::applyRenderTarget(bool ignoreViewport)
1622{
1623 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001624
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001625 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626
1627 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1628 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001629 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630 }
1631
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001632 IDirect3DSurface9 *renderTarget = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001633 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001635 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001636 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1637 if (renderTargetSerial != mAppliedRenderTargetSerial)
1638 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001639 renderTarget = framebufferObject->getRenderTarget();
1640
1641 if (!renderTarget)
1642 {
1643 return false; // Context must be lost
1644 }
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001645 device->SetRenderTarget(0, renderTarget);
1646 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001647 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001648 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001649 }
1650
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001651 unsigned int depthbufferSerial = 0;
1652 unsigned int stencilbufferSerial = 0;
1653 if (framebufferObject->getDepthbufferType() != GL_NONE)
1654 {
1655 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001656 if (!depthStencil)
1657 {
1658 ERR("Depth stencil pointer unexpectedly null.");
1659 return false;
1660 }
1661
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001662 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1663 }
1664 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1665 {
1666 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001667 if (!depthStencil)
1668 {
1669 ERR("Depth stencil pointer unexpectedly null.");
1670 return false;
1671 }
1672
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001673 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1674 }
1675
1676 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001677 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001678 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001679 {
1680 device->SetDepthStencilSurface(depthStencil);
1681 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001682 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001683 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001684 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001686 if (!mRenderTargetDescInitialized || renderTargetChanged)
1687 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001688 if (!renderTarget)
1689 {
1690 renderTarget = framebufferObject->getRenderTarget();
1691
1692 if (!renderTarget)
1693 {
1694 return false; // Context must be lost
1695 }
1696 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001697 renderTarget->GetDesc(&mRenderTargetDesc);
1698 mRenderTargetDescInitialized = true;
1699 }
1700
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001701 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001702
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001703 float zNear = clamp01(mState.zNear);
1704 float zFar = clamp01(mState.zFar);
1705
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706 if (ignoreViewport)
1707 {
1708 viewport.X = 0;
1709 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001710 viewport.Width = mRenderTargetDesc.Width;
1711 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712 viewport.MinZ = 0.0f;
1713 viewport.MaxZ = 1.0f;
1714 }
1715 else
1716 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001717 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1718 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1719 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1720 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1721 viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height) - static_cast<LONG>(viewport.Y));
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001722 viewport.MinZ = zNear;
1723 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724 }
1725
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001726 if (viewport.Width <= 0 || viewport.Height <= 0)
1727 {
1728 return false; // Nothing to render
1729 }
1730
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001731 if (!mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
1732 {
1733 device->SetViewport(&viewport);
1734 mSetViewport = viewport;
1735 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001736 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001737 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001739 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 if (mState.scissorTest)
1742 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001743 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1744 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1745 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1746 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1747 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 device->SetScissorRect(&rect);
1749 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1750 }
1751 else
1752 {
1753 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1754 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001755
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 }
1758
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001759 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761 Program *programObject = getCurrentProgram();
1762
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001763 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001764 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001765 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001766
daniel@transgaming.com31754962010-11-28 02:02:52 +00001767 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001768 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1769 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1770 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001771 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001772
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001773 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001774 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001775 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001776
daniel@transgaming.com31754962010-11-28 02:02:52 +00001777 GLint depthRange = programObject->getDxDepthRangeLocation();
1778 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1779 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001780 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781 }
1782
1783 return true;
1784}
1785
1786// 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 +00001787void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001788{
1789 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001790 Program *programObject = getCurrentProgram();
1791
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001792 Framebuffer *framebufferObject = getDrawFramebuffer();
1793
1794 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1795
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001796 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001797 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001798 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001799
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001800 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001801 GLint alwaysFront = !isTriangleMode(drawMode);
1802 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1803
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001804 egl::Display *display = getDisplay();
1805 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
1806 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1807 // Apparently some ATI cards have a bug where a draw with a zero color
1808 // write mask can cause later draws to have incorrect results. Instead,
1809 // set a nonzero color write mask but modify the blend state so that no
1810 // drawing is done.
1811 // http://code.google.com/p/angleproject/issues/detail?id=169
1812
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001813 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001815 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001817 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818 }
1819 else
1820 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001821 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822 }
1823
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001824 mCullStateDirty = false;
1825 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001827 if (mDepthStateDirty)
1828 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001829 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001831 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1832 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833 }
1834 else
1835 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001836 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001838
1839 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840 }
1841
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001842 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1843 {
1844 mBlendStateDirty = true;
1845 mMaskStateDirty = true;
1846 }
1847
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001849 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001850 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001851 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001852 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1853
1854 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1855 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1856 {
1857 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1858 }
1859 else
1860 {
1861 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1862 unorm<8>(mState.blendColor.alpha),
1863 unorm<8>(mState.blendColor.alpha),
1864 unorm<8>(mState.blendColor.alpha)));
1865 }
1866
1867 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1868 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1869 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1870
1871 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1872 mState.destBlendRGB != mState.destBlendAlpha ||
1873 mState.blendEquationRGB != mState.blendEquationAlpha)
1874 {
1875 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1876
1877 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1878 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1879 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001880 }
1881 else
1882 {
1883 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1884 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001885 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001886 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001887 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001888 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001889 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001890
1891 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892 }
1893
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001894 if (mStencilStateDirty || mFrontFaceDirty)
1895 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001896 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001897 {
1898 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1899 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1900
1901 // FIXME: Unsupported by D3D9
1902 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1903 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1904 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1905 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1906 mState.stencilRef != mState.stencilBackRef ||
1907 mState.stencilMask != mState.stencilBackMask)
1908 {
1909 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1910 return error(GL_INVALID_OPERATION);
1911 }
1912
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001913 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001914 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001915 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1916
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001917 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1918 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001919 es2dx::ConvertComparison(mState.stencilFunc));
1920
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001921 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1922 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001923
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001924 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001925 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001926 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001927 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001928 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001929 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1930
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001931 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1932 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001933 es2dx::ConvertComparison(mState.stencilBackFunc));
1934
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001935 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1936 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001937
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001938 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001939 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001940 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001941 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001942 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001943 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1944 }
1945 else
1946 {
1947 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1948 }
1949
1950 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001951 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001952 }
1953
1954 if (mMaskStateDirty)
1955 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001956 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1957 mState.colorMaskBlue, mState.colorMaskAlpha);
1958 if (colorMask == 0 && !zeroColorMaskAllowed)
1959 {
1960 // Enable green channel, but set blending so nothing will be drawn.
1961 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1962 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1963
1964 device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1965 device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1966 device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
1967 }
1968 else
1969 {
1970 device->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
1971 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001972 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1973
1974 mMaskStateDirty = false;
1975 }
1976
1977 if (mPolygonOffsetStateDirty)
1978 {
1979 if (mState.polygonOffsetFill)
1980 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001981 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001982 if (depthbuffer)
1983 {
1984 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1985 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1986 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1987 }
1988 }
1989 else
1990 {
1991 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1992 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1993 }
1994
1995 mPolygonOffsetStateDirty = false;
1996 }
1997
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001998 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002000 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002001 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002002 FIXME("Sample alpha to coverage is unimplemented.");
2003 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002004
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002005 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
2006 if (mState.sampleCoverage)
2007 {
2008 unsigned int mask = 0;
2009 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002010 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002011 float threshold = 0.5f;
2012
2013 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002014 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002015 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002016
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002017 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002018 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002019 threshold += 1.0f;
2020 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002021 }
2022 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002023 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002024
2025 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002026 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002027 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002028 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002029
2030 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002031 }
2032 else
2033 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002034 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002035 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002036
2037 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038 }
2039
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002040 if (mDitherStateDirty)
2041 {
2042 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
2043
2044 mDitherStateDirty = false;
2045 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046}
2047
daniel@transgaming.com83921382011-01-08 05:46:00 +00002048GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002049{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002050 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002051
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002052 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002053 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002054 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002055 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002056 }
2057
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00002058 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002059}
2060
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002061// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002062GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002063{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002064 IDirect3DDevice9 *device = getDevice();
2065 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002066
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002067 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002068 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002069 if (indexInfo->serial != mAppliedIBSerial)
2070 {
2071 device->SetIndices(indexInfo->indexBuffer);
2072 mAppliedIBSerial = indexInfo->serial;
2073 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002074 }
2075
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002076 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077}
2078
2079// Applies the shaders and shader constants to the Direct3D 9 device
2080void Context::applyShaders()
2081{
2082 IDirect3DDevice9 *device = getDevice();
2083 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002084 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002085 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002086 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2087 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2088
2089 device->SetPixelShader(pixelShader);
2090 device->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002091 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002092 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002093 }
2094
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095 programObject->applyUniforms();
2096}
2097
2098// Applies the textures and sampler states to the Direct3D 9 device
2099void Context::applyTextures()
2100{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002101 applyTextures(SAMPLER_PIXEL);
2102
2103 if (mSupportsVertexTexture)
2104 {
2105 applyTextures(SAMPLER_VERTEX);
2106 }
2107}
2108
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002109// For each Direct3D 9 sampler of either the pixel or vertex stage,
2110// looks up the corresponding OpenGL texture image unit and texture type,
2111// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002112void Context::applyTextures(SamplerType type)
2113{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114 IDirect3DDevice9 *device = getDevice();
2115 Program *programObject = getCurrentProgram();
2116
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002117 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002118 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2119 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002120
2121 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002123 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002124 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002125
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002126 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002128 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002129
2130 Texture *texture = getSamplerTexture(textureUnit, textureType);
2131
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002132 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002133 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002134 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2135
2136 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002137 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002138 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002139 {
2140 GLenum wrapS = texture->getWrapS();
2141 GLenum wrapT = texture->getWrapT();
2142 GLenum minFilter = texture->getMinFilter();
2143 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002145 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2146 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002148 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002149 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2150 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002151 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2152 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002153 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002155 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002156 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002157 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002158 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002159 }
2160 else
2161 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002162 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002163 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002164
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002165 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002166 texture->resetDirty();
2167 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002169 else
2170 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002171 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002172 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002173 device->SetTexture(d3dSampler, NULL);
2174 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002175 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177 }
2178}
2179
2180void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2181{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002182 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002183
2184 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2185 {
2186 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2187 }
2188
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002189 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2190 {
2191 return error(GL_INVALID_OPERATION);
2192 }
2193
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002194 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002195
2196 if (!renderTarget)
2197 {
2198 return; // Context must be lost, return silently
2199 }
2200
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201 IDirect3DDevice9 *device = getDevice();
2202
2203 D3DSURFACE_DESC desc;
2204 renderTarget->GetDesc(&desc);
2205
2206 IDirect3DSurface9 *systemSurface;
2207 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2208
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002209 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002210 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002211 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002212 return error(GL_OUT_OF_MEMORY);
2213 }
2214
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002215 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2216 {
2217 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002218 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002219 }
2220
2221 result = device->GetRenderTargetData(renderTarget, systemSurface);
2222
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002223 if (FAILED(result))
2224 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002225 systemSurface->Release();
2226
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002227 switch (result)
2228 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002229 // It turns out that D3D will sometimes produce more error
2230 // codes than those documented.
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002231 case D3DERR_DRIVERINTERNALERROR:
2232 case D3DERR_DEVICELOST:
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002233 case D3DERR_DEVICEHUNG:
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002234 return error(GL_OUT_OF_MEMORY);
2235 default:
2236 UNREACHABLE();
2237 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002238 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002239 }
2240
2241 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002242 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2243 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2244 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2245 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2246 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247
2248 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2249
2250 if (FAILED(result))
2251 {
2252 UNREACHABLE();
2253 systemSurface->Release();
2254
2255 return; // No sensible error to generate
2256 }
2257
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002258 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002260 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002261 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002262 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002263
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 for (int j = 0; j < rect.bottom - rect.top; j++)
2265 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002266 if (desc.Format == D3DFMT_A8R8G8B8 &&
2267 format == GL_BGRA_EXT &&
2268 type == GL_UNSIGNED_BYTE)
2269 {
2270 // Fast path for EXT_read_format_bgra, given
2271 // an RGBA source buffer. Note that buffers with no
2272 // alpha go through the slow path below.
2273 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002274 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002275 (rect.right - rect.left) * 4);
2276 continue;
2277 }
2278
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 for (int i = 0; i < rect.right - rect.left; i++)
2280 {
2281 float r;
2282 float g;
2283 float b;
2284 float a;
2285
2286 switch (desc.Format)
2287 {
2288 case D3DFMT_R5G6B5:
2289 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002290 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291
2292 a = 1.0f;
2293 b = (rgb & 0x001F) * (1.0f / 0x001F);
2294 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2295 r = (rgb & 0xF800) * (1.0f / 0xF800);
2296 }
2297 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298 case D3DFMT_A1R5G5B5:
2299 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002300 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
2302 a = (argb & 0x8000) ? 1.0f : 0.0f;
2303 b = (argb & 0x001F) * (1.0f / 0x001F);
2304 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2305 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2306 }
2307 break;
2308 case D3DFMT_A8R8G8B8:
2309 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002310 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311
2312 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2313 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2314 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2315 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2316 }
2317 break;
2318 case D3DFMT_X8R8G8B8:
2319 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002320 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
2322 a = 1.0f;
2323 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2324 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2325 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2326 }
2327 break;
2328 case D3DFMT_A2R10G10B10:
2329 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002330 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331
2332 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2333 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2334 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2335 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2336 }
2337 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002338 case D3DFMT_A32B32G32R32F:
2339 {
2340 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002341 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2342 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2343 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2344 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002345 }
2346 break;
2347 case D3DFMT_A16B16G16R16F:
2348 {
2349 // float formats in D3D are stored rgba, rather than the other way round
2350 float abgr[4];
2351
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002352 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002353
2354 a = abgr[3];
2355 b = abgr[2];
2356 g = abgr[1];
2357 r = abgr[0];
2358 }
2359 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360 default:
2361 UNIMPLEMENTED(); // FIXME
2362 UNREACHABLE();
2363 }
2364
2365 switch (format)
2366 {
2367 case GL_RGBA:
2368 switch (type)
2369 {
2370 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002371 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2372 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2373 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2374 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 break;
2376 default: UNREACHABLE();
2377 }
2378 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002379 case GL_BGRA_EXT:
2380 switch (type)
2381 {
2382 case GL_UNSIGNED_BYTE:
2383 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2384 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2385 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2386 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2387 break;
2388 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2389 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2390 // this type is packed as follows:
2391 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2392 // --------------------------------------------------------------------------------
2393 // | 4th | 3rd | 2nd | 1st component |
2394 // --------------------------------------------------------------------------------
2395 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2396 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2397 ((unsigned short)(15 * a + 0.5f) << 12)|
2398 ((unsigned short)(15 * r + 0.5f) << 8) |
2399 ((unsigned short)(15 * g + 0.5f) << 4) |
2400 ((unsigned short)(15 * b + 0.5f) << 0);
2401 break;
2402 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2403 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2404 // this type is packed as follows:
2405 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2406 // --------------------------------------------------------------------------------
2407 // | 4th | 3rd | 2nd | 1st component |
2408 // --------------------------------------------------------------------------------
2409 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2410 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2411 ((unsigned short)( a + 0.5f) << 15) |
2412 ((unsigned short)(31 * r + 0.5f) << 10) |
2413 ((unsigned short)(31 * g + 0.5f) << 5) |
2414 ((unsigned short)(31 * b + 0.5f) << 0);
2415 break;
2416 default: UNREACHABLE();
2417 }
2418 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002419 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420 switch (type)
2421 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002422 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002423 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2424 ((unsigned short)(31 * b + 0.5f) << 0) |
2425 ((unsigned short)(63 * g + 0.5f) << 5) |
2426 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 break;
2428 default: UNREACHABLE();
2429 }
2430 break;
2431 default: UNREACHABLE();
2432 }
2433 }
2434 }
2435
2436 systemSurface->UnlockRect();
2437
2438 systemSurface->Release();
2439}
2440
2441void Context::clear(GLbitfield mask)
2442{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002443 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002444
2445 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2446 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002447 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002448 }
2449
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002450 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451 IDirect3DDevice9 *device = getDevice();
2452 DWORD flags = 0;
2453
2454 if (mask & GL_COLOR_BUFFER_BIT)
2455 {
2456 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002457
2458 if (framebufferObject->getColorbufferType() != GL_NONE)
2459 {
2460 flags |= D3DCLEAR_TARGET;
2461 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 }
2463
2464 if (mask & GL_DEPTH_BUFFER_BIT)
2465 {
2466 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002467 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468 {
2469 flags |= D3DCLEAR_ZBUFFER;
2470 }
2471 }
2472
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 GLuint stencilUnmasked = 0x0;
2474
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002475 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002478 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002480 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002481 if (!depthStencil)
2482 {
2483 ERR("Depth stencil pointer unexpectedly null.");
2484 return;
2485 }
2486
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002487 D3DSURFACE_DESC desc;
2488 depthStencil->GetDesc(&desc);
2489
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002490 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002491 stencilUnmasked = (0x1 << stencilSize) - 1;
2492
2493 if (stencilUnmasked != 0x0)
2494 {
2495 flags |= D3DCLEAR_STENCIL;
2496 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 }
2498 }
2499
2500 if (mask != 0)
2501 {
2502 return error(GL_INVALID_VALUE);
2503 }
2504
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002505 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2506 {
2507 return;
2508 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002509
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002510 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002511 unorm<8>(mState.colorClearValue.red),
2512 unorm<8>(mState.colorClearValue.green),
2513 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002514 float depth = clamp01(mState.depthClearValue);
2515 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516
2517 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2518
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002519 if (!renderTarget)
2520 {
2521 return; // Context must be lost, return silently
2522 }
2523
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524 D3DSURFACE_DESC desc;
2525 renderTarget->GetDesc(&desc);
2526
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002527 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002528
2529 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002530 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002532 !(mState.colorMaskRed && mState.colorMaskGreen &&
2533 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534
2535 if (needMaskedColorClear || needMaskedStencilClear)
2536 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002537 // State which is altered in all paths from this point to the clear call is saved.
2538 // State which is altered in only some paths will be flagged dirty in the case that
2539 // that path is taken.
2540 HRESULT hr;
2541 if (mMaskedClearSavedState == NULL)
2542 {
2543 hr = device->BeginStateBlock();
2544 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2545
2546 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2547 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2548 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2549 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2550 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2551 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2552 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2553 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2554 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2555 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2556 device->SetPixelShader(NULL);
2557 device->SetVertexShader(NULL);
2558 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
jbauman@chromium.org23c9e312011-09-21 22:16:44 +00002559 device->SetStreamSource(0, NULL, 0, 0);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002560 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2561 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2562 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2563 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2564 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2565 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2566 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002567
2568 hr = device->EndStateBlock(&mMaskedClearSavedState);
2569 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2570 }
2571
2572 ASSERT(mMaskedClearSavedState != NULL);
2573
2574 if (mMaskedClearSavedState != NULL)
2575 {
2576 hr = mMaskedClearSavedState->Capture();
2577 ASSERT(SUCCEEDED(hr));
2578 }
2579
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2581 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2582 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2583 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2584 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2585 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2586 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2587 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2588
2589 if (flags & D3DCLEAR_TARGET)
2590 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002591 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592 }
2593 else
2594 {
2595 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2596 }
2597
2598 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2599 {
2600 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2601 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2602 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2603 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002604 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002605 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002606 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2607 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002608 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609 }
2610 else
2611 {
2612 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2613 }
2614
2615 device->SetPixelShader(NULL);
2616 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002617 device->SetFVF(D3DFVF_XYZRHW);
2618 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2619 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2620 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2621 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2622 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2623 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2624 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002625
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002626 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2627 quad[0][0] = -0.5f;
2628 quad[0][1] = desc.Height - 0.5f;
2629 quad[0][2] = 0.0f;
2630 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002632 quad[1][0] = desc.Width - 0.5f;
2633 quad[1][1] = desc.Height - 0.5f;
2634 quad[1][2] = 0.0f;
2635 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002636
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002637 quad[2][0] = -0.5f;
2638 quad[2][1] = -0.5f;
2639 quad[2][2] = 0.0f;
2640 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002642 quad[3][0] = desc.Width - 0.5f;
2643 quad[3][1] = -0.5f;
2644 quad[3][2] = 0.0f;
2645 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002647 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002648 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649
2650 if (flags & D3DCLEAR_ZBUFFER)
2651 {
2652 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2653 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2654 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2655 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002656
2657 if (mMaskedClearSavedState != NULL)
2658 {
2659 mMaskedClearSavedState->Apply();
2660 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002662 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002663 {
2664 device->Clear(0, NULL, flags, color, depth, stencil);
2665 }
2666}
2667
2668void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2669{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002670 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671 {
2672 return error(GL_INVALID_OPERATION);
2673 }
2674
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002675 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676 IDirect3DDevice9 *device = getDevice();
2677 D3DPRIMITIVETYPE primitiveType;
2678 int primitiveCount;
2679
2680 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2681 return error(GL_INVALID_ENUM);
2682
2683 if (primitiveCount <= 0)
2684 {
2685 return;
2686 }
2687
2688 if (!applyRenderTarget(false))
2689 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002690 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002691 }
2692
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002693 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002694
daniel@transgaming.com83921382011-01-08 05:46:00 +00002695 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002696 if (err != GL_NO_ERROR)
2697 {
2698 return error(err);
2699 }
2700
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002701 applyShaders();
2702 applyTextures();
2703
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002704 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002705 {
2706 return error(GL_INVALID_OPERATION);
2707 }
2708
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002709 if (!cullSkipsDraw(mode))
2710 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002711 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002712
2713 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002714
2715 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2716 {
2717 drawClosingLine(first, first + count - 1);
2718 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002719 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720}
2721
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002722void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002723{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002724 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725 {
2726 return error(GL_INVALID_OPERATION);
2727 }
2728
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002729 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730 {
2731 return error(GL_INVALID_OPERATION);
2732 }
2733
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002734 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735 IDirect3DDevice9 *device = getDevice();
2736 D3DPRIMITIVETYPE primitiveType;
2737 int primitiveCount;
2738
2739 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2740 return error(GL_INVALID_ENUM);
2741
2742 if (primitiveCount <= 0)
2743 {
2744 return;
2745 }
2746
2747 if (!applyRenderTarget(false))
2748 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002749 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002750 }
2751
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002752 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002753
2754 TranslatedIndexData indexInfo;
2755 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2756 if (err != GL_NO_ERROR)
2757 {
2758 return error(err);
2759 }
2760
daniel@transgaming.com83921382011-01-08 05:46:00 +00002761 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2762 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002763 if (err != GL_NO_ERROR)
2764 {
2765 return error(err);
2766 }
2767
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768 applyShaders();
2769 applyTextures();
2770
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002771 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002772 {
2773 return error(GL_INVALID_OPERATION);
2774 }
2775
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002776 if (!cullSkipsDraw(mode))
2777 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002778 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002779
daniel@transgaming.com83921382011-01-08 05:46:00 +00002780 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002781
2782 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2783 {
2784 drawClosingLine(count, type, indices);
2785 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002786 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002787}
2788
2789void Context::finish()
2790{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002791 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002792 IDirect3DDevice9 *device = getDevice();
2793 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002794 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002796 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2797 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002798 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002799 ERR("CreateQuery failed hr=%x\n", result);
2800 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2801 {
2802 return error(GL_OUT_OF_MEMORY);
2803 }
2804 ASSERT(false);
2805 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002806 }
2807
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002808 IDirect3DStateBlock9 *savedState = NULL;
2809 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2810 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002811 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002812 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002814
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002815 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002816 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002817 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002818 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002819 ASSERT(false);
2820 return;
2821 }
2822
2823 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2824 if (FAILED(result))
2825 {
2826 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2827 occlusionQuery->Release();
2828 savedState->Release();
2829 ASSERT(false);
2830 return;
2831 }
2832
2833 // Render something outside the render target
2834 device->SetPixelShader(NULL);
2835 device->SetVertexShader(NULL);
2836 device->SetFVF(D3DFVF_XYZRHW);
2837 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2838 display->startScene();
2839 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2840
2841 result = occlusionQuery->Issue(D3DISSUE_END);
2842 if (FAILED(result))
2843 {
2844 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2845 occlusionQuery->Release();
2846 savedState->Apply();
2847 savedState->Release();
2848 ASSERT(false);
2849 return;
2850 }
2851
2852 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2853 {
2854 // Keep polling, but allow other threads to do something useful first
2855 Sleep(0);
2856 }
2857
2858 occlusionQuery->Release();
2859 savedState->Apply();
2860 savedState->Release();
2861
2862 if (result == D3DERR_DEVICELOST)
2863 {
2864 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002865 }
2866}
2867
2868void Context::flush()
2869{
2870 IDirect3DDevice9 *device = getDevice();
2871 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002872 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002874 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2875 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002876 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002877 ERR("CreateQuery failed hr=%x\n", result);
2878 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2879 {
2880 return error(GL_OUT_OF_MEMORY);
2881 }
2882 ASSERT(false);
2883 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002884 }
2885
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002886 result = eventQuery->Issue(D3DISSUE_END);
2887 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002888 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002889 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2890 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002891 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002892 return;
2893 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002894
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002895 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2896 eventQuery->Release();
2897
2898 if (result == D3DERR_DEVICELOST)
2899 {
2900 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002901 }
2902}
2903
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002904void Context::drawClosingLine(unsigned int first, unsigned int last)
2905{
2906 IDirect3DDevice9 *device = getDevice();
2907 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002908 bool succeeded = false;
2909 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002910
2911 if (supports32bitIndices())
2912 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002913 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002914
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002915 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002916 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002917 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2918 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002919
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002920 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2921
2922 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2923 if (data)
2924 {
2925 data[0] = last;
2926 data[1] = first;
2927 mClosingIB->unmap();
2928 offset /= 4;
2929 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002930 }
2931 }
2932 else
2933 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002934 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002935
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002936 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002937 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002938 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2939 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002940
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002941 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2942
2943 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2944 if (data)
2945 {
2946 data[0] = last;
2947 data[1] = first;
2948 mClosingIB->unmap();
2949 offset /= 2;
2950 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002951 }
2952 }
2953
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002954 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002955 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002956 device->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002957 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002958
jbauman@chromium.org2c199b12011-06-13 22:20:06 +00002959 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002960 }
2961 else
2962 {
2963 ERR("Could not create an index buffer for closing a line loop.");
2964 error(GL_OUT_OF_MEMORY);
2965 }
2966}
2967
2968void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2969{
2970 unsigned int first = 0;
2971 unsigned int last = 0;
2972
2973 if (mState.elementArrayBuffer.get())
2974 {
2975 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2976 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2977 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2978 }
2979
2980 switch (type)
2981 {
2982 case GL_UNSIGNED_BYTE:
2983 first = static_cast<const GLubyte*>(indices)[0];
2984 last = static_cast<const GLubyte*>(indices)[count - 1];
2985 break;
2986 case GL_UNSIGNED_SHORT:
2987 first = static_cast<const GLushort*>(indices)[0];
2988 last = static_cast<const GLushort*>(indices)[count - 1];
2989 break;
2990 case GL_UNSIGNED_INT:
2991 first = static_cast<const GLuint*>(indices)[0];
2992 last = static_cast<const GLuint*>(indices)[count - 1];
2993 break;
2994 default: UNREACHABLE();
2995 }
2996
2997 drawClosingLine(first, last);
2998}
2999
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000void Context::recordInvalidEnum()
3001{
3002 mInvalidEnum = true;
3003}
3004
3005void Context::recordInvalidValue()
3006{
3007 mInvalidValue = true;
3008}
3009
3010void Context::recordInvalidOperation()
3011{
3012 mInvalidOperation = true;
3013}
3014
3015void Context::recordOutOfMemory()
3016{
3017 mOutOfMemory = true;
3018}
3019
3020void Context::recordInvalidFramebufferOperation()
3021{
3022 mInvalidFramebufferOperation = true;
3023}
3024
3025// Get one of the recorded errors and clear its flag, if any.
3026// [OpenGL ES 2.0.24] section 2.5 page 13.
3027GLenum Context::getError()
3028{
3029 if (mInvalidEnum)
3030 {
3031 mInvalidEnum = false;
3032
3033 return GL_INVALID_ENUM;
3034 }
3035
3036 if (mInvalidValue)
3037 {
3038 mInvalidValue = false;
3039
3040 return GL_INVALID_VALUE;
3041 }
3042
3043 if (mInvalidOperation)
3044 {
3045 mInvalidOperation = false;
3046
3047 return GL_INVALID_OPERATION;
3048 }
3049
3050 if (mOutOfMemory)
3051 {
3052 mOutOfMemory = false;
3053
3054 return GL_OUT_OF_MEMORY;
3055 }
3056
3057 if (mInvalidFramebufferOperation)
3058 {
3059 mInvalidFramebufferOperation = false;
3060
3061 return GL_INVALID_FRAMEBUFFER_OPERATION;
3062 }
3063
3064 return GL_NO_ERROR;
3065}
3066
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003067bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003068{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003069 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003070}
3071
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003072int Context::getMaximumVaryingVectors() const
3073{
3074 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3075}
3076
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003077unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003078{
3079 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3080}
3081
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003082unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003083{
3084 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3085}
3086
daniel@transgaming.com458da142010-11-28 02:03:02 +00003087int Context::getMaximumFragmentUniformVectors() const
3088{
3089 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3090}
3091
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003092int Context::getMaxSupportedSamples() const
3093{
3094 return mMaxSupportedSamples;
3095}
3096
3097int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3098{
3099 if (requested == 0)
3100 {
3101 return requested;
3102 }
3103
3104 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3105 if (itr == mMultiSampleSupport.end())
3106 {
3107 return -1;
3108 }
3109
3110 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3111 {
3112 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3113 {
3114 return i;
3115 }
3116 }
3117
3118 return -1;
3119}
3120
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003121bool Context::supportsEventQueries() const
3122{
3123 return mSupportsEventQueries;
3124}
3125
gman@chromium.org50c526d2011-08-10 05:19:44 +00003126bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003127{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003128 return mSupportsDXT1Textures;
3129}
3130
3131bool Context::supportsDXT3Textures() const
3132{
3133 return mSupportsDXT3Textures;
3134}
3135
3136bool Context::supportsDXT5Textures() const
3137{
3138 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003139}
3140
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003141bool Context::supportsFloatTextures() const
3142{
3143 return mSupportsFloatTextures;
3144}
3145
3146bool Context::supportsFloatLinearFilter() const
3147{
3148 return mSupportsFloatLinearFilter;
3149}
3150
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003151bool Context::supportsFloatRenderableTextures() const
3152{
3153 return mSupportsFloatRenderableTextures;
3154}
3155
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003156bool Context::supportsHalfFloatTextures() const
3157{
3158 return mSupportsHalfFloatTextures;
3159}
3160
3161bool Context::supportsHalfFloatLinearFilter() const
3162{
3163 return mSupportsHalfFloatLinearFilter;
3164}
3165
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003166bool Context::supportsHalfFloatRenderableTextures() const
3167{
3168 return mSupportsHalfFloatRenderableTextures;
3169}
3170
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003171int Context::getMaximumRenderbufferDimension() const
3172{
3173 return mMaxRenderbufferDimension;
3174}
3175
3176int Context::getMaximumTextureDimension() const
3177{
3178 return mMaxTextureDimension;
3179}
3180
3181int Context::getMaximumCubeTextureDimension() const
3182{
3183 return mMaxCubeTextureDimension;
3184}
3185
3186int Context::getMaximumTextureLevel() const
3187{
3188 return mMaxTextureLevel;
3189}
3190
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003191bool Context::supportsLuminanceTextures() const
3192{
3193 return mSupportsLuminanceTextures;
3194}
3195
3196bool Context::supportsLuminanceAlphaTextures() const
3197{
3198 return mSupportsLuminanceAlphaTextures;
3199}
3200
daniel@transgaming.com83921382011-01-08 05:46:00 +00003201bool Context::supports32bitIndices() const
3202{
3203 return mSupports32bitIndices;
3204}
3205
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003206bool Context::supportsNonPower2Texture() const
3207{
3208 return mSupportsNonPower2Texture;
3209}
3210
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003211void Context::detachBuffer(GLuint buffer)
3212{
3213 // [OpenGL ES 2.0.24] section 2.9 page 22:
3214 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3215 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3216
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003217 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003218 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003219 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003220 }
3221
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003222 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003223 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003224 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003225 }
3226
3227 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3228 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003229 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003230 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003231 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003232 }
3233 }
3234}
3235
3236void Context::detachTexture(GLuint texture)
3237{
3238 // [OpenGL ES 2.0.24] section 3.8 page 84:
3239 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3240 // rebound to texture object zero
3241
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003242 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003243 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003244 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003245 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003246 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003247 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003248 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003249 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003250 }
3251 }
3252
3253 // [OpenGL ES 2.0.24] section 4.4 page 112:
3254 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3255 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3256 // image was attached in the currently bound framebuffer.
3257
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003258 Framebuffer *readFramebuffer = getReadFramebuffer();
3259 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003260
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003261 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003262 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003263 readFramebuffer->detachTexture(texture);
3264 }
3265
3266 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3267 {
3268 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003269 }
3270}
3271
3272void Context::detachFramebuffer(GLuint framebuffer)
3273{
3274 // [OpenGL ES 2.0.24] section 4.4 page 107:
3275 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3276 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3277
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003278 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003279 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003280 bindReadFramebuffer(0);
3281 }
3282
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003283 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003284 {
3285 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003286 }
3287}
3288
3289void Context::detachRenderbuffer(GLuint renderbuffer)
3290{
3291 // [OpenGL ES 2.0.24] section 4.4 page 109:
3292 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3293 // had been executed with the target RENDERBUFFER and name of zero.
3294
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003295 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003296 {
3297 bindRenderbuffer(0);
3298 }
3299
3300 // [OpenGL ES 2.0.24] section 4.4 page 111:
3301 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3302 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3303 // point to which this image was attached in the currently bound framebuffer.
3304
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003305 Framebuffer *readFramebuffer = getReadFramebuffer();
3306 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003307
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003308 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003309 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003310 readFramebuffer->detachRenderbuffer(renderbuffer);
3311 }
3312
3313 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3314 {
3315 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003316 }
3317}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003318
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003319Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003320{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003321 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003322
3323 if (t == NULL)
3324 {
3325 static const GLubyte color[] = { 0, 0, 0, 255 };
3326
3327 switch (type)
3328 {
3329 default:
3330 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003331 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003332
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003333 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003334 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003335 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003336 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003337 t = incomplete2d;
3338 }
3339 break;
3340
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003341 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003342 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003343 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003344
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003345 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3346 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3347 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3348 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3349 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3350 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003351
3352 t = incompleteCube;
3353 }
3354 break;
3355 }
3356
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003357 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003358 }
3359
3360 return t;
3361}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003362
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003363bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003364{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003365 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003366}
3367
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003368bool Context::isTriangleMode(GLenum drawMode)
3369{
3370 switch (drawMode)
3371 {
3372 case GL_TRIANGLES:
3373 case GL_TRIANGLE_FAN:
3374 case GL_TRIANGLE_STRIP:
3375 return true;
3376 case GL_POINTS:
3377 case GL_LINES:
3378 case GL_LINE_LOOP:
3379 case GL_LINE_STRIP:
3380 return false;
3381 default: UNREACHABLE();
3382 }
3383
3384 return false;
3385}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003386
3387void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3388{
3389 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3390
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003391 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3392 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3393 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3394 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003395
daniel@transgaming.com83921382011-01-08 05:46:00 +00003396 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003397}
3398
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003399// keep list sorted in following order
3400// OES extensions
3401// EXT extensions
3402// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003403void Context::initExtensionString()
3404{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003405 mExtensionString = "";
3406
3407 // OES extensions
3408 if (supports32bitIndices())
3409 {
3410 mExtensionString += "GL_OES_element_index_uint ";
3411 }
3412
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003413 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003414 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003415 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003416
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003417 if (supportsHalfFloatTextures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003418 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003419 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003420 }
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003421 if (supportsHalfFloatLinearFilter())
3422 {
3423 mExtensionString += "GL_OES_texture_half_float_linear ";
3424 }
3425 if (supportsFloatTextures())
3426 {
3427 mExtensionString += "GL_OES_texture_float ";
3428 }
3429 if (supportsFloatLinearFilter())
3430 {
3431 mExtensionString += "GL_OES_texture_float_linear ";
3432 }
3433
3434 if (supportsNonPower2Texture())
3435 {
3436 mExtensionString += "GL_OES_texture_npot ";
3437 }
3438
3439 // Multi-vendor (EXT) extensions
3440 mExtensionString += "GL_EXT_read_format_bgra ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003441
gman@chromium.org50c526d2011-08-10 05:19:44 +00003442 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003443 {
3444 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3445 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003446
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003447 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003448
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003449 // ANGLE-specific extensions
3450 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003451 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003452 {
3453 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3454 }
3455
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003456 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003457 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003458 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3459 }
3460 if (supportsDXT5Textures())
3461 {
3462 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003463 }
zmo@google.coma574f782011-10-03 21:45:23 +00003464 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003465
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003466 // Other vendor-specific extensions
3467 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003468 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003469 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003470 }
3471
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003472 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3473 if (end != std::string::npos)
3474 {
3475 mExtensionString.resize(end+1);
3476 }
3477}
3478
3479const char *Context::getExtensionString() const
3480{
3481 return mExtensionString.c_str();
3482}
3483
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003484void Context::initRendererString()
3485{
3486 egl::Display *display = getDisplay();
3487 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
3488
3489 mRendererString = "ANGLE (";
3490 mRendererString += identifier->Description;
3491 mRendererString += ")";
3492}
3493
3494const char *Context::getRendererString() const
3495{
3496 return mRendererString.c_str();
3497}
3498
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003499void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3500 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3501 GLbitfield mask)
3502{
3503 IDirect3DDevice9 *device = getDevice();
3504
3505 Framebuffer *readFramebuffer = getReadFramebuffer();
3506 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3507
3508 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3509 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3510 {
3511 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3512 }
3513
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003514 if (drawFramebuffer->getSamples() != 0)
3515 {
3516 return error(GL_INVALID_OPERATION);
3517 }
3518
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003519 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3520 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3521 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3522 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3523
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003524 RECT sourceRect;
3525 RECT destRect;
3526
3527 if (srcX0 < srcX1)
3528 {
3529 sourceRect.left = srcX0;
3530 sourceRect.right = srcX1;
3531 destRect.left = dstX0;
3532 destRect.right = dstX1;
3533 }
3534 else
3535 {
3536 sourceRect.left = srcX1;
3537 destRect.left = dstX1;
3538 sourceRect.right = srcX0;
3539 destRect.right = dstX0;
3540 }
3541
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003542 if (srcY0 < srcY1)
3543 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003544 sourceRect.top = readBufferHeight - srcY1;
3545 destRect.top = drawBufferHeight - dstY1;
3546 sourceRect.bottom = readBufferHeight - srcY0;
3547 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003548 }
3549 else
3550 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003551 sourceRect.top = readBufferHeight - srcY0;
3552 destRect.top = drawBufferHeight - dstY0;
3553 sourceRect.bottom = readBufferHeight - srcY1;
3554 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003555 }
3556
3557 RECT sourceScissoredRect = sourceRect;
3558 RECT destScissoredRect = destRect;
3559
3560 if (mState.scissorTest)
3561 {
3562 // Only write to parts of the destination framebuffer which pass the scissor test
3563 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3564 // rect will be checked against scissorY, rather than the bottom.
3565 if (destRect.left < mState.scissorX)
3566 {
3567 int xDiff = mState.scissorX - destRect.left;
3568 destScissoredRect.left = mState.scissorX;
3569 sourceScissoredRect.left += xDiff;
3570 }
3571
3572 if (destRect.right > mState.scissorX + mState.scissorWidth)
3573 {
3574 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3575 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3576 sourceScissoredRect.right -= xDiff;
3577 }
3578
3579 if (destRect.top < mState.scissorY)
3580 {
3581 int yDiff = mState.scissorY - destRect.top;
3582 destScissoredRect.top = mState.scissorY;
3583 sourceScissoredRect.top += yDiff;
3584 }
3585
3586 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3587 {
3588 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3589 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3590 sourceScissoredRect.bottom -= yDiff;
3591 }
3592 }
3593
3594 bool blitRenderTarget = false;
3595 bool blitDepthStencil = false;
3596
3597 RECT sourceTrimmedRect = sourceScissoredRect;
3598 RECT destTrimmedRect = destScissoredRect;
3599
3600 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3601 // the actual draw and read surfaces.
3602 if (sourceTrimmedRect.left < 0)
3603 {
3604 int xDiff = 0 - sourceTrimmedRect.left;
3605 sourceTrimmedRect.left = 0;
3606 destTrimmedRect.left += xDiff;
3607 }
3608
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003609 if (sourceTrimmedRect.right > readBufferWidth)
3610 {
3611 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3612 sourceTrimmedRect.right = readBufferWidth;
3613 destTrimmedRect.right -= xDiff;
3614 }
3615
3616 if (sourceTrimmedRect.top < 0)
3617 {
3618 int yDiff = 0 - sourceTrimmedRect.top;
3619 sourceTrimmedRect.top = 0;
3620 destTrimmedRect.top += yDiff;
3621 }
3622
3623 if (sourceTrimmedRect.bottom > readBufferHeight)
3624 {
3625 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3626 sourceTrimmedRect.bottom = readBufferHeight;
3627 destTrimmedRect.bottom -= yDiff;
3628 }
3629
3630 if (destTrimmedRect.left < 0)
3631 {
3632 int xDiff = 0 - destTrimmedRect.left;
3633 destTrimmedRect.left = 0;
3634 sourceTrimmedRect.left += xDiff;
3635 }
3636
3637 if (destTrimmedRect.right > drawBufferWidth)
3638 {
3639 int xDiff = destTrimmedRect.right - drawBufferWidth;
3640 destTrimmedRect.right = drawBufferWidth;
3641 sourceTrimmedRect.right -= xDiff;
3642 }
3643
3644 if (destTrimmedRect.top < 0)
3645 {
3646 int yDiff = 0 - destTrimmedRect.top;
3647 destTrimmedRect.top = 0;
3648 sourceTrimmedRect.top += yDiff;
3649 }
3650
3651 if (destTrimmedRect.bottom > drawBufferHeight)
3652 {
3653 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3654 destTrimmedRect.bottom = drawBufferHeight;
3655 sourceTrimmedRect.bottom -= yDiff;
3656 }
3657
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003658 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003659 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3660 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3661 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3662 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003663 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3664 {
3665 partialBufferCopy = true;
3666 }
3667
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003668 if (mask & GL_COLOR_BUFFER_BIT)
3669 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003670 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3671 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3672 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3673 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3674 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003675 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3676 {
3677 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3678 return error(GL_INVALID_OPERATION);
3679 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003680
3681 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3682 {
3683 return error(GL_INVALID_OPERATION);
3684 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003685
3686 blitRenderTarget = true;
3687
3688 }
3689
3690 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3691 {
3692 DepthStencilbuffer *readDSBuffer = NULL;
3693 DepthStencilbuffer *drawDSBuffer = NULL;
3694
3695 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3696 // both a depth and stencil buffer, it will be the same buffer.
3697
3698 if (mask & GL_DEPTH_BUFFER_BIT)
3699 {
3700 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3701 {
3702 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3703 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3704 {
3705 return error(GL_INVALID_OPERATION);
3706 }
3707
3708 blitDepthStencil = true;
3709 readDSBuffer = readFramebuffer->getDepthbuffer();
3710 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3711 }
3712 }
3713
3714 if (mask & GL_STENCIL_BUFFER_BIT)
3715 {
3716 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3717 {
3718 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3719 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3720 {
3721 return error(GL_INVALID_OPERATION);
3722 }
3723
3724 blitDepthStencil = true;
3725 readDSBuffer = readFramebuffer->getStencilbuffer();
3726 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3727 }
3728 }
3729
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003730 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003731 {
3732 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3733 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3734 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003735
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003736 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3737 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003738 {
3739 return error(GL_INVALID_OPERATION);
3740 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003741 }
3742
3743 if (blitRenderTarget || blitDepthStencil)
3744 {
3745 egl::Display *display = getDisplay();
3746 display->endScene();
3747
3748 if (blitRenderTarget)
3749 {
3750 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3751 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3752
3753 if (FAILED(result))
3754 {
3755 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3756 return;
3757 }
3758 }
3759
3760 if (blitDepthStencil)
3761 {
3762 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3763
3764 if (FAILED(result))
3765 {
3766 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3767 return;
3768 }
3769 }
3770 }
3771}
3772
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003773VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3774{
3775 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3776 {
3777 mVertexDeclCache[i].vertexDeclaration = NULL;
3778 mVertexDeclCache[i].lruCount = 0;
3779 }
3780}
3781
3782VertexDeclarationCache::~VertexDeclarationCache()
3783{
3784 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3785 {
3786 if (mVertexDeclCache[i].vertexDeclaration)
3787 {
3788 mVertexDeclCache[i].vertexDeclaration->Release();
3789 }
3790 }
3791}
3792
3793GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3794{
3795 IDirect3DDevice9 *device = getDevice();
3796
3797 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3798 D3DVERTEXELEMENT9 *element = &elements[0];
3799
3800 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3801 {
3802 if (attributes[i].active)
3803 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003804 if (mAppliedVBs[i].serial != attributes[i].serial ||
3805 mAppliedVBs[i].stride != attributes[i].stride ||
3806 mAppliedVBs[i].offset != attributes[i].offset)
3807 {
3808 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3809 mAppliedVBs[i].serial = attributes[i].serial;
3810 mAppliedVBs[i].stride = attributes[i].stride;
3811 mAppliedVBs[i].offset = attributes[i].offset;
3812 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003813
3814 element->Stream = i;
3815 element->Offset = 0;
3816 element->Type = attributes[i].type;
3817 element->Method = D3DDECLMETHOD_DEFAULT;
3818 element->Usage = D3DDECLUSAGE_TEXCOORD;
3819 element->UsageIndex = program->getSemanticIndex(i);
3820 element++;
3821 }
3822 }
3823
3824 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3825 *(element++) = end;
3826
3827 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3828 {
3829 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3830 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3831 {
3832 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003833 if(entry->vertexDeclaration != mLastSetVDecl)
3834 {
3835 device->SetVertexDeclaration(entry->vertexDeclaration);
3836 mLastSetVDecl = entry->vertexDeclaration;
3837 }
3838
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003839 return GL_NO_ERROR;
3840 }
3841 }
3842
3843 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3844
3845 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3846 {
3847 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3848 {
3849 lastCache = &mVertexDeclCache[i];
3850 }
3851 }
3852
3853 if (lastCache->vertexDeclaration != NULL)
3854 {
3855 lastCache->vertexDeclaration->Release();
3856 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003857 // mLastSetVDecl is set to the replacement, so we don't have to worry
3858 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003859 }
3860
3861 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3862 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3863 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003864 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003865 lastCache->lruCount = ++mMaxLru;
3866
3867 return GL_NO_ERROR;
3868}
3869
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003870void VertexDeclarationCache::markStateDirty()
3871{
3872 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3873 {
3874 mAppliedVBs[i].serial = 0;
3875 }
3876
3877 mLastSetVDecl = NULL;
3878}
3879
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003880}
3881
3882extern "C"
3883{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003884gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003885{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003886 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003887}
3888
3889void glDestroyContext(gl::Context *context)
3890{
3891 delete context;
3892
3893 if (context == gl::getContext())
3894 {
3895 gl::makeCurrent(NULL, NULL, NULL);
3896 }
3897}
3898
3899void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3900{
3901 gl::makeCurrent(context, display, surface);
3902}
3903
3904gl::Context *glGetCurrentContext()
3905{
3906 return gl::getContext();
3907}
3908}