blob: ef290f07cfbcf61f282558ca914805a3e98e1911 [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{
979 return getFramebuffer(mState.drawFramebuffer);
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;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
1030void Context::bindRenderbuffer(GLuint renderbuffer)
1031{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001032 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1033
1034 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035}
1036
1037void Context::useProgram(GLuint program)
1038{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001039 GLuint priorProgram = mState.currentProgram;
1040 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001041
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001042 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001044 Program *newProgram = mResourceManager->getProgram(program);
1045 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001046 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001047 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001048
1049 if (newProgram)
1050 {
1051 newProgram->addRef();
1052 }
1053
1054 if (oldProgram)
1055 {
1056 oldProgram->release();
1057 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059}
1060
1061void Context::setFramebufferZero(Framebuffer *buffer)
1062{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001063 delete mFramebufferMap[0];
1064 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065}
1066
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001067void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001069 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1070 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071}
1072
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001073Framebuffer *Context::getFramebuffer(unsigned int handle)
1074{
1075 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1076
1077 if (framebuffer == mFramebufferMap.end())
1078 {
1079 return NULL;
1080 }
1081 else
1082 {
1083 return framebuffer->second;
1084 }
1085}
1086
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001087Fence *Context::getFence(unsigned int handle)
1088{
1089 FenceMap::iterator fence = mFenceMap.find(handle);
1090
1091 if (fence == mFenceMap.end())
1092 {
1093 return NULL;
1094 }
1095 else
1096 {
1097 return fence->second;
1098 }
1099}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001100
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101Buffer *Context::getArrayBuffer()
1102{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001103 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104}
1105
1106Buffer *Context::getElementArrayBuffer()
1107{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001108 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109}
1110
1111Program *Context::getCurrentProgram()
1112{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001113 if (!mCachedCurrentProgram)
1114 {
1115 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1116 }
1117 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118}
1119
1120Texture2D *Context::getTexture2D()
1121{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001122 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123}
1124
1125TextureCubeMap *Context::getTextureCubeMap()
1126{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001127 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128}
1129
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001130Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001132 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001133
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001134 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001135 {
1136 switch (type)
1137 {
1138 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001139 case TEXTURE_2D: return mTexture2DZero.get();
1140 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001141 }
1142 }
1143
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001144 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001145}
1146
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001147bool Context::getBooleanv(GLenum pname, GLboolean *params)
1148{
1149 switch (pname)
1150 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001151 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1152 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1153 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001154 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001155 params[0] = mState.colorMaskRed;
1156 params[1] = mState.colorMaskGreen;
1157 params[2] = mState.colorMaskBlue;
1158 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001159 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001160 case GL_CULL_FACE: *params = mState.cullFace; break;
1161 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1162 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1163 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1164 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1165 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1166 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1167 case GL_BLEND: *params = mState.blend; break;
1168 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001169 default:
1170 return false;
1171 }
1172
1173 return true;
1174}
1175
1176bool Context::getFloatv(GLenum pname, GLfloat *params)
1177{
1178 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1179 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1180 // GetIntegerv as its native query function. As it would require conversion in any
1181 // case, this should make no difference to the calling application.
1182 switch (pname)
1183 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001184 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1185 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1186 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1187 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1188 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001189 case GL_ALIASED_LINE_WIDTH_RANGE:
1190 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1191 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1192 break;
1193 case GL_ALIASED_POINT_SIZE_RANGE:
1194 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001195 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 +00001196 break;
1197 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001198 params[0] = mState.zNear;
1199 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001200 break;
1201 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001202 params[0] = mState.colorClearValue.red;
1203 params[1] = mState.colorClearValue.green;
1204 params[2] = mState.colorClearValue.blue;
1205 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001206 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001207 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001208 params[0] = mState.blendColor.red;
1209 params[1] = mState.blendColor.green;
1210 params[2] = mState.blendColor.blue;
1211 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001212 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001213 default:
1214 return false;
1215 }
1216
1217 return true;
1218}
1219
1220bool Context::getIntegerv(GLenum pname, GLint *params)
1221{
1222 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1223 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1224 // GetIntegerv as its native query function. As it would require conversion in any
1225 // case, this should make no difference to the calling application. You may find it in
1226 // Context::getFloatv.
1227 switch (pname)
1228 {
1229 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1230 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001231 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001232 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1233 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001234 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001235 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001236 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001237 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001238 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001239 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1240 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001241 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1242 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1243 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001244 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001245 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1246 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1247 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1248 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001249 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001250 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1251 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1252 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1253 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1254 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1255 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1256 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1257 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1258 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1259 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1260 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1261 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1262 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1263 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1264 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1265 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1266 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1267 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1268 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1269 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1270 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1271 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1272 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1273 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001274 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1275 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001276 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001277 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001278 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001279 case GL_MAX_SAMPLES_ANGLE:
1280 {
1281 GLsizei maxSamples = getMaxSupportedSamples();
1282 if (maxSamples != 0)
1283 {
1284 *params = maxSamples;
1285 }
1286 else
1287 {
1288 return false;
1289 }
1290
1291 break;
1292 }
1293 case GL_SAMPLE_BUFFERS:
1294 case GL_SAMPLES:
1295 {
1296 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1297 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1298 {
1299 switch (pname)
1300 {
1301 case GL_SAMPLE_BUFFERS:
1302 if (framebuffer->getSamples() != 0)
1303 {
1304 *params = 1;
1305 }
1306 else
1307 {
1308 *params = 0;
1309 }
1310 break;
1311 case GL_SAMPLES:
1312 *params = framebuffer->getSamples();
1313 break;
1314 }
1315 }
1316 else
1317 {
1318 *params = 0;
1319 }
1320 }
1321 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001322 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1323 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1324 case GL_MAX_VIEWPORT_DIMS:
1325 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001326 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001327 params[0] = maxDimension;
1328 params[1] = maxDimension;
1329 }
1330 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001331 case GL_COMPRESSED_TEXTURE_FORMATS:
1332 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001333 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001334 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001335 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1336 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1337 }
1338 if (supportsDXT3Textures())
1339 {
1340 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1341 }
1342 if (supportsDXT5Textures())
1343 {
1344 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001345 }
1346 }
1347 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001348 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001349 params[0] = mState.viewportX;
1350 params[1] = mState.viewportY;
1351 params[2] = mState.viewportWidth;
1352 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001353 break;
1354 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001355 params[0] = mState.scissorX;
1356 params[1] = mState.scissorY;
1357 params[2] = mState.scissorWidth;
1358 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001360 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1361 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001362 case GL_RED_BITS:
1363 case GL_GREEN_BITS:
1364 case GL_BLUE_BITS:
1365 case GL_ALPHA_BITS:
1366 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001367 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001368 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1369
1370 if (colorbuffer)
1371 {
1372 switch (pname)
1373 {
1374 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1375 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1376 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1377 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1378 }
1379 }
1380 else
1381 {
1382 *params = 0;
1383 }
1384 }
1385 break;
1386 case GL_DEPTH_BITS:
1387 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001388 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001389 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001390
1391 if (depthbuffer)
1392 {
1393 *params = depthbuffer->getDepthSize();
1394 }
1395 else
1396 {
1397 *params = 0;
1398 }
1399 }
1400 break;
1401 case GL_STENCIL_BITS:
1402 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001403 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001404 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001405
1406 if (stencilbuffer)
1407 {
1408 *params = stencilbuffer->getStencilSize();
1409 }
1410 else
1411 {
1412 *params = 0;
1413 }
1414 }
1415 break;
1416 case GL_TEXTURE_BINDING_2D:
1417 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001418 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001419 {
1420 error(GL_INVALID_OPERATION);
1421 return false;
1422 }
1423
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001424 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001425 }
1426 break;
1427 case GL_TEXTURE_BINDING_CUBE_MAP:
1428 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001429 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001430 {
1431 error(GL_INVALID_OPERATION);
1432 return false;
1433 }
1434
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001435 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001436 }
1437 break;
1438 default:
1439 return false;
1440 }
1441
1442 return true;
1443}
1444
1445bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1446{
1447 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1448 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1449 // to the fact that it is stored internally as a float, and so would require conversion
1450 // if returned from Context::getIntegerv. Since this conversion is already implemented
1451 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1452 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1453 // application.
1454 switch (pname)
1455 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001456 case GL_COMPRESSED_TEXTURE_FORMATS:
1457 {
1458 *type = GL_INT;
1459 *numParams = mNumCompressedTextureFormats;
1460 }
1461 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001462 case GL_SHADER_BINARY_FORMATS:
1463 {
1464 *type = GL_INT;
1465 *numParams = 0;
1466 }
1467 break;
1468 case GL_MAX_VERTEX_ATTRIBS:
1469 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1470 case GL_MAX_VARYING_VECTORS:
1471 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1472 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1473 case GL_MAX_TEXTURE_IMAGE_UNITS:
1474 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1475 case GL_MAX_RENDERBUFFER_SIZE:
1476 case GL_NUM_SHADER_BINARY_FORMATS:
1477 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1478 case GL_ARRAY_BUFFER_BINDING:
1479 case GL_FRAMEBUFFER_BINDING:
1480 case GL_RENDERBUFFER_BINDING:
1481 case GL_CURRENT_PROGRAM:
1482 case GL_PACK_ALIGNMENT:
1483 case GL_UNPACK_ALIGNMENT:
1484 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001485 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001486 case GL_RED_BITS:
1487 case GL_GREEN_BITS:
1488 case GL_BLUE_BITS:
1489 case GL_ALPHA_BITS:
1490 case GL_DEPTH_BITS:
1491 case GL_STENCIL_BITS:
1492 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1493 case GL_CULL_FACE_MODE:
1494 case GL_FRONT_FACE:
1495 case GL_ACTIVE_TEXTURE:
1496 case GL_STENCIL_FUNC:
1497 case GL_STENCIL_VALUE_MASK:
1498 case GL_STENCIL_REF:
1499 case GL_STENCIL_FAIL:
1500 case GL_STENCIL_PASS_DEPTH_FAIL:
1501 case GL_STENCIL_PASS_DEPTH_PASS:
1502 case GL_STENCIL_BACK_FUNC:
1503 case GL_STENCIL_BACK_VALUE_MASK:
1504 case GL_STENCIL_BACK_REF:
1505 case GL_STENCIL_BACK_FAIL:
1506 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1507 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1508 case GL_DEPTH_FUNC:
1509 case GL_BLEND_SRC_RGB:
1510 case GL_BLEND_SRC_ALPHA:
1511 case GL_BLEND_DST_RGB:
1512 case GL_BLEND_DST_ALPHA:
1513 case GL_BLEND_EQUATION_RGB:
1514 case GL_BLEND_EQUATION_ALPHA:
1515 case GL_STENCIL_WRITEMASK:
1516 case GL_STENCIL_BACK_WRITEMASK:
1517 case GL_STENCIL_CLEAR_VALUE:
1518 case GL_SUBPIXEL_BITS:
1519 case GL_MAX_TEXTURE_SIZE:
1520 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1521 case GL_SAMPLE_BUFFERS:
1522 case GL_SAMPLES:
1523 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1524 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1525 case GL_TEXTURE_BINDING_2D:
1526 case GL_TEXTURE_BINDING_CUBE_MAP:
1527 {
1528 *type = GL_INT;
1529 *numParams = 1;
1530 }
1531 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001532 case GL_MAX_SAMPLES_ANGLE:
1533 {
1534 if (getMaxSupportedSamples() != 0)
1535 {
1536 *type = GL_INT;
1537 *numParams = 1;
1538 }
1539 else
1540 {
1541 return false;
1542 }
1543 }
1544 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001545 case GL_MAX_VIEWPORT_DIMS:
1546 {
1547 *type = GL_INT;
1548 *numParams = 2;
1549 }
1550 break;
1551 case GL_VIEWPORT:
1552 case GL_SCISSOR_BOX:
1553 {
1554 *type = GL_INT;
1555 *numParams = 4;
1556 }
1557 break;
1558 case GL_SHADER_COMPILER:
1559 case GL_SAMPLE_COVERAGE_INVERT:
1560 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001561 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1562 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1563 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1564 case GL_SAMPLE_COVERAGE:
1565 case GL_SCISSOR_TEST:
1566 case GL_STENCIL_TEST:
1567 case GL_DEPTH_TEST:
1568 case GL_BLEND:
1569 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001570 {
1571 *type = GL_BOOL;
1572 *numParams = 1;
1573 }
1574 break;
1575 case GL_COLOR_WRITEMASK:
1576 {
1577 *type = GL_BOOL;
1578 *numParams = 4;
1579 }
1580 break;
1581 case GL_POLYGON_OFFSET_FACTOR:
1582 case GL_POLYGON_OFFSET_UNITS:
1583 case GL_SAMPLE_COVERAGE_VALUE:
1584 case GL_DEPTH_CLEAR_VALUE:
1585 case GL_LINE_WIDTH:
1586 {
1587 *type = GL_FLOAT;
1588 *numParams = 1;
1589 }
1590 break;
1591 case GL_ALIASED_LINE_WIDTH_RANGE:
1592 case GL_ALIASED_POINT_SIZE_RANGE:
1593 case GL_DEPTH_RANGE:
1594 {
1595 *type = GL_FLOAT;
1596 *numParams = 2;
1597 }
1598 break;
1599 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001600 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001601 {
1602 *type = GL_FLOAT;
1603 *numParams = 4;
1604 }
1605 break;
1606 default:
1607 return false;
1608 }
1609
1610 return true;
1611}
1612
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001613// Applies the render target surface, depth stencil surface, viewport rectangle and
1614// scissor rectangle to the Direct3D 9 device
1615bool Context::applyRenderTarget(bool ignoreViewport)
1616{
1617 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001618
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001619 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620
1621 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1622 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001623 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001624 }
1625
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001626 IDirect3DSurface9 *renderTarget = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001627 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001628
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001629 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001630 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1631 if (renderTargetSerial != mAppliedRenderTargetSerial)
1632 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001633 renderTarget = framebufferObject->getRenderTarget();
1634
1635 if (!renderTarget)
1636 {
1637 return false; // Context must be lost
1638 }
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001639 device->SetRenderTarget(0, renderTarget);
1640 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001641 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 +00001642 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001643 }
1644
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001645 unsigned int depthbufferSerial = 0;
1646 unsigned int stencilbufferSerial = 0;
1647 if (framebufferObject->getDepthbufferType() != GL_NONE)
1648 {
1649 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001650 if (!depthStencil)
1651 {
1652 ERR("Depth stencil pointer unexpectedly null.");
1653 return false;
1654 }
1655
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001656 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1657 }
1658 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1659 {
1660 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001661 if (!depthStencil)
1662 {
1663 ERR("Depth stencil pointer unexpectedly null.");
1664 return false;
1665 }
1666
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001667 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1668 }
1669
1670 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001671 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001672 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001673 {
1674 device->SetDepthStencilSurface(depthStencil);
1675 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001676 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001677 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001678 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001679
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001680 if (!mRenderTargetDescInitialized || renderTargetChanged)
1681 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001682 if (!renderTarget)
1683 {
1684 renderTarget = framebufferObject->getRenderTarget();
1685
1686 if (!renderTarget)
1687 {
1688 return false; // Context must be lost
1689 }
1690 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001691 renderTarget->GetDesc(&mRenderTargetDesc);
1692 mRenderTargetDescInitialized = true;
1693 }
1694
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001697 float zNear = clamp01(mState.zNear);
1698 float zFar = clamp01(mState.zFar);
1699
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700 if (ignoreViewport)
1701 {
1702 viewport.X = 0;
1703 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001704 viewport.Width = mRenderTargetDesc.Width;
1705 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706 viewport.MinZ = 0.0f;
1707 viewport.MaxZ = 1.0f;
1708 }
1709 else
1710 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001711 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1712 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1713 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1714 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1715 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 +00001716 viewport.MinZ = zNear;
1717 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001718 }
1719
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001720 if (viewport.Width <= 0 || viewport.Height <= 0)
1721 {
1722 return false; // Nothing to render
1723 }
1724
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001725 if (!mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
1726 {
1727 device->SetViewport(&viewport);
1728 mSetViewport = viewport;
1729 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001730 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001731 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001733 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001735 if (mState.scissorTest)
1736 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001737 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1738 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1739 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1740 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1741 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001742 device->SetScissorRect(&rect);
1743 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1744 }
1745 else
1746 {
1747 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1748 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001749
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001750 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 }
1752
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001753 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755 Program *programObject = getCurrentProgram();
1756
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001757 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001758 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001759 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001760
daniel@transgaming.com31754962010-11-28 02:02:52 +00001761 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001762 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1763 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1764 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001765 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001766
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001767 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001768 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001769 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001770
daniel@transgaming.com31754962010-11-28 02:02:52 +00001771 GLint depthRange = programObject->getDxDepthRangeLocation();
1772 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1773 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001774 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001775 }
1776
1777 return true;
1778}
1779
1780// 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 +00001781void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001782{
1783 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001784 Program *programObject = getCurrentProgram();
1785
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001786 Framebuffer *framebufferObject = getDrawFramebuffer();
1787
1788 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1789
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001790 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001791 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001792 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001793
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001794 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001795 GLint alwaysFront = !isTriangleMode(drawMode);
1796 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1797
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001798 egl::Display *display = getDisplay();
1799 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
1800 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1801 // Apparently some ATI cards have a bug where a draw with a zero color
1802 // write mask can cause later draws to have incorrect results. Instead,
1803 // set a nonzero color write mask but modify the blend state so that no
1804 // drawing is done.
1805 // http://code.google.com/p/angleproject/issues/detail?id=169
1806
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001807 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001809 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001811 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812 }
1813 else
1814 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001815 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816 }
1817
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001818 mCullStateDirty = false;
1819 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001821 if (mDepthStateDirty)
1822 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001823 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001825 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1826 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827 }
1828 else
1829 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001830 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001832
1833 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001834 }
1835
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001836 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1837 {
1838 mBlendStateDirty = true;
1839 mMaskStateDirty = true;
1840 }
1841
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001842 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001844 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001845 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001846 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1847
1848 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1849 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1850 {
1851 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1852 }
1853 else
1854 {
1855 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1856 unorm<8>(mState.blendColor.alpha),
1857 unorm<8>(mState.blendColor.alpha),
1858 unorm<8>(mState.blendColor.alpha)));
1859 }
1860
1861 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1862 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1863 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1864
1865 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1866 mState.destBlendRGB != mState.destBlendAlpha ||
1867 mState.blendEquationRGB != mState.blendEquationAlpha)
1868 {
1869 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1870
1871 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1872 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1873 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001874 }
1875 else
1876 {
1877 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1878 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001879 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001880 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001881 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001882 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001883 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001884
1885 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001886 }
1887
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001888 if (mStencilStateDirty || mFrontFaceDirty)
1889 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001890 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001891 {
1892 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1893 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1894
1895 // FIXME: Unsupported by D3D9
1896 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1897 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1898 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1899 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1900 mState.stencilRef != mState.stencilBackRef ||
1901 mState.stencilMask != mState.stencilBackMask)
1902 {
1903 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1904 return error(GL_INVALID_OPERATION);
1905 }
1906
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001907 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001908 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001909 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1910
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001911 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1912 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001913 es2dx::ConvertComparison(mState.stencilFunc));
1914
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001915 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1916 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001917
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001918 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001919 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001920 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001921 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001922 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001923 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1924
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001925 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1926 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001927 es2dx::ConvertComparison(mState.stencilBackFunc));
1928
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001929 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1930 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001931
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001932 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001933 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001934 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001935 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001936 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001937 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1938 }
1939 else
1940 {
1941 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1942 }
1943
1944 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001945 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001946 }
1947
1948 if (mMaskStateDirty)
1949 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001950 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1951 mState.colorMaskBlue, mState.colorMaskAlpha);
1952 if (colorMask == 0 && !zeroColorMaskAllowed)
1953 {
1954 // Enable green channel, but set blending so nothing will be drawn.
1955 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1956 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1957
1958 device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1959 device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1960 device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
1961 }
1962 else
1963 {
1964 device->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
1965 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001966 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1967
1968 mMaskStateDirty = false;
1969 }
1970
1971 if (mPolygonOffsetStateDirty)
1972 {
1973 if (mState.polygonOffsetFill)
1974 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001975 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001976 if (depthbuffer)
1977 {
1978 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1979 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1980 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1981 }
1982 }
1983 else
1984 {
1985 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1986 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1987 }
1988
1989 mPolygonOffsetStateDirty = false;
1990 }
1991
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001992 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001994 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001995 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001996 FIXME("Sample alpha to coverage is unimplemented.");
1997 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001998
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001999 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
2000 if (mState.sampleCoverage)
2001 {
2002 unsigned int mask = 0;
2003 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002004 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002005 float threshold = 0.5f;
2006
2007 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002008 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002009 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002010
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002011 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002012 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002013 threshold += 1.0f;
2014 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002015 }
2016 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002017 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002018
2019 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002020 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002021 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002022 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002023
2024 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002025 }
2026 else
2027 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002028 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002029 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002030
2031 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032 }
2033
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002034 if (mDitherStateDirty)
2035 {
2036 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
2037
2038 mDitherStateDirty = false;
2039 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040}
2041
daniel@transgaming.com83921382011-01-08 05:46:00 +00002042GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002043{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002044 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002045
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002046 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002047 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002048 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002049 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002050 }
2051
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00002052 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002053}
2054
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002055// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002056GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002057{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002058 IDirect3DDevice9 *device = getDevice();
2059 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002060
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002061 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002062 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002063 if (indexInfo->serial != mAppliedIBSerial)
2064 {
2065 device->SetIndices(indexInfo->indexBuffer);
2066 mAppliedIBSerial = indexInfo->serial;
2067 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002068 }
2069
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002070 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071}
2072
2073// Applies the shaders and shader constants to the Direct3D 9 device
2074void Context::applyShaders()
2075{
2076 IDirect3DDevice9 *device = getDevice();
2077 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002078 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002079 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002080 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2081 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2082
2083 device->SetPixelShader(pixelShader);
2084 device->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002085 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002086 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002087 }
2088
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 programObject->applyUniforms();
2090}
2091
2092// Applies the textures and sampler states to the Direct3D 9 device
2093void Context::applyTextures()
2094{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002095 applyTextures(SAMPLER_PIXEL);
2096
2097 if (mSupportsVertexTexture)
2098 {
2099 applyTextures(SAMPLER_VERTEX);
2100 }
2101}
2102
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002103// For each Direct3D 9 sampler of either the pixel or vertex stage,
2104// looks up the corresponding OpenGL texture image unit and texture type,
2105// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002106void Context::applyTextures(SamplerType type)
2107{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108 IDirect3DDevice9 *device = getDevice();
2109 Program *programObject = getCurrentProgram();
2110
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002111 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 +00002112 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2113 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002114
2115 for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002117 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002118 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002119
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002120 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002121 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002122 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002123
2124 Texture *texture = getSamplerTexture(textureUnit, textureType);
2125
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002126 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002127 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002128 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2129
2130 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002131 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002132 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002133 {
2134 GLenum wrapS = texture->getWrapS();
2135 GLenum wrapT = texture->getWrapT();
2136 GLenum minFilter = texture->getMinFilter();
2137 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002138
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002139 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2140 device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002142 device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002143 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2144 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002145 device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2146 device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002147 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002149 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002150 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002151 device->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002152 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002153 }
2154 else
2155 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002156 device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002157 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002158
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002159 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002160 texture->resetDirty();
2161 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002163 else
2164 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002165 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002166 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002167 device->SetTexture(d3dSampler, NULL);
2168 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002169 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002170 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002171 }
2172}
2173
2174void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2175{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002176 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002177
2178 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2179 {
2180 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2181 }
2182
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002183 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2184 {
2185 return error(GL_INVALID_OPERATION);
2186 }
2187
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002189
2190 if (!renderTarget)
2191 {
2192 return; // Context must be lost, return silently
2193 }
2194
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002195 IDirect3DDevice9 *device = getDevice();
2196
2197 D3DSURFACE_DESC desc;
2198 renderTarget->GetDesc(&desc);
2199
2200 IDirect3DSurface9 *systemSurface;
2201 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2202
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002203 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002204 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002205 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206 return error(GL_OUT_OF_MEMORY);
2207 }
2208
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2210 {
2211 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002212 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213 }
2214
2215 result = device->GetRenderTargetData(renderTarget, systemSurface);
2216
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002217 if (FAILED(result))
2218 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002219 systemSurface->Release();
2220
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002221 switch (result)
2222 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002223 // It turns out that D3D will sometimes produce more error
2224 // codes than those documented.
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002225 case D3DERR_DRIVERINTERNALERROR:
2226 case D3DERR_DEVICELOST:
kbr@chromium.org1a2cd262011-07-08 17:30:18 +00002227 case D3DERR_DEVICEHUNG:
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002228 return error(GL_OUT_OF_MEMORY);
2229 default:
2230 UNREACHABLE();
2231 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002232 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233 }
2234
2235 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002236 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2237 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2238 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2239 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2240 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241
2242 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2243
2244 if (FAILED(result))
2245 {
2246 UNREACHABLE();
2247 systemSurface->Release();
2248
2249 return; // No sensible error to generate
2250 }
2251
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002252 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002254 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002255 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002256 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002257
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002258 for (int j = 0; j < rect.bottom - rect.top; j++)
2259 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002260 if (desc.Format == D3DFMT_A8R8G8B8 &&
2261 format == GL_BGRA_EXT &&
2262 type == GL_UNSIGNED_BYTE)
2263 {
2264 // Fast path for EXT_read_format_bgra, given
2265 // an RGBA source buffer. Note that buffers with no
2266 // alpha go through the slow path below.
2267 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002268 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002269 (rect.right - rect.left) * 4);
2270 continue;
2271 }
2272
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002273 for (int i = 0; i < rect.right - rect.left; i++)
2274 {
2275 float r;
2276 float g;
2277 float b;
2278 float a;
2279
2280 switch (desc.Format)
2281 {
2282 case D3DFMT_R5G6B5:
2283 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002284 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285
2286 a = 1.0f;
2287 b = (rgb & 0x001F) * (1.0f / 0x001F);
2288 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2289 r = (rgb & 0xF800) * (1.0f / 0xF800);
2290 }
2291 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 case D3DFMT_A1R5G5B5:
2293 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002294 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295
2296 a = (argb & 0x8000) ? 1.0f : 0.0f;
2297 b = (argb & 0x001F) * (1.0f / 0x001F);
2298 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2299 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2300 }
2301 break;
2302 case D3DFMT_A8R8G8B8:
2303 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002304 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305
2306 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2307 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2308 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2309 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2310 }
2311 break;
2312 case D3DFMT_X8R8G8B8:
2313 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002314 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315
2316 a = 1.0f;
2317 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2318 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2319 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2320 }
2321 break;
2322 case D3DFMT_A2R10G10B10:
2323 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002324 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325
2326 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2327 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2328 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2329 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2330 }
2331 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002332 case D3DFMT_A32B32G32R32F:
2333 {
2334 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002335 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2336 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2337 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2338 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002339 }
2340 break;
2341 case D3DFMT_A16B16G16R16F:
2342 {
2343 // float formats in D3D are stored rgba, rather than the other way round
2344 float abgr[4];
2345
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002346 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002347
2348 a = abgr[3];
2349 b = abgr[2];
2350 g = abgr[1];
2351 r = abgr[0];
2352 }
2353 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002354 default:
2355 UNIMPLEMENTED(); // FIXME
2356 UNREACHABLE();
2357 }
2358
2359 switch (format)
2360 {
2361 case GL_RGBA:
2362 switch (type)
2363 {
2364 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002365 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2366 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2367 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2368 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002369 break;
2370 default: UNREACHABLE();
2371 }
2372 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002373 case GL_BGRA_EXT:
2374 switch (type)
2375 {
2376 case GL_UNSIGNED_BYTE:
2377 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2378 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2379 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2380 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2381 break;
2382 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2383 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2384 // this type is packed as follows:
2385 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2386 // --------------------------------------------------------------------------------
2387 // | 4th | 3rd | 2nd | 1st component |
2388 // --------------------------------------------------------------------------------
2389 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2390 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2391 ((unsigned short)(15 * a + 0.5f) << 12)|
2392 ((unsigned short)(15 * r + 0.5f) << 8) |
2393 ((unsigned short)(15 * g + 0.5f) << 4) |
2394 ((unsigned short)(15 * b + 0.5f) << 0);
2395 break;
2396 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2397 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2398 // this type is packed as follows:
2399 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2400 // --------------------------------------------------------------------------------
2401 // | 4th | 3rd | 2nd | 1st component |
2402 // --------------------------------------------------------------------------------
2403 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2404 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2405 ((unsigned short)( a + 0.5f) << 15) |
2406 ((unsigned short)(31 * r + 0.5f) << 10) |
2407 ((unsigned short)(31 * g + 0.5f) << 5) |
2408 ((unsigned short)(31 * b + 0.5f) << 0);
2409 break;
2410 default: UNREACHABLE();
2411 }
2412 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002413 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 switch (type)
2415 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002416 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002417 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2418 ((unsigned short)(31 * b + 0.5f) << 0) |
2419 ((unsigned short)(63 * g + 0.5f) << 5) |
2420 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 break;
2422 default: UNREACHABLE();
2423 }
2424 break;
2425 default: UNREACHABLE();
2426 }
2427 }
2428 }
2429
2430 systemSurface->UnlockRect();
2431
2432 systemSurface->Release();
2433}
2434
2435void Context::clear(GLbitfield mask)
2436{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002437 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002438
2439 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2440 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002441 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002442 }
2443
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002444 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 IDirect3DDevice9 *device = getDevice();
2446 DWORD flags = 0;
2447
2448 if (mask & GL_COLOR_BUFFER_BIT)
2449 {
2450 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002451
2452 if (framebufferObject->getColorbufferType() != GL_NONE)
2453 {
2454 flags |= D3DCLEAR_TARGET;
2455 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 }
2457
2458 if (mask & GL_DEPTH_BUFFER_BIT)
2459 {
2460 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002461 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 {
2463 flags |= D3DCLEAR_ZBUFFER;
2464 }
2465 }
2466
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 GLuint stencilUnmasked = 0x0;
2468
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002469 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002470 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002472 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002474 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002475 if (!depthStencil)
2476 {
2477 ERR("Depth stencil pointer unexpectedly null.");
2478 return;
2479 }
2480
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002481 D3DSURFACE_DESC desc;
2482 depthStencil->GetDesc(&desc);
2483
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002484 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002485 stencilUnmasked = (0x1 << stencilSize) - 1;
2486
2487 if (stencilUnmasked != 0x0)
2488 {
2489 flags |= D3DCLEAR_STENCIL;
2490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 }
2492 }
2493
2494 if (mask != 0)
2495 {
2496 return error(GL_INVALID_VALUE);
2497 }
2498
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002499 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2500 {
2501 return;
2502 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002504 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002505 unorm<8>(mState.colorClearValue.red),
2506 unorm<8>(mState.colorClearValue.green),
2507 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002508 float depth = clamp01(mState.depthClearValue);
2509 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002510
2511 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2512
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002513 if (!renderTarget)
2514 {
2515 return; // Context must be lost, return silently
2516 }
2517
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518 D3DSURFACE_DESC desc;
2519 renderTarget->GetDesc(&desc);
2520
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002521 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002522
2523 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002524 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002526 !(mState.colorMaskRed && mState.colorMaskGreen &&
2527 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002528
2529 if (needMaskedColorClear || needMaskedStencilClear)
2530 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002531 // State which is altered in all paths from this point to the clear call is saved.
2532 // State which is altered in only some paths will be flagged dirty in the case that
2533 // that path is taken.
2534 HRESULT hr;
2535 if (mMaskedClearSavedState == NULL)
2536 {
2537 hr = device->BeginStateBlock();
2538 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2539
2540 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2541 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2542 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2543 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2544 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2545 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2546 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2547 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2548 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2549 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2550 device->SetPixelShader(NULL);
2551 device->SetVertexShader(NULL);
2552 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
jbauman@chromium.org23c9e312011-09-21 22:16:44 +00002553 device->SetStreamSource(0, NULL, 0, 0);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002554 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2555 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2556 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2557 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2558 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2559 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2560 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002561
2562 hr = device->EndStateBlock(&mMaskedClearSavedState);
2563 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2564 }
2565
2566 ASSERT(mMaskedClearSavedState != NULL);
2567
2568 if (mMaskedClearSavedState != NULL)
2569 {
2570 hr = mMaskedClearSavedState->Capture();
2571 ASSERT(SUCCEEDED(hr));
2572 }
2573
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002574 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2575 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2576 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2577 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2578 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2579 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2580 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2581 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2582
2583 if (flags & D3DCLEAR_TARGET)
2584 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002585 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002586 }
2587 else
2588 {
2589 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2590 }
2591
2592 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2593 {
2594 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2595 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2596 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2597 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002598 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002599 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2601 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002602 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603 }
2604 else
2605 {
2606 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2607 }
2608
2609 device->SetPixelShader(NULL);
2610 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002611 device->SetFVF(D3DFVF_XYZRHW);
2612 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2613 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2614 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2615 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2616 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2617 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2618 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002620 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2621 quad[0][0] = -0.5f;
2622 quad[0][1] = desc.Height - 0.5f;
2623 quad[0][2] = 0.0f;
2624 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002625
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002626 quad[1][0] = desc.Width - 0.5f;
2627 quad[1][1] = desc.Height - 0.5f;
2628 quad[1][2] = 0.0f;
2629 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002630
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002631 quad[2][0] = -0.5f;
2632 quad[2][1] = -0.5f;
2633 quad[2][2] = 0.0f;
2634 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002636 quad[3][0] = desc.Width - 0.5f;
2637 quad[3][1] = -0.5f;
2638 quad[3][2] = 0.0f;
2639 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002640
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002641 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002642 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002643
2644 if (flags & D3DCLEAR_ZBUFFER)
2645 {
2646 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2647 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2648 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2649 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002650
2651 if (mMaskedClearSavedState != NULL)
2652 {
2653 mMaskedClearSavedState->Apply();
2654 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002656 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002657 {
2658 device->Clear(0, NULL, flags, color, depth, stencil);
2659 }
2660}
2661
2662void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2663{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002664 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002665 {
2666 return error(GL_INVALID_OPERATION);
2667 }
2668
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002669 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002670 IDirect3DDevice9 *device = getDevice();
2671 D3DPRIMITIVETYPE primitiveType;
2672 int primitiveCount;
2673
2674 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2675 return error(GL_INVALID_ENUM);
2676
2677 if (primitiveCount <= 0)
2678 {
2679 return;
2680 }
2681
2682 if (!applyRenderTarget(false))
2683 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002684 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002685 }
2686
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002687 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002688
daniel@transgaming.com83921382011-01-08 05:46:00 +00002689 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002690 if (err != GL_NO_ERROR)
2691 {
2692 return error(err);
2693 }
2694
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002695 applyShaders();
2696 applyTextures();
2697
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002698 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002699 {
2700 return error(GL_INVALID_OPERATION);
2701 }
2702
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002703 if (!cullSkipsDraw(mode))
2704 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002705 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002706
2707 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002708
2709 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2710 {
2711 drawClosingLine(first, first + count - 1);
2712 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002713 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714}
2715
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002716void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002717{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002718 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719 {
2720 return error(GL_INVALID_OPERATION);
2721 }
2722
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002723 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724 {
2725 return error(GL_INVALID_OPERATION);
2726 }
2727
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002728 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002729 IDirect3DDevice9 *device = getDevice();
2730 D3DPRIMITIVETYPE primitiveType;
2731 int primitiveCount;
2732
2733 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2734 return error(GL_INVALID_ENUM);
2735
2736 if (primitiveCount <= 0)
2737 {
2738 return;
2739 }
2740
2741 if (!applyRenderTarget(false))
2742 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002743 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002744 }
2745
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002746 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002747
2748 TranslatedIndexData indexInfo;
2749 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2750 if (err != GL_NO_ERROR)
2751 {
2752 return error(err);
2753 }
2754
daniel@transgaming.com83921382011-01-08 05:46:00 +00002755 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2756 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002757 if (err != GL_NO_ERROR)
2758 {
2759 return error(err);
2760 }
2761
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762 applyShaders();
2763 applyTextures();
2764
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002765 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002766 {
2767 return error(GL_INVALID_OPERATION);
2768 }
2769
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002770 if (!cullSkipsDraw(mode))
2771 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002772 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002773
daniel@transgaming.com83921382011-01-08 05:46:00 +00002774 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002775
2776 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2777 {
2778 drawClosingLine(count, type, indices);
2779 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002780 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002781}
2782
2783void Context::finish()
2784{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002785 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786 IDirect3DDevice9 *device = getDevice();
2787 IDirect3DQuery9 *occlusionQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002788 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002790 result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2791 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002792 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002793 ERR("CreateQuery failed hr=%x\n", result);
2794 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2795 {
2796 return error(GL_OUT_OF_MEMORY);
2797 }
2798 ASSERT(false);
2799 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002800 }
2801
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002802 IDirect3DStateBlock9 *savedState = NULL;
2803 result = device->CreateStateBlock(D3DSBT_ALL, &savedState);
2804 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002806 ERR("CreateStateBlock failed hr=%x\n", result);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002808
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002809 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002810 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002811 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002812 }
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002813 ASSERT(false);
2814 return;
2815 }
2816
2817 result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2818 if (FAILED(result))
2819 {
2820 ERR("occlusionQuery->Issue(BEGIN) failed hr=%x\n", result);
2821 occlusionQuery->Release();
2822 savedState->Release();
2823 ASSERT(false);
2824 return;
2825 }
2826
2827 // Render something outside the render target
2828 device->SetPixelShader(NULL);
2829 device->SetVertexShader(NULL);
2830 device->SetFVF(D3DFVF_XYZRHW);
2831 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
2832 display->startScene();
2833 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
2834
2835 result = occlusionQuery->Issue(D3DISSUE_END);
2836 if (FAILED(result))
2837 {
2838 ERR("occlusionQuery->Issue(END) failed hr=%x\n", result);
2839 occlusionQuery->Release();
2840 savedState->Apply();
2841 savedState->Release();
2842 ASSERT(false);
2843 return;
2844 }
2845
2846 while ((result = occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)) == S_FALSE)
2847 {
2848 // Keep polling, but allow other threads to do something useful first
2849 Sleep(0);
2850 }
2851
2852 occlusionQuery->Release();
2853 savedState->Apply();
2854 savedState->Release();
2855
2856 if (result == D3DERR_DEVICELOST)
2857 {
2858 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002859 }
2860}
2861
2862void Context::flush()
2863{
2864 IDirect3DDevice9 *device = getDevice();
2865 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002866 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002868 result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2869 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002870 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002871 ERR("CreateQuery failed hr=%x\n", result);
2872 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2873 {
2874 return error(GL_OUT_OF_MEMORY);
2875 }
2876 ASSERT(false);
2877 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002878 }
2879
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002880 result = eventQuery->Issue(D3DISSUE_END);
2881 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002882 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002883 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2884 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002885 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002886 return;
2887 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002888
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002889 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2890 eventQuery->Release();
2891
2892 if (result == D3DERR_DEVICELOST)
2893 {
2894 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002895 }
2896}
2897
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002898void Context::drawClosingLine(unsigned int first, unsigned int last)
2899{
2900 IDirect3DDevice9 *device = getDevice();
2901 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002902 bool succeeded = false;
2903 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002904
2905 if (supports32bitIndices())
2906 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002907 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002908
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002909 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002910 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002911 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2912 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002913
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002914 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2915
2916 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2917 if (data)
2918 {
2919 data[0] = last;
2920 data[1] = first;
2921 mClosingIB->unmap();
2922 offset /= 4;
2923 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002924 }
2925 }
2926 else
2927 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002928 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002929
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002930 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002931 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002932 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2933 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002934
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002935 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2936
2937 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2938 if (data)
2939 {
2940 data[0] = last;
2941 data[1] = first;
2942 mClosingIB->unmap();
2943 offset /= 2;
2944 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002945 }
2946 }
2947
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002948 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002949 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002950 device->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002951 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002952
jbauman@chromium.org2c199b12011-06-13 22:20:06 +00002953 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002954 }
2955 else
2956 {
2957 ERR("Could not create an index buffer for closing a line loop.");
2958 error(GL_OUT_OF_MEMORY);
2959 }
2960}
2961
2962void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2963{
2964 unsigned int first = 0;
2965 unsigned int last = 0;
2966
2967 if (mState.elementArrayBuffer.get())
2968 {
2969 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2970 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2971 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2972 }
2973
2974 switch (type)
2975 {
2976 case GL_UNSIGNED_BYTE:
2977 first = static_cast<const GLubyte*>(indices)[0];
2978 last = static_cast<const GLubyte*>(indices)[count - 1];
2979 break;
2980 case GL_UNSIGNED_SHORT:
2981 first = static_cast<const GLushort*>(indices)[0];
2982 last = static_cast<const GLushort*>(indices)[count - 1];
2983 break;
2984 case GL_UNSIGNED_INT:
2985 first = static_cast<const GLuint*>(indices)[0];
2986 last = static_cast<const GLuint*>(indices)[count - 1];
2987 break;
2988 default: UNREACHABLE();
2989 }
2990
2991 drawClosingLine(first, last);
2992}
2993
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002994void Context::recordInvalidEnum()
2995{
2996 mInvalidEnum = true;
2997}
2998
2999void Context::recordInvalidValue()
3000{
3001 mInvalidValue = true;
3002}
3003
3004void Context::recordInvalidOperation()
3005{
3006 mInvalidOperation = true;
3007}
3008
3009void Context::recordOutOfMemory()
3010{
3011 mOutOfMemory = true;
3012}
3013
3014void Context::recordInvalidFramebufferOperation()
3015{
3016 mInvalidFramebufferOperation = true;
3017}
3018
3019// Get one of the recorded errors and clear its flag, if any.
3020// [OpenGL ES 2.0.24] section 2.5 page 13.
3021GLenum Context::getError()
3022{
3023 if (mInvalidEnum)
3024 {
3025 mInvalidEnum = false;
3026
3027 return GL_INVALID_ENUM;
3028 }
3029
3030 if (mInvalidValue)
3031 {
3032 mInvalidValue = false;
3033
3034 return GL_INVALID_VALUE;
3035 }
3036
3037 if (mInvalidOperation)
3038 {
3039 mInvalidOperation = false;
3040
3041 return GL_INVALID_OPERATION;
3042 }
3043
3044 if (mOutOfMemory)
3045 {
3046 mOutOfMemory = false;
3047
3048 return GL_OUT_OF_MEMORY;
3049 }
3050
3051 if (mInvalidFramebufferOperation)
3052 {
3053 mInvalidFramebufferOperation = false;
3054
3055 return GL_INVALID_FRAMEBUFFER_OPERATION;
3056 }
3057
3058 return GL_NO_ERROR;
3059}
3060
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003061bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003062{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003063 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003064}
3065
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003066int Context::getMaximumVaryingVectors() const
3067{
3068 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3069}
3070
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003071unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003072{
3073 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3074}
3075
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003076unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003077{
3078 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3079}
3080
daniel@transgaming.com458da142010-11-28 02:03:02 +00003081int Context::getMaximumFragmentUniformVectors() const
3082{
3083 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3084}
3085
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003086int Context::getMaxSupportedSamples() const
3087{
3088 return mMaxSupportedSamples;
3089}
3090
3091int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3092{
3093 if (requested == 0)
3094 {
3095 return requested;
3096 }
3097
3098 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3099 if (itr == mMultiSampleSupport.end())
3100 {
3101 return -1;
3102 }
3103
3104 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3105 {
3106 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3107 {
3108 return i;
3109 }
3110 }
3111
3112 return -1;
3113}
3114
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003115bool Context::supportsEventQueries() const
3116{
3117 return mSupportsEventQueries;
3118}
3119
gman@chromium.org50c526d2011-08-10 05:19:44 +00003120bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003121{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003122 return mSupportsDXT1Textures;
3123}
3124
3125bool Context::supportsDXT3Textures() const
3126{
3127 return mSupportsDXT3Textures;
3128}
3129
3130bool Context::supportsDXT5Textures() const
3131{
3132 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003133}
3134
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003135bool Context::supportsFloatTextures() const
3136{
3137 return mSupportsFloatTextures;
3138}
3139
3140bool Context::supportsFloatLinearFilter() const
3141{
3142 return mSupportsFloatLinearFilter;
3143}
3144
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003145bool Context::supportsFloatRenderableTextures() const
3146{
3147 return mSupportsFloatRenderableTextures;
3148}
3149
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003150bool Context::supportsHalfFloatTextures() const
3151{
3152 return mSupportsHalfFloatTextures;
3153}
3154
3155bool Context::supportsHalfFloatLinearFilter() const
3156{
3157 return mSupportsHalfFloatLinearFilter;
3158}
3159
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003160bool Context::supportsHalfFloatRenderableTextures() const
3161{
3162 return mSupportsHalfFloatRenderableTextures;
3163}
3164
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003165int Context::getMaximumRenderbufferDimension() const
3166{
3167 return mMaxRenderbufferDimension;
3168}
3169
3170int Context::getMaximumTextureDimension() const
3171{
3172 return mMaxTextureDimension;
3173}
3174
3175int Context::getMaximumCubeTextureDimension() const
3176{
3177 return mMaxCubeTextureDimension;
3178}
3179
3180int Context::getMaximumTextureLevel() const
3181{
3182 return mMaxTextureLevel;
3183}
3184
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003185bool Context::supportsLuminanceTextures() const
3186{
3187 return mSupportsLuminanceTextures;
3188}
3189
3190bool Context::supportsLuminanceAlphaTextures() const
3191{
3192 return mSupportsLuminanceAlphaTextures;
3193}
3194
daniel@transgaming.com83921382011-01-08 05:46:00 +00003195bool Context::supports32bitIndices() const
3196{
3197 return mSupports32bitIndices;
3198}
3199
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003200bool Context::supportsNonPower2Texture() const
3201{
3202 return mSupportsNonPower2Texture;
3203}
3204
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003205void Context::detachBuffer(GLuint buffer)
3206{
3207 // [OpenGL ES 2.0.24] section 2.9 page 22:
3208 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3209 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3210
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003211 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003212 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003213 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003214 }
3215
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003216 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003217 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003218 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003219 }
3220
3221 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3222 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003223 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003224 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003225 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003226 }
3227 }
3228}
3229
3230void Context::detachTexture(GLuint texture)
3231{
3232 // [OpenGL ES 2.0.24] section 3.8 page 84:
3233 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3234 // rebound to texture object zero
3235
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003236 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003237 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003238 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003239 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003240 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003241 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003242 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003243 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003244 }
3245 }
3246
3247 // [OpenGL ES 2.0.24] section 4.4 page 112:
3248 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3249 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3250 // image was attached in the currently bound framebuffer.
3251
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003252 Framebuffer *readFramebuffer = getReadFramebuffer();
3253 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003254
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003255 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003256 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003257 readFramebuffer->detachTexture(texture);
3258 }
3259
3260 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3261 {
3262 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003263 }
3264}
3265
3266void Context::detachFramebuffer(GLuint framebuffer)
3267{
3268 // [OpenGL ES 2.0.24] section 4.4 page 107:
3269 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3270 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3271
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003272 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003273 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003274 bindReadFramebuffer(0);
3275 }
3276
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003277 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003278 {
3279 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003280 }
3281}
3282
3283void Context::detachRenderbuffer(GLuint renderbuffer)
3284{
3285 // [OpenGL ES 2.0.24] section 4.4 page 109:
3286 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3287 // had been executed with the target RENDERBUFFER and name of zero.
3288
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003289 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003290 {
3291 bindRenderbuffer(0);
3292 }
3293
3294 // [OpenGL ES 2.0.24] section 4.4 page 111:
3295 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3296 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3297 // point to which this image was attached in the currently bound framebuffer.
3298
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003299 Framebuffer *readFramebuffer = getReadFramebuffer();
3300 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003301
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003302 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003303 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003304 readFramebuffer->detachRenderbuffer(renderbuffer);
3305 }
3306
3307 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3308 {
3309 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003310 }
3311}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003312
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003313Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003314{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003315 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003316
3317 if (t == NULL)
3318 {
3319 static const GLubyte color[] = { 0, 0, 0, 255 };
3320
3321 switch (type)
3322 {
3323 default:
3324 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003325 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003326
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003327 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003328 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003329 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003330 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003331 t = incomplete2d;
3332 }
3333 break;
3334
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003335 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003336 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003337 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003338
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003339 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3340 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3341 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3342 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3343 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3344 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003345
3346 t = incompleteCube;
3347 }
3348 break;
3349 }
3350
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003351 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003352 }
3353
3354 return t;
3355}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003356
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003357bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003358{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003359 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003360}
3361
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003362bool Context::isTriangleMode(GLenum drawMode)
3363{
3364 switch (drawMode)
3365 {
3366 case GL_TRIANGLES:
3367 case GL_TRIANGLE_FAN:
3368 case GL_TRIANGLE_STRIP:
3369 return true;
3370 case GL_POINTS:
3371 case GL_LINES:
3372 case GL_LINE_LOOP:
3373 case GL_LINE_STRIP:
3374 return false;
3375 default: UNREACHABLE();
3376 }
3377
3378 return false;
3379}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003380
3381void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3382{
3383 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3384
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003385 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3386 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3387 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3388 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003389
daniel@transgaming.com83921382011-01-08 05:46:00 +00003390 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003391}
3392
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003393// keep list sorted in following order
3394// OES extensions
3395// EXT extensions
3396// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003397void Context::initExtensionString()
3398{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003399 mExtensionString = "";
3400
3401 // OES extensions
3402 if (supports32bitIndices())
3403 {
3404 mExtensionString += "GL_OES_element_index_uint ";
3405 }
3406
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003407 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003408 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003409 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003410
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003411 if (supportsHalfFloatTextures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003412 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003413 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003414 }
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003415 if (supportsHalfFloatLinearFilter())
3416 {
3417 mExtensionString += "GL_OES_texture_half_float_linear ";
3418 }
3419 if (supportsFloatTextures())
3420 {
3421 mExtensionString += "GL_OES_texture_float ";
3422 }
3423 if (supportsFloatLinearFilter())
3424 {
3425 mExtensionString += "GL_OES_texture_float_linear ";
3426 }
3427
3428 if (supportsNonPower2Texture())
3429 {
3430 mExtensionString += "GL_OES_texture_npot ";
3431 }
3432
3433 // Multi-vendor (EXT) extensions
3434 mExtensionString += "GL_EXT_read_format_bgra ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003435
gman@chromium.org50c526d2011-08-10 05:19:44 +00003436 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003437 {
3438 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3439 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003440
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003441 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003442
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003443 // ANGLE-specific extensions
3444 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003445 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003446 {
3447 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3448 }
3449
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003450 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003451 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003452 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3453 }
3454 if (supportsDXT5Textures())
3455 {
3456 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003457 }
zmo@google.coma574f782011-10-03 21:45:23 +00003458 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003459
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003460 // Other vendor-specific extensions
3461 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003462 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003463 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003464 }
3465
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003466 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3467 if (end != std::string::npos)
3468 {
3469 mExtensionString.resize(end+1);
3470 }
3471}
3472
3473const char *Context::getExtensionString() const
3474{
3475 return mExtensionString.c_str();
3476}
3477
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003478void Context::initRendererString()
3479{
3480 egl::Display *display = getDisplay();
3481 D3DADAPTER_IDENTIFIER9 *identifier = display->getAdapterIdentifier();
3482
3483 mRendererString = "ANGLE (";
3484 mRendererString += identifier->Description;
3485 mRendererString += ")";
3486}
3487
3488const char *Context::getRendererString() const
3489{
3490 return mRendererString.c_str();
3491}
3492
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003493void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3494 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3495 GLbitfield mask)
3496{
3497 IDirect3DDevice9 *device = getDevice();
3498
3499 Framebuffer *readFramebuffer = getReadFramebuffer();
3500 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3501
3502 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3503 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3504 {
3505 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3506 }
3507
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003508 if (drawFramebuffer->getSamples() != 0)
3509 {
3510 return error(GL_INVALID_OPERATION);
3511 }
3512
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003513 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3514 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3515 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3516 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3517
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003518 RECT sourceRect;
3519 RECT destRect;
3520
3521 if (srcX0 < srcX1)
3522 {
3523 sourceRect.left = srcX0;
3524 sourceRect.right = srcX1;
3525 destRect.left = dstX0;
3526 destRect.right = dstX1;
3527 }
3528 else
3529 {
3530 sourceRect.left = srcX1;
3531 destRect.left = dstX1;
3532 sourceRect.right = srcX0;
3533 destRect.right = dstX0;
3534 }
3535
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003536 if (srcY0 < srcY1)
3537 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003538 sourceRect.top = readBufferHeight - srcY1;
3539 destRect.top = drawBufferHeight - dstY1;
3540 sourceRect.bottom = readBufferHeight - srcY0;
3541 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003542 }
3543 else
3544 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003545 sourceRect.top = readBufferHeight - srcY0;
3546 destRect.top = drawBufferHeight - dstY0;
3547 sourceRect.bottom = readBufferHeight - srcY1;
3548 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003549 }
3550
3551 RECT sourceScissoredRect = sourceRect;
3552 RECT destScissoredRect = destRect;
3553
3554 if (mState.scissorTest)
3555 {
3556 // Only write to parts of the destination framebuffer which pass the scissor test
3557 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3558 // rect will be checked against scissorY, rather than the bottom.
3559 if (destRect.left < mState.scissorX)
3560 {
3561 int xDiff = mState.scissorX - destRect.left;
3562 destScissoredRect.left = mState.scissorX;
3563 sourceScissoredRect.left += xDiff;
3564 }
3565
3566 if (destRect.right > mState.scissorX + mState.scissorWidth)
3567 {
3568 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3569 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3570 sourceScissoredRect.right -= xDiff;
3571 }
3572
3573 if (destRect.top < mState.scissorY)
3574 {
3575 int yDiff = mState.scissorY - destRect.top;
3576 destScissoredRect.top = mState.scissorY;
3577 sourceScissoredRect.top += yDiff;
3578 }
3579
3580 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3581 {
3582 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3583 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3584 sourceScissoredRect.bottom -= yDiff;
3585 }
3586 }
3587
3588 bool blitRenderTarget = false;
3589 bool blitDepthStencil = false;
3590
3591 RECT sourceTrimmedRect = sourceScissoredRect;
3592 RECT destTrimmedRect = destScissoredRect;
3593
3594 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3595 // the actual draw and read surfaces.
3596 if (sourceTrimmedRect.left < 0)
3597 {
3598 int xDiff = 0 - sourceTrimmedRect.left;
3599 sourceTrimmedRect.left = 0;
3600 destTrimmedRect.left += xDiff;
3601 }
3602
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003603 if (sourceTrimmedRect.right > readBufferWidth)
3604 {
3605 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3606 sourceTrimmedRect.right = readBufferWidth;
3607 destTrimmedRect.right -= xDiff;
3608 }
3609
3610 if (sourceTrimmedRect.top < 0)
3611 {
3612 int yDiff = 0 - sourceTrimmedRect.top;
3613 sourceTrimmedRect.top = 0;
3614 destTrimmedRect.top += yDiff;
3615 }
3616
3617 if (sourceTrimmedRect.bottom > readBufferHeight)
3618 {
3619 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3620 sourceTrimmedRect.bottom = readBufferHeight;
3621 destTrimmedRect.bottom -= yDiff;
3622 }
3623
3624 if (destTrimmedRect.left < 0)
3625 {
3626 int xDiff = 0 - destTrimmedRect.left;
3627 destTrimmedRect.left = 0;
3628 sourceTrimmedRect.left += xDiff;
3629 }
3630
3631 if (destTrimmedRect.right > drawBufferWidth)
3632 {
3633 int xDiff = destTrimmedRect.right - drawBufferWidth;
3634 destTrimmedRect.right = drawBufferWidth;
3635 sourceTrimmedRect.right -= xDiff;
3636 }
3637
3638 if (destTrimmedRect.top < 0)
3639 {
3640 int yDiff = 0 - destTrimmedRect.top;
3641 destTrimmedRect.top = 0;
3642 sourceTrimmedRect.top += yDiff;
3643 }
3644
3645 if (destTrimmedRect.bottom > drawBufferHeight)
3646 {
3647 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3648 destTrimmedRect.bottom = drawBufferHeight;
3649 sourceTrimmedRect.bottom -= yDiff;
3650 }
3651
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003652 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003653 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3654 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3655 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3656 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003657 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3658 {
3659 partialBufferCopy = true;
3660 }
3661
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003662 if (mask & GL_COLOR_BUFFER_BIT)
3663 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003664 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3665 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3666 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3667 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3668 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003669 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3670 {
3671 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3672 return error(GL_INVALID_OPERATION);
3673 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003674
3675 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3676 {
3677 return error(GL_INVALID_OPERATION);
3678 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003679
3680 blitRenderTarget = true;
3681
3682 }
3683
3684 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3685 {
3686 DepthStencilbuffer *readDSBuffer = NULL;
3687 DepthStencilbuffer *drawDSBuffer = NULL;
3688
3689 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3690 // both a depth and stencil buffer, it will be the same buffer.
3691
3692 if (mask & GL_DEPTH_BUFFER_BIT)
3693 {
3694 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3695 {
3696 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3697 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3698 {
3699 return error(GL_INVALID_OPERATION);
3700 }
3701
3702 blitDepthStencil = true;
3703 readDSBuffer = readFramebuffer->getDepthbuffer();
3704 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3705 }
3706 }
3707
3708 if (mask & GL_STENCIL_BUFFER_BIT)
3709 {
3710 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3711 {
3712 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3713 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3714 {
3715 return error(GL_INVALID_OPERATION);
3716 }
3717
3718 blitDepthStencil = true;
3719 readDSBuffer = readFramebuffer->getStencilbuffer();
3720 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3721 }
3722 }
3723
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003724 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003725 {
3726 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3727 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3728 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003729
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003730 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3731 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003732 {
3733 return error(GL_INVALID_OPERATION);
3734 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003735 }
3736
3737 if (blitRenderTarget || blitDepthStencil)
3738 {
3739 egl::Display *display = getDisplay();
3740 display->endScene();
3741
3742 if (blitRenderTarget)
3743 {
3744 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3745 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3746
3747 if (FAILED(result))
3748 {
3749 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3750 return;
3751 }
3752 }
3753
3754 if (blitDepthStencil)
3755 {
3756 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3757
3758 if (FAILED(result))
3759 {
3760 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3761 return;
3762 }
3763 }
3764 }
3765}
3766
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003767VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3768{
3769 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3770 {
3771 mVertexDeclCache[i].vertexDeclaration = NULL;
3772 mVertexDeclCache[i].lruCount = 0;
3773 }
3774}
3775
3776VertexDeclarationCache::~VertexDeclarationCache()
3777{
3778 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3779 {
3780 if (mVertexDeclCache[i].vertexDeclaration)
3781 {
3782 mVertexDeclCache[i].vertexDeclaration->Release();
3783 }
3784 }
3785}
3786
3787GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3788{
3789 IDirect3DDevice9 *device = getDevice();
3790
3791 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3792 D3DVERTEXELEMENT9 *element = &elements[0];
3793
3794 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3795 {
3796 if (attributes[i].active)
3797 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003798 if (mAppliedVBs[i].serial != attributes[i].serial ||
3799 mAppliedVBs[i].stride != attributes[i].stride ||
3800 mAppliedVBs[i].offset != attributes[i].offset)
3801 {
3802 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3803 mAppliedVBs[i].serial = attributes[i].serial;
3804 mAppliedVBs[i].stride = attributes[i].stride;
3805 mAppliedVBs[i].offset = attributes[i].offset;
3806 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003807
3808 element->Stream = i;
3809 element->Offset = 0;
3810 element->Type = attributes[i].type;
3811 element->Method = D3DDECLMETHOD_DEFAULT;
3812 element->Usage = D3DDECLUSAGE_TEXCOORD;
3813 element->UsageIndex = program->getSemanticIndex(i);
3814 element++;
3815 }
3816 }
3817
3818 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3819 *(element++) = end;
3820
3821 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3822 {
3823 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3824 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3825 {
3826 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003827 if(entry->vertexDeclaration != mLastSetVDecl)
3828 {
3829 device->SetVertexDeclaration(entry->vertexDeclaration);
3830 mLastSetVDecl = entry->vertexDeclaration;
3831 }
3832
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003833 return GL_NO_ERROR;
3834 }
3835 }
3836
3837 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3838
3839 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3840 {
3841 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3842 {
3843 lastCache = &mVertexDeclCache[i];
3844 }
3845 }
3846
3847 if (lastCache->vertexDeclaration != NULL)
3848 {
3849 lastCache->vertexDeclaration->Release();
3850 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003851 // mLastSetVDecl is set to the replacement, so we don't have to worry
3852 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003853 }
3854
3855 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3856 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3857 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003858 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003859 lastCache->lruCount = ++mMaxLru;
3860
3861 return GL_NO_ERROR;
3862}
3863
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003864void VertexDeclarationCache::markStateDirty()
3865{
3866 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3867 {
3868 mAppliedVBs[i].serial = 0;
3869 }
3870
3871 mLastSetVDecl = NULL;
3872}
3873
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003874}
3875
3876extern "C"
3877{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003878gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003879{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003880 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003881}
3882
3883void glDestroyContext(gl::Context *context)
3884{
3885 delete context;
3886
3887 if (context == gl::getContext())
3888 {
3889 gl::makeCurrent(NULL, NULL, NULL);
3890 }
3891}
3892
3893void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3894{
3895 gl::makeCurrent(context, display, surface);
3896}
3897
3898gl::Context *glGetCurrentContext()
3899{
3900 return gl::getContext();
3901}
3902}