blob: 5f92fc074066c646f4f51e7d5b3a7253d3013973 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// 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
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034namespace gl
35{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +000036Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037{
benvanik@google.com1a233342011-04-28 19:44:39 +000038 mFenceHandleAllocator.setBaseHandle(0);
39
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000041
daniel@transgaming.com428d1582010-05-04 03:35:25 +000042 mState.depthClearValue = 1.0f;
43 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044
daniel@transgaming.com428d1582010-05-04 03:35:25 +000045 mState.cullFace = false;
46 mState.cullMode = GL_BACK;
47 mState.frontFace = GL_CCW;
48 mState.depthTest = false;
49 mState.depthFunc = GL_LESS;
50 mState.blend = false;
51 mState.sourceBlendRGB = GL_ONE;
52 mState.sourceBlendAlpha = GL_ONE;
53 mState.destBlendRGB = GL_ZERO;
54 mState.destBlendAlpha = GL_ZERO;
55 mState.blendEquationRGB = GL_FUNC_ADD;
56 mState.blendEquationAlpha = GL_FUNC_ADD;
57 mState.blendColor.red = 0;
58 mState.blendColor.green = 0;
59 mState.blendColor.blue = 0;
60 mState.blendColor.alpha = 0;
61 mState.stencilTest = false;
62 mState.stencilFunc = GL_ALWAYS;
63 mState.stencilRef = 0;
64 mState.stencilMask = -1;
65 mState.stencilWritemask = -1;
66 mState.stencilBackFunc = GL_ALWAYS;
67 mState.stencilBackRef = 0;
68 mState.stencilBackMask = - 1;
69 mState.stencilBackWritemask = -1;
70 mState.stencilFail = GL_KEEP;
71 mState.stencilPassDepthFail = GL_KEEP;
72 mState.stencilPassDepthPass = GL_KEEP;
73 mState.stencilBackFail = GL_KEEP;
74 mState.stencilBackPassDepthFail = GL_KEEP;
75 mState.stencilBackPassDepthPass = GL_KEEP;
76 mState.polygonOffsetFill = false;
77 mState.polygonOffsetFactor = 0.0f;
78 mState.polygonOffsetUnits = 0.0f;
79 mState.sampleAlphaToCoverage = false;
80 mState.sampleCoverage = false;
81 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000082 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000083 mState.scissorTest = false;
84 mState.dither = true;
85 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000086 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000089
daniel@transgaming.com428d1582010-05-04 03:35:25 +000090 mState.viewportX = 0;
91 mState.viewportY = 0;
92 mState.viewportWidth = config->mDisplayMode.Width;
93 mState.viewportHeight = config->mDisplayMode.Height;
94 mState.zNear = 0.0f;
95 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096
daniel@transgaming.com428d1582010-05-04 03:35:25 +000097 mState.scissorX = 0;
98 mState.scissorY = 0;
99 mState.scissorWidth = config->mDisplayMode.Width;
100 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000102 mState.colorMaskRed = true;
103 mState.colorMaskGreen = true;
104 mState.colorMaskBlue = true;
105 mState.colorMaskAlpha = true;
106 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000108 if (shareContext != NULL)
109 {
110 mResourceManager = shareContext->mResourceManager;
111 mResourceManager->addRef();
112 }
113 else
114 {
115 mResourceManager = new ResourceManager();
116 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000117
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118 // [OpenGL ES 2.0.24] section 3.7 page 83:
119 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
120 // and cube map texture state vectors respectively associated with them.
121 // In order that access to these initial textures not be lost, they are treated as texture
122 // objects all of whose names are 0.
123
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000124 mTexture2DZero.set(new Texture2D(0));
125 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000127 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000128 bindArrayBuffer(0);
129 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130 bindTextureCubeMap(0);
131 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000132 bindReadFramebuffer(0);
133 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134 bindRenderbuffer(0);
135
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000136 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000138 mState.packAlignment = 4;
139 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000140
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000141 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000142 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000143 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000144
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145 mInvalidEnum = false;
146 mInvalidValue = false;
147 mInvalidOperation = false;
148 mOutOfMemory = false;
149 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000150
151 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000152
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000153 mSupportsCompressedTextures = false;
154 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000155 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000156 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000157 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158}
159
160Context::~Context()
161{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000162 if (mState.currentProgram != 0)
163 {
164 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
165 if (programObject)
166 {
167 programObject->release();
168 }
169 mState.currentProgram = 0;
170 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000172 while (!mFramebufferMap.empty())
173 {
174 deleteFramebuffer(mFramebufferMap.begin()->first);
175 }
176
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000177 while (!mFenceMap.empty())
178 {
179 deleteFence(mFenceMap.begin()->first);
180 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000181
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000182 while (!mMultiSampleSupport.empty())
183 {
184 delete [] mMultiSampleSupport.begin()->second;
185 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
186 }
187
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000188 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
189 {
190 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
191 {
192 mState.samplerTexture[type][sampler].set(NULL);
193 }
194 }
195
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000196 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
197 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000198 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000199 }
200
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000201 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
202 {
203 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
204 }
205
206 mState.arrayBuffer.set(NULL);
207 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000208 mState.renderbuffer.set(NULL);
209
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000210 mTexture2DZero.set(NULL);
211 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000213 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000214 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000215 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000216
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000217 if (mMaskedClearSavedState)
218 {
219 mMaskedClearSavedState->Release();
220 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000221
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000222 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223}
224
225void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
226{
227 IDirect3DDevice9 *device = display->getDevice();
228
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000229 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000230 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000231 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000232
daniel@transgaming.com83921382011-01-08 05:46:00 +0000233 mVertexDataManager = new VertexDataManager(this, device);
234 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000235 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000236
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000237 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
238
239 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
240 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
241 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
242 mMaxRenderbufferDimension = mMaxTextureDimension;
243 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
244 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
245 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
246
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000247 const D3DFORMAT renderBufferFormats[] =
248 {
249 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000250 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000251 D3DFMT_R5G6B5,
252 D3DFMT_D24S8
253 };
254
255 int max = 0;
256 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
257 {
258 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
259 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
260 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
261
262 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
263 {
264 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
265 {
266 max = j;
267 }
268 }
269 }
270
271 mMaxSupportedSamples = max;
272
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000273 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000274 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000275 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
276 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000277 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
278 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000279
daniel@transgaming.com83921382011-01-08 05:46:00 +0000280 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
281
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000282 initExtensionString();
283
284 mState.viewportX = 0;
285 mState.viewportY = 0;
286 mState.viewportWidth = surface->getWidth();
287 mState.viewportHeight = surface->getHeight();
288
289 mState.scissorX = 0;
290 mState.scissorY = 0;
291 mState.scissorWidth = surface->getWidth();
292 mState.scissorHeight = surface->getHeight();
293
294 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000295 }
296
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
298 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000299 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000302 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000303 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
305 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000307 if (defaultRenderTarget)
308 {
309 defaultRenderTarget->Release();
310 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000312 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000314 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000316
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000317 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318}
319
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000320// 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 +0000321void Context::markAllStateDirty()
322{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000323 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
324 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000325 mAppliedTextureSerial[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000326 }
327
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000328 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000329 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000330 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000331 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000332 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000333
334 mClearStateDirty = true;
335 mCullStateDirty = true;
336 mDepthStateDirty = true;
337 mMaskStateDirty = true;
338 mBlendStateDirty = true;
339 mStencilStateDirty = true;
340 mPolygonOffsetStateDirty = true;
341 mScissorStateDirty = true;
342 mSampleStateDirty = true;
343 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000344 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000345}
346
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347void Context::setClearColor(float red, float green, float blue, float alpha)
348{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000349 mState.colorClearValue.red = red;
350 mState.colorClearValue.green = green;
351 mState.colorClearValue.blue = blue;
352 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353}
354
355void Context::setClearDepth(float depth)
356{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000357 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358}
359
360void Context::setClearStencil(int stencil)
361{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000362 mState.stencilClearValue = stencil;
363}
364
365void Context::setCullFace(bool enabled)
366{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000367 if (mState.cullFace != enabled)
368 {
369 mState.cullFace = enabled;
370 mCullStateDirty = true;
371 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000372}
373
374bool Context::isCullFaceEnabled() const
375{
376 return mState.cullFace;
377}
378
379void Context::setCullMode(GLenum mode)
380{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000381 if (mState.cullMode != mode)
382 {
383 mState.cullMode = mode;
384 mCullStateDirty = true;
385 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000386}
387
388void Context::setFrontFace(GLenum front)
389{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000390 if (mState.frontFace != front)
391 {
392 mState.frontFace = front;
393 mFrontFaceDirty = true;
394 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000395}
396
397void Context::setDepthTest(bool enabled)
398{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000399 if (mState.depthTest != enabled)
400 {
401 mState.depthTest = enabled;
402 mDepthStateDirty = true;
403 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000404}
405
406bool Context::isDepthTestEnabled() const
407{
408 return mState.depthTest;
409}
410
411void Context::setDepthFunc(GLenum depthFunc)
412{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000413 if (mState.depthFunc != depthFunc)
414 {
415 mState.depthFunc = depthFunc;
416 mDepthStateDirty = true;
417 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000418}
419
420void Context::setDepthRange(float zNear, float zFar)
421{
422 mState.zNear = zNear;
423 mState.zFar = zFar;
424}
425
426void Context::setBlend(bool enabled)
427{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000428 if (mState.blend != enabled)
429 {
430 mState.blend = enabled;
431 mBlendStateDirty = true;
432 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000433}
434
435bool Context::isBlendEnabled() const
436{
437 return mState.blend;
438}
439
440void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
441{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000442 if (mState.sourceBlendRGB != sourceRGB ||
443 mState.sourceBlendAlpha != sourceAlpha ||
444 mState.destBlendRGB != destRGB ||
445 mState.destBlendAlpha != destAlpha)
446 {
447 mState.sourceBlendRGB = sourceRGB;
448 mState.destBlendRGB = destRGB;
449 mState.sourceBlendAlpha = sourceAlpha;
450 mState.destBlendAlpha = destAlpha;
451 mBlendStateDirty = true;
452 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000453}
454
455void Context::setBlendColor(float red, float green, float blue, float alpha)
456{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000457 if (mState.blendColor.red != red ||
458 mState.blendColor.green != green ||
459 mState.blendColor.blue != blue ||
460 mState.blendColor.alpha != alpha)
461 {
462 mState.blendColor.red = red;
463 mState.blendColor.green = green;
464 mState.blendColor.blue = blue;
465 mState.blendColor.alpha = alpha;
466 mBlendStateDirty = true;
467 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000468}
469
470void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
471{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000472 if (mState.blendEquationRGB != rgbEquation ||
473 mState.blendEquationAlpha != alphaEquation)
474 {
475 mState.blendEquationRGB = rgbEquation;
476 mState.blendEquationAlpha = alphaEquation;
477 mBlendStateDirty = true;
478 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000479}
480
481void Context::setStencilTest(bool enabled)
482{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000483 if (mState.stencilTest != enabled)
484 {
485 mState.stencilTest = enabled;
486 mStencilStateDirty = true;
487 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000488}
489
490bool Context::isStencilTestEnabled() const
491{
492 return mState.stencilTest;
493}
494
495void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
496{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 if (mState.stencilFunc != stencilFunc ||
498 mState.stencilRef != stencilRef ||
499 mState.stencilMask != stencilMask)
500 {
501 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000502 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000503 mState.stencilMask = stencilMask;
504 mStencilStateDirty = true;
505 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000506}
507
508void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
509{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 if (mState.stencilBackFunc != stencilBackFunc ||
511 mState.stencilBackRef != stencilBackRef ||
512 mState.stencilBackMask != stencilBackMask)
513 {
514 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000515 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000516 mState.stencilBackMask = stencilBackMask;
517 mStencilStateDirty = true;
518 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000519}
520
521void Context::setStencilWritemask(GLuint stencilWritemask)
522{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000523 if (mState.stencilWritemask != stencilWritemask)
524 {
525 mState.stencilWritemask = stencilWritemask;
526 mStencilStateDirty = true;
527 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000528}
529
530void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
531{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000532 if (mState.stencilBackWritemask != stencilBackWritemask)
533 {
534 mState.stencilBackWritemask = stencilBackWritemask;
535 mStencilStateDirty = true;
536 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000537}
538
539void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
540{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000541 if (mState.stencilFail != stencilFail ||
542 mState.stencilPassDepthFail != stencilPassDepthFail ||
543 mState.stencilPassDepthPass != stencilPassDepthPass)
544 {
545 mState.stencilFail = stencilFail;
546 mState.stencilPassDepthFail = stencilPassDepthFail;
547 mState.stencilPassDepthPass = stencilPassDepthPass;
548 mStencilStateDirty = true;
549 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000550}
551
552void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
553{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000554 if (mState.stencilBackFail != stencilBackFail ||
555 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
556 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
557 {
558 mState.stencilBackFail = stencilBackFail;
559 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
560 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
561 mStencilStateDirty = true;
562 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000563}
564
565void Context::setPolygonOffsetFill(bool enabled)
566{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000567 if (mState.polygonOffsetFill != enabled)
568 {
569 mState.polygonOffsetFill = enabled;
570 mPolygonOffsetStateDirty = true;
571 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000572}
573
574bool Context::isPolygonOffsetFillEnabled() const
575{
576 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000577
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000578}
579
580void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
581{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000582 if (mState.polygonOffsetFactor != factor ||
583 mState.polygonOffsetUnits != units)
584 {
585 mState.polygonOffsetFactor = factor;
586 mState.polygonOffsetUnits = units;
587 mPolygonOffsetStateDirty = true;
588 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000589}
590
591void Context::setSampleAlphaToCoverage(bool enabled)
592{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000593 if (mState.sampleAlphaToCoverage != enabled)
594 {
595 mState.sampleAlphaToCoverage = enabled;
596 mSampleStateDirty = true;
597 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000598}
599
600bool Context::isSampleAlphaToCoverageEnabled() const
601{
602 return mState.sampleAlphaToCoverage;
603}
604
605void Context::setSampleCoverage(bool enabled)
606{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000607 if (mState.sampleCoverage != enabled)
608 {
609 mState.sampleCoverage = enabled;
610 mSampleStateDirty = true;
611 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000612}
613
614bool Context::isSampleCoverageEnabled() const
615{
616 return mState.sampleCoverage;
617}
618
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000619void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000620{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000621 if (mState.sampleCoverageValue != value ||
622 mState.sampleCoverageInvert != invert)
623 {
624 mState.sampleCoverageValue = value;
625 mState.sampleCoverageInvert = invert;
626 mSampleStateDirty = true;
627 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000628}
629
630void Context::setScissorTest(bool enabled)
631{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000632 if (mState.scissorTest != enabled)
633 {
634 mState.scissorTest = enabled;
635 mScissorStateDirty = true;
636 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000637}
638
639bool Context::isScissorTestEnabled() const
640{
641 return mState.scissorTest;
642}
643
644void Context::setDither(bool enabled)
645{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000646 if (mState.dither != enabled)
647 {
648 mState.dither = enabled;
649 mDitherStateDirty = true;
650 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000651}
652
653bool Context::isDitherEnabled() const
654{
655 return mState.dither;
656}
657
658void Context::setLineWidth(GLfloat width)
659{
660 mState.lineWidth = width;
661}
662
663void Context::setGenerateMipmapHint(GLenum hint)
664{
665 mState.generateMipmapHint = hint;
666}
667
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000668void Context::setFragmentShaderDerivativeHint(GLenum hint)
669{
670 mState.fragmentShaderDerivativeHint = hint;
671 // TODO: Propagate the hint to shader translator so we can write
672 // ddx, ddx_coarse, or ddx_fine depending on the hint.
673 // Ignore for now. It is valid for implementations to ignore hint.
674}
675
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000676void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
677{
678 mState.viewportX = x;
679 mState.viewportY = y;
680 mState.viewportWidth = width;
681 mState.viewportHeight = height;
682}
683
684void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
685{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000686 if (mState.scissorX != x || mState.scissorY != y ||
687 mState.scissorWidth != width || mState.scissorHeight != height)
688 {
689 mState.scissorX = x;
690 mState.scissorY = y;
691 mState.scissorWidth = width;
692 mState.scissorHeight = height;
693 mScissorStateDirty = true;
694 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000695}
696
697void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
698{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000699 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
700 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
701 {
702 mState.colorMaskRed = red;
703 mState.colorMaskGreen = green;
704 mState.colorMaskBlue = blue;
705 mState.colorMaskAlpha = alpha;
706 mMaskStateDirty = true;
707 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000708}
709
710void Context::setDepthMask(bool mask)
711{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000712 if (mState.depthMask != mask)
713 {
714 mState.depthMask = mask;
715 mMaskStateDirty = true;
716 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000717}
718
719void Context::setActiveSampler(int active)
720{
721 mState.activeSampler = active;
722}
723
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000724GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000725{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000726 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000727}
728
729GLuint Context::getDrawFramebufferHandle() const
730{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000731 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000732}
733
734GLuint Context::getRenderbufferHandle() const
735{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000736 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000737}
738
739GLuint Context::getArrayBufferHandle() const
740{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000741 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000742}
743
daniel@transgaming.com83921382011-01-08 05:46:00 +0000744void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000745{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000746 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000747}
748
daniel@transgaming.com83921382011-01-08 05:46:00 +0000749const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000750{
751 return mState.vertexAttribute[attribNum];
752}
753
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000754void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000755 GLsizei stride, const void *pointer)
756{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000757 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000758 mState.vertexAttribute[attribNum].mSize = size;
759 mState.vertexAttribute[attribNum].mType = type;
760 mState.vertexAttribute[attribNum].mNormalized = normalized;
761 mState.vertexAttribute[attribNum].mStride = stride;
762 mState.vertexAttribute[attribNum].mPointer = pointer;
763}
764
765const void *Context::getVertexAttribPointer(unsigned int attribNum) const
766{
767 return mState.vertexAttribute[attribNum].mPointer;
768}
769
daniel@transgaming.com83921382011-01-08 05:46:00 +0000770const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000771{
772 return mState.vertexAttribute;
773}
774
775void Context::setPackAlignment(GLint alignment)
776{
777 mState.packAlignment = alignment;
778}
779
780GLint Context::getPackAlignment() const
781{
782 return mState.packAlignment;
783}
784
785void Context::setUnpackAlignment(GLint alignment)
786{
787 mState.unpackAlignment = alignment;
788}
789
790GLint Context::getUnpackAlignment() const
791{
792 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793}
794
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000795GLuint Context::createBuffer()
796{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000797 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798}
799
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000800GLuint Context::createProgram()
801{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000802 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803}
804
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000805GLuint Context::createShader(GLenum type)
806{
807 return mResourceManager->createShader(type);
808}
809
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810GLuint Context::createTexture()
811{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000812 return mResourceManager->createTexture();
813}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000815GLuint Context::createRenderbuffer()
816{
817 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818}
819
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000820// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821GLuint Context::createFramebuffer()
822{
benvanik@google.com1a233342011-04-28 19:44:39 +0000823 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000824
825 mFramebufferMap[handle] = NULL;
826
827 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828}
829
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000830GLuint Context::createFence()
831{
benvanik@google.com1a233342011-04-28 19:44:39 +0000832 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000833
834 mFenceMap[handle] = new Fence;
835
836 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000837}
838
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839void Context::deleteBuffer(GLuint buffer)
840{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000841 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842 {
843 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000845
846 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
849void Context::deleteShader(GLuint shader)
850{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000851 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852}
853
854void Context::deleteProgram(GLuint program)
855{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000856 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
859void Context::deleteTexture(GLuint texture)
860{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000861 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862 {
863 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000865
866 mResourceManager->deleteTexture(texture);
867}
868
869void Context::deleteRenderbuffer(GLuint renderbuffer)
870{
871 if (mResourceManager->getRenderbuffer(renderbuffer))
872 {
873 detachRenderbuffer(renderbuffer);
874 }
875
876 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877}
878
879void Context::deleteFramebuffer(GLuint framebuffer)
880{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000881 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
882
883 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884 {
885 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000886
benvanik@google.com1a233342011-04-28 19:44:39 +0000887 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000888 delete framebufferObject->second;
889 mFramebufferMap.erase(framebufferObject);
890 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000892
893void Context::deleteFence(GLuint fence)
894{
895 FenceMap::iterator fenceObject = mFenceMap.find(fence);
896
897 if (fenceObject != mFenceMap.end())
898 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000899 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000900 delete fenceObject->second;
901 mFenceMap.erase(fenceObject);
902 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000903}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000905Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907 return mResourceManager->getBuffer(handle);
908}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000910Shader *Context::getShader(GLuint handle)
911{
912 return mResourceManager->getShader(handle);
913}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000915Program *Context::getProgram(GLuint handle)
916{
917 return mResourceManager->getProgram(handle);
918}
919
920Texture *Context::getTexture(GLuint handle)
921{
922 return mResourceManager->getTexture(handle);
923}
924
925Renderbuffer *Context::getRenderbuffer(GLuint handle)
926{
927 return mResourceManager->getRenderbuffer(handle);
928}
929
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000930Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000931{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000932 return getFramebuffer(mState.readFramebuffer);
933}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000934
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000935Framebuffer *Context::getDrawFramebuffer()
936{
937 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938}
939
940void Context::bindArrayBuffer(unsigned int buffer)
941{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000942 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000944 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945}
946
947void Context::bindElementArrayBuffer(unsigned int buffer)
948{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000949 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000951 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
954void Context::bindTexture2D(GLuint texture)
955{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000956 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000958 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959}
960
961void Context::bindTextureCubeMap(GLuint texture)
962{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000963 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000965 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000968void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000970 if (!getFramebuffer(framebuffer))
971 {
972 mFramebufferMap[framebuffer] = new Framebuffer();
973 }
974
975 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000976}
977
978void Context::bindDrawFramebuffer(GLuint framebuffer)
979{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000980 if (!getFramebuffer(framebuffer))
981 {
982 mFramebufferMap[framebuffer] = new Framebuffer();
983 }
984
985 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
988void Context::bindRenderbuffer(GLuint renderbuffer)
989{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000990 mResourceManager->checkRenderbufferAllocation(renderbuffer);
991
992 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
995void Context::useProgram(GLuint program)
996{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000997 GLuint priorProgram = mState.currentProgram;
998 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000999
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001000 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001002 Program *newProgram = mResourceManager->getProgram(program);
1003 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1004
1005 if (newProgram)
1006 {
1007 newProgram->addRef();
1008 }
1009
1010 if (oldProgram)
1011 {
1012 oldProgram->release();
1013 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015}
1016
1017void Context::setFramebufferZero(Framebuffer *buffer)
1018{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001019 delete mFramebufferMap[0];
1020 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021}
1022
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001023void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001025 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1026 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027}
1028
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001029Framebuffer *Context::getFramebuffer(unsigned int handle)
1030{
1031 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1032
1033 if (framebuffer == mFramebufferMap.end())
1034 {
1035 return NULL;
1036 }
1037 else
1038 {
1039 return framebuffer->second;
1040 }
1041}
1042
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001043Fence *Context::getFence(unsigned int handle)
1044{
1045 FenceMap::iterator fence = mFenceMap.find(handle);
1046
1047 if (fence == mFenceMap.end())
1048 {
1049 return NULL;
1050 }
1051 else
1052 {
1053 return fence->second;
1054 }
1055}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001056
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057Buffer *Context::getArrayBuffer()
1058{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001059 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060}
1061
1062Buffer *Context::getElementArrayBuffer()
1063{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001064 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065}
1066
1067Program *Context::getCurrentProgram()
1068{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001069 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070}
1071
1072Texture2D *Context::getTexture2D()
1073{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001074 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, SAMPLER_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075}
1076
1077TextureCubeMap *Context::getTextureCubeMap()
1078{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001079 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, SAMPLER_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080}
1081
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001082Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001084 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001085
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001086 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001087 {
1088 switch (type)
1089 {
1090 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001091 case SAMPLER_2D: return mTexture2DZero.get();
1092 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001093 }
1094 }
1095
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001096 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097}
1098
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001099bool Context::getBooleanv(GLenum pname, GLboolean *params)
1100{
1101 switch (pname)
1102 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001103 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1104 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1105 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001106 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001107 params[0] = mState.colorMaskRed;
1108 params[1] = mState.colorMaskGreen;
1109 params[2] = mState.colorMaskBlue;
1110 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001111 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001112 case GL_CULL_FACE: *params = mState.cullFace; break;
1113 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1114 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1115 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1116 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1117 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1118 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1119 case GL_BLEND: *params = mState.blend; break;
1120 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001121 default:
1122 return false;
1123 }
1124
1125 return true;
1126}
1127
1128bool Context::getFloatv(GLenum pname, GLfloat *params)
1129{
1130 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1131 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1132 // GetIntegerv as its native query function. As it would require conversion in any
1133 // case, this should make no difference to the calling application.
1134 switch (pname)
1135 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001136 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1137 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1138 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1139 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1140 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001141 case GL_ALIASED_LINE_WIDTH_RANGE:
1142 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1143 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1144 break;
1145 case GL_ALIASED_POINT_SIZE_RANGE:
1146 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001147 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 +00001148 break;
1149 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001150 params[0] = mState.zNear;
1151 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001152 break;
1153 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001154 params[0] = mState.colorClearValue.red;
1155 params[1] = mState.colorClearValue.green;
1156 params[2] = mState.colorClearValue.blue;
1157 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001158 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001159 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001160 params[0] = mState.blendColor.red;
1161 params[1] = mState.blendColor.green;
1162 params[2] = mState.blendColor.blue;
1163 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001164 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001165 default:
1166 return false;
1167 }
1168
1169 return true;
1170}
1171
1172bool Context::getIntegerv(GLenum pname, GLint *params)
1173{
1174 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1175 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1176 // GetIntegerv as its native query function. As it would require conversion in any
1177 // case, this should make no difference to the calling application. You may find it in
1178 // Context::getFloatv.
1179 switch (pname)
1180 {
1181 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1182 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001183 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001184 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1185 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1186 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001187 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001188 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001189 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001190 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001191 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1192 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001193 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1194 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1195 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001196 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001197 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1198 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1199 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1200 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001201 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001202 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1203 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1204 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1205 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1206 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1207 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1208 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1209 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1210 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1211 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1212 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1213 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1214 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1215 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1216 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1217 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1218 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1219 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1220 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1221 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1222 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1223 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1224 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1225 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001226 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1227 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001228 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1229 {
1230 if (supportsCompressedTextures())
1231 {
1232 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1233 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1234 *params = 2;
1235 }
1236 else
1237 {
1238 *params = 0;
1239 }
1240 }
1241 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001242 case GL_MAX_SAMPLES_ANGLE:
1243 {
1244 GLsizei maxSamples = getMaxSupportedSamples();
1245 if (maxSamples != 0)
1246 {
1247 *params = maxSamples;
1248 }
1249 else
1250 {
1251 return false;
1252 }
1253
1254 break;
1255 }
1256 case GL_SAMPLE_BUFFERS:
1257 case GL_SAMPLES:
1258 {
1259 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1260 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1261 {
1262 switch (pname)
1263 {
1264 case GL_SAMPLE_BUFFERS:
1265 if (framebuffer->getSamples() != 0)
1266 {
1267 *params = 1;
1268 }
1269 else
1270 {
1271 *params = 0;
1272 }
1273 break;
1274 case GL_SAMPLES:
1275 *params = framebuffer->getSamples();
1276 break;
1277 }
1278 }
1279 else
1280 {
1281 *params = 0;
1282 }
1283 }
1284 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001285 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1286 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1287 case GL_MAX_VIEWPORT_DIMS:
1288 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001289 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001290 params[0] = maxDimension;
1291 params[1] = maxDimension;
1292 }
1293 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001294 case GL_COMPRESSED_TEXTURE_FORMATS:
1295 {
1296 if (supportsCompressedTextures())
1297 {
1298 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1299 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1300 }
1301 }
1302 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001303 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001304 params[0] = mState.viewportX;
1305 params[1] = mState.viewportY;
1306 params[2] = mState.viewportWidth;
1307 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001308 break;
1309 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001310 params[0] = mState.scissorX;
1311 params[1] = mState.scissorY;
1312 params[2] = mState.scissorWidth;
1313 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001314 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001315 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1316 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001317 case GL_RED_BITS:
1318 case GL_GREEN_BITS:
1319 case GL_BLUE_BITS:
1320 case GL_ALPHA_BITS:
1321 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001322 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001323 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1324
1325 if (colorbuffer)
1326 {
1327 switch (pname)
1328 {
1329 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1330 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1331 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1332 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1333 }
1334 }
1335 else
1336 {
1337 *params = 0;
1338 }
1339 }
1340 break;
1341 case GL_DEPTH_BITS:
1342 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001343 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001344 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001345
1346 if (depthbuffer)
1347 {
1348 *params = depthbuffer->getDepthSize();
1349 }
1350 else
1351 {
1352 *params = 0;
1353 }
1354 }
1355 break;
1356 case GL_STENCIL_BITS:
1357 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001358 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001359 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001360
1361 if (stencilbuffer)
1362 {
1363 *params = stencilbuffer->getStencilSize();
1364 }
1365 else
1366 {
1367 *params = 0;
1368 }
1369 }
1370 break;
1371 case GL_TEXTURE_BINDING_2D:
1372 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001373 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001374 {
1375 error(GL_INVALID_OPERATION);
1376 return false;
1377 }
1378
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001379 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001380 }
1381 break;
1382 case GL_TEXTURE_BINDING_CUBE_MAP:
1383 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001384 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001385 {
1386 error(GL_INVALID_OPERATION);
1387 return false;
1388 }
1389
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001390 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 }
1392 break;
1393 default:
1394 return false;
1395 }
1396
1397 return true;
1398}
1399
1400bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1401{
1402 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1403 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1404 // to the fact that it is stored internally as a float, and so would require conversion
1405 // if returned from Context::getIntegerv. Since this conversion is already implemented
1406 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1407 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1408 // application.
1409 switch (pname)
1410 {
1411 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1412 case GL_SHADER_BINARY_FORMATS:
1413 {
1414 *type = GL_INT;
1415 *numParams = 0;
1416 }
1417 break;
1418 case GL_MAX_VERTEX_ATTRIBS:
1419 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1420 case GL_MAX_VARYING_VECTORS:
1421 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1422 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1423 case GL_MAX_TEXTURE_IMAGE_UNITS:
1424 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1425 case GL_MAX_RENDERBUFFER_SIZE:
1426 case GL_NUM_SHADER_BINARY_FORMATS:
1427 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1428 case GL_ARRAY_BUFFER_BINDING:
1429 case GL_FRAMEBUFFER_BINDING:
1430 case GL_RENDERBUFFER_BINDING:
1431 case GL_CURRENT_PROGRAM:
1432 case GL_PACK_ALIGNMENT:
1433 case GL_UNPACK_ALIGNMENT:
1434 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001435 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001436 case GL_RED_BITS:
1437 case GL_GREEN_BITS:
1438 case GL_BLUE_BITS:
1439 case GL_ALPHA_BITS:
1440 case GL_DEPTH_BITS:
1441 case GL_STENCIL_BITS:
1442 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1443 case GL_CULL_FACE_MODE:
1444 case GL_FRONT_FACE:
1445 case GL_ACTIVE_TEXTURE:
1446 case GL_STENCIL_FUNC:
1447 case GL_STENCIL_VALUE_MASK:
1448 case GL_STENCIL_REF:
1449 case GL_STENCIL_FAIL:
1450 case GL_STENCIL_PASS_DEPTH_FAIL:
1451 case GL_STENCIL_PASS_DEPTH_PASS:
1452 case GL_STENCIL_BACK_FUNC:
1453 case GL_STENCIL_BACK_VALUE_MASK:
1454 case GL_STENCIL_BACK_REF:
1455 case GL_STENCIL_BACK_FAIL:
1456 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1457 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1458 case GL_DEPTH_FUNC:
1459 case GL_BLEND_SRC_RGB:
1460 case GL_BLEND_SRC_ALPHA:
1461 case GL_BLEND_DST_RGB:
1462 case GL_BLEND_DST_ALPHA:
1463 case GL_BLEND_EQUATION_RGB:
1464 case GL_BLEND_EQUATION_ALPHA:
1465 case GL_STENCIL_WRITEMASK:
1466 case GL_STENCIL_BACK_WRITEMASK:
1467 case GL_STENCIL_CLEAR_VALUE:
1468 case GL_SUBPIXEL_BITS:
1469 case GL_MAX_TEXTURE_SIZE:
1470 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1471 case GL_SAMPLE_BUFFERS:
1472 case GL_SAMPLES:
1473 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1474 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1475 case GL_TEXTURE_BINDING_2D:
1476 case GL_TEXTURE_BINDING_CUBE_MAP:
1477 {
1478 *type = GL_INT;
1479 *numParams = 1;
1480 }
1481 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001482 case GL_MAX_SAMPLES_ANGLE:
1483 {
1484 if (getMaxSupportedSamples() != 0)
1485 {
1486 *type = GL_INT;
1487 *numParams = 1;
1488 }
1489 else
1490 {
1491 return false;
1492 }
1493 }
1494 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001495 case GL_MAX_VIEWPORT_DIMS:
1496 {
1497 *type = GL_INT;
1498 *numParams = 2;
1499 }
1500 break;
1501 case GL_VIEWPORT:
1502 case GL_SCISSOR_BOX:
1503 {
1504 *type = GL_INT;
1505 *numParams = 4;
1506 }
1507 break;
1508 case GL_SHADER_COMPILER:
1509 case GL_SAMPLE_COVERAGE_INVERT:
1510 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001511 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1512 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1513 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1514 case GL_SAMPLE_COVERAGE:
1515 case GL_SCISSOR_TEST:
1516 case GL_STENCIL_TEST:
1517 case GL_DEPTH_TEST:
1518 case GL_BLEND:
1519 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001520 {
1521 *type = GL_BOOL;
1522 *numParams = 1;
1523 }
1524 break;
1525 case GL_COLOR_WRITEMASK:
1526 {
1527 *type = GL_BOOL;
1528 *numParams = 4;
1529 }
1530 break;
1531 case GL_POLYGON_OFFSET_FACTOR:
1532 case GL_POLYGON_OFFSET_UNITS:
1533 case GL_SAMPLE_COVERAGE_VALUE:
1534 case GL_DEPTH_CLEAR_VALUE:
1535 case GL_LINE_WIDTH:
1536 {
1537 *type = GL_FLOAT;
1538 *numParams = 1;
1539 }
1540 break;
1541 case GL_ALIASED_LINE_WIDTH_RANGE:
1542 case GL_ALIASED_POINT_SIZE_RANGE:
1543 case GL_DEPTH_RANGE:
1544 {
1545 *type = GL_FLOAT;
1546 *numParams = 2;
1547 }
1548 break;
1549 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001550 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001551 {
1552 *type = GL_FLOAT;
1553 *numParams = 4;
1554 }
1555 break;
1556 default:
1557 return false;
1558 }
1559
1560 return true;
1561}
1562
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001563// Applies the render target surface, depth stencil surface, viewport rectangle and
1564// scissor rectangle to the Direct3D 9 device
1565bool Context::applyRenderTarget(bool ignoreViewport)
1566{
1567 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001568
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001569 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001570
1571 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1572 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001573 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574 }
1575
1576 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001577
1578 if (!renderTarget)
1579 {
1580 return false; // Context must be lost
1581 }
1582
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001583 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001584
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001585 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1586 if (renderTargetSerial != mAppliedRenderTargetSerial)
1587 {
1588 device->SetRenderTarget(0, renderTarget);
1589 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001590 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001591 }
1592
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001593 unsigned int depthbufferSerial = 0;
1594 unsigned int stencilbufferSerial = 0;
1595 if (framebufferObject->getDepthbufferType() != GL_NONE)
1596 {
1597 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001598 if (!depthStencil)
1599 {
1600 ERR("Depth stencil pointer unexpectedly null.");
1601 return false;
1602 }
1603
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001604 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1605 }
1606 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1607 {
1608 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001609 if (!depthStencil)
1610 {
1611 ERR("Depth stencil pointer unexpectedly null.");
1612 return false;
1613 }
1614
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001615 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1616 }
1617
1618 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001619 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001620 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001621 {
1622 device->SetDepthStencilSurface(depthStencil);
1623 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001624 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001625 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001626 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001627
1628 D3DVIEWPORT9 viewport;
1629 D3DSURFACE_DESC desc;
1630 renderTarget->GetDesc(&desc);
1631
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001632 float zNear = clamp01(mState.zNear);
1633 float zFar = clamp01(mState.zFar);
1634
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001635 if (ignoreViewport)
1636 {
1637 viewport.X = 0;
1638 viewport.Y = 0;
1639 viewport.Width = desc.Width;
1640 viewport.Height = desc.Height;
1641 viewport.MinZ = 0.0f;
1642 viewport.MaxZ = 1.0f;
1643 }
1644 else
1645 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001646 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1647 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1648 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1649 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1650 viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(desc.Height) - static_cast<LONG>(viewport.Y));
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001651 viewport.MinZ = zNear;
1652 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653 }
1654
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001655 if (viewport.Width <= 0 || viewport.Height <= 0)
1656 {
1657 return false; // Nothing to render
1658 }
1659
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001660 device->SetViewport(&viewport);
1661
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001662 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001663 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001664 if (mState.scissorTest)
1665 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001666 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1667 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1668 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1669 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1670 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001671 device->SetScissorRect(&rect);
1672 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1673 }
1674 else
1675 {
1676 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1677 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001678
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001679 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001680 }
1681
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001682 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684 Program *programObject = getCurrentProgram();
1685
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001686 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001687 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001688 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001689
daniel@transgaming.com31754962010-11-28 02:02:52 +00001690 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001691 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1692 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1693 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001694 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001695
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001696 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001697 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001698 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001699
daniel@transgaming.com31754962010-11-28 02:02:52 +00001700 GLint depthRange = programObject->getDxDepthRangeLocation();
1701 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1702 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001703 }
1704
1705 return true;
1706}
1707
1708// 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 +00001709void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001710{
1711 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001712 Program *programObject = getCurrentProgram();
1713
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001714 Framebuffer *framebufferObject = getDrawFramebuffer();
1715
1716 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1717
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001718 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001719 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001720 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001722 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001723 GLint alwaysFront = !isTriangleMode(drawMode);
1724 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1725
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001726 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001728 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001730 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731 }
1732 else
1733 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001734 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 }
1736
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001737 mCullStateDirty = false;
1738 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001740 if (mDepthStateDirty)
1741 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001742 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001744 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1745 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746 }
1747 else
1748 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001749 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751
1752 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753 }
1754
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001755 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001758 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001759 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1760
1761 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1762 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1763 {
1764 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1765 }
1766 else
1767 {
1768 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1769 unorm<8>(mState.blendColor.alpha),
1770 unorm<8>(mState.blendColor.alpha),
1771 unorm<8>(mState.blendColor.alpha)));
1772 }
1773
1774 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1775 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1776 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1777
1778 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1779 mState.destBlendRGB != mState.destBlendAlpha ||
1780 mState.blendEquationRGB != mState.blendEquationAlpha)
1781 {
1782 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1783
1784 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1785 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1786 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001787 }
1788 else
1789 {
1790 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1791 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001792 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001793 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001794 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001795 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001796 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001797
1798 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001799 }
1800
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001801 if (mStencilStateDirty || mFrontFaceDirty)
1802 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001803 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001804 {
1805 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1806 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1807
1808 // FIXME: Unsupported by D3D9
1809 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1810 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1811 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1812 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1813 mState.stencilRef != mState.stencilBackRef ||
1814 mState.stencilMask != mState.stencilBackMask)
1815 {
1816 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1817 return error(GL_INVALID_OPERATION);
1818 }
1819
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001820 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001821 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001822 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1823
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001824 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1825 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826 es2dx::ConvertComparison(mState.stencilFunc));
1827
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001828 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1829 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001830
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001831 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001832 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001833 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001834 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001835 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001836 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1837
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001838 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1839 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001840 es2dx::ConvertComparison(mState.stencilBackFunc));
1841
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001842 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1843 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001844
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001845 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001846 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001847 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001849 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001850 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1851 }
1852 else
1853 {
1854 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1855 }
1856
1857 mStencilStateDirty = false;
1858 }
1859
1860 if (mMaskStateDirty)
1861 {
1862 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1863 mState.colorMaskBlue, mState.colorMaskAlpha));
1864 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1865
1866 mMaskStateDirty = false;
1867 }
1868
1869 if (mPolygonOffsetStateDirty)
1870 {
1871 if (mState.polygonOffsetFill)
1872 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001873 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001874 if (depthbuffer)
1875 {
1876 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1877 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1878 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1879 }
1880 }
1881 else
1882 {
1883 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1884 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1885 }
1886
1887 mPolygonOffsetStateDirty = false;
1888 }
1889
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001890 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001892 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001893 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001894 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001895 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001896 FIXME("Sample alpha to coverage is unimplemented.");
1897 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001898
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001899 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1900 if (mState.sampleCoverage)
1901 {
1902 unsigned int mask = 0;
1903 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001904 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001905 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001906
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001907 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001908 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001909 mask <<= 1;
1910
1911 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1912 {
1913 threshold += 1.0f;
1914 mask |= 1;
1915 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001916 }
1917 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001918
1919 if (mState.sampleCoverageInvert)
1920 {
1921 mask = ~mask;
1922 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001923
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001924 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1925 }
1926 else
1927 {
1928 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1929 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001930 }
1931 else
1932 {
1933 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001934 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001935
1936 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937 }
1938
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001939 if (mDitherStateDirty)
1940 {
1941 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1942
1943 mDitherStateDirty = false;
1944 }
1945
1946 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947}
1948
daniel@transgaming.com83921382011-01-08 05:46:00 +00001949GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001950{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001951 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001952
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001953 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001954 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001955 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001956 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001957 }
1958
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00001959 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001960}
1961
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001962// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001963GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001964{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001965 IDirect3DDevice9 *device = getDevice();
1966 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001967
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001968 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001969 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001970 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001971 }
1972
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001973 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974}
1975
1976// Applies the shaders and shader constants to the Direct3D 9 device
1977void Context::applyShaders()
1978{
1979 IDirect3DDevice9 *device = getDevice();
1980 Program *programObject = getCurrentProgram();
1981 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1982 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1983
1984 device->SetVertexShader(vertexShader);
1985 device->SetPixelShader(pixelShader);
1986
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001987 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001988 {
1989 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001990 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001991 }
1992
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 programObject->applyUniforms();
1994}
1995
1996// Applies the textures and sampler states to the Direct3D 9 device
1997void Context::applyTextures()
1998{
1999 IDirect3DDevice9 *device = getDevice();
2000 Program *programObject = getCurrentProgram();
2001
2002 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2003 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002004 int textureUnit = programObject->getSamplerMapping(sampler);
2005 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002007 SamplerType textureType = programObject->getSamplerType(sampler);
2008
2009 Texture *texture = getSamplerTexture(textureUnit, textureType);
2010
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002011 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002012 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002013 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2014
2015 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002016 {
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002017 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter())
2018 {
2019 GLenum wrapS = texture->getWrapS();
2020 GLenum wrapT = texture->getWrapT();
2021 GLenum minFilter = texture->getMinFilter();
2022 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002024 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2025 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002027 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2028 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2029 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2030 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2031 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
2032 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002034 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyImage())
2035 {
2036 device->SetTexture(sampler, d3dTexture);
2037 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002038 }
2039 else
2040 {
2041 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2042 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002043
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002044 mAppliedTextureSerial[sampler] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002045 texture->resetDirty();
2046 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002047 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002048 else
2049 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002050 if (mAppliedTextureSerial[sampler] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002051 {
2052 device->SetTexture(sampler, NULL);
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002053 mAppliedTextureSerial[sampler] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002054 }
2055 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002056 }
2057}
2058
2059void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2060{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002061 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002062
2063 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2064 {
2065 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2066 }
2067
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002068 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2069 {
2070 return error(GL_INVALID_OPERATION);
2071 }
2072
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002074
2075 if (!renderTarget)
2076 {
2077 return; // Context must be lost, return silently
2078 }
2079
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080 IDirect3DDevice9 *device = getDevice();
2081
2082 D3DSURFACE_DESC desc;
2083 renderTarget->GetDesc(&desc);
2084
2085 IDirect3DSurface9 *systemSurface;
2086 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2087
2088 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2089 {
2090 return error(GL_OUT_OF_MEMORY);
2091 }
2092
2093 ASSERT(SUCCEEDED(result));
2094
2095 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2096 {
2097 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2098 }
2099
2100 result = device->GetRenderTargetData(renderTarget, systemSurface);
2101
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 if (FAILED(result))
2103 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 systemSurface->Release();
2105
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002106 switch (result)
2107 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002108 case D3DERR_DRIVERINTERNALERROR:
2109 case D3DERR_DEVICELOST:
2110 return error(GL_OUT_OF_MEMORY);
2111 default:
2112 UNREACHABLE();
2113 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002114 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 }
2116
2117 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002118 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2119 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2120 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2121 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2122 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123
2124 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2125
2126 if (FAILED(result))
2127 {
2128 UNREACHABLE();
2129 systemSurface->Release();
2130
2131 return; // No sensible error to generate
2132 }
2133
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002134 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002136 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002137 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002138 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002139
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002140 for (int j = 0; j < rect.bottom - rect.top; j++)
2141 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002142 if (desc.Format == D3DFMT_A8R8G8B8 &&
2143 format == GL_BGRA_EXT &&
2144 type == GL_UNSIGNED_BYTE)
2145 {
2146 // Fast path for EXT_read_format_bgra, given
2147 // an RGBA source buffer. Note that buffers with no
2148 // alpha go through the slow path below.
2149 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002150 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002151 (rect.right - rect.left) * 4);
2152 continue;
2153 }
2154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 for (int i = 0; i < rect.right - rect.left; i++)
2156 {
2157 float r;
2158 float g;
2159 float b;
2160 float a;
2161
2162 switch (desc.Format)
2163 {
2164 case D3DFMT_R5G6B5:
2165 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002166 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167
2168 a = 1.0f;
2169 b = (rgb & 0x001F) * (1.0f / 0x001F);
2170 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2171 r = (rgb & 0xF800) * (1.0f / 0xF800);
2172 }
2173 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 case D3DFMT_A1R5G5B5:
2175 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002176 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177
2178 a = (argb & 0x8000) ? 1.0f : 0.0f;
2179 b = (argb & 0x001F) * (1.0f / 0x001F);
2180 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2181 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2182 }
2183 break;
2184 case D3DFMT_A8R8G8B8:
2185 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002186 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002187
2188 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2189 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2190 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2191 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2192 }
2193 break;
2194 case D3DFMT_X8R8G8B8:
2195 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002196 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197
2198 a = 1.0f;
2199 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2200 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2201 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2202 }
2203 break;
2204 case D3DFMT_A2R10G10B10:
2205 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002206 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002207
2208 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2209 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2210 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2211 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2212 }
2213 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002214 case D3DFMT_A32B32G32R32F:
2215 {
2216 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002217 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2218 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2219 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2220 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002221 }
2222 break;
2223 case D3DFMT_A16B16G16R16F:
2224 {
2225 // float formats in D3D are stored rgba, rather than the other way round
2226 float abgr[4];
2227
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002228 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002229
2230 a = abgr[3];
2231 b = abgr[2];
2232 g = abgr[1];
2233 r = abgr[0];
2234 }
2235 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002236 default:
2237 UNIMPLEMENTED(); // FIXME
2238 UNREACHABLE();
2239 }
2240
2241 switch (format)
2242 {
2243 case GL_RGBA:
2244 switch (type)
2245 {
2246 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002247 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2248 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2249 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2250 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251 break;
2252 default: UNREACHABLE();
2253 }
2254 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002255 case GL_BGRA_EXT:
2256 switch (type)
2257 {
2258 case GL_UNSIGNED_BYTE:
2259 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2260 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2261 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2262 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2263 break;
2264 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2265 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2266 // this type is packed as follows:
2267 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2268 // --------------------------------------------------------------------------------
2269 // | 4th | 3rd | 2nd | 1st component |
2270 // --------------------------------------------------------------------------------
2271 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2272 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2273 ((unsigned short)(15 * a + 0.5f) << 12)|
2274 ((unsigned short)(15 * r + 0.5f) << 8) |
2275 ((unsigned short)(15 * g + 0.5f) << 4) |
2276 ((unsigned short)(15 * b + 0.5f) << 0);
2277 break;
2278 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2279 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2280 // this type is packed as follows:
2281 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2282 // --------------------------------------------------------------------------------
2283 // | 4th | 3rd | 2nd | 1st component |
2284 // --------------------------------------------------------------------------------
2285 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2286 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2287 ((unsigned short)( a + 0.5f) << 15) |
2288 ((unsigned short)(31 * r + 0.5f) << 10) |
2289 ((unsigned short)(31 * g + 0.5f) << 5) |
2290 ((unsigned short)(31 * b + 0.5f) << 0);
2291 break;
2292 default: UNREACHABLE();
2293 }
2294 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002295 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 switch (type)
2297 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002298 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002299 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2300 ((unsigned short)(31 * b + 0.5f) << 0) |
2301 ((unsigned short)(63 * g + 0.5f) << 5) |
2302 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 break;
2304 default: UNREACHABLE();
2305 }
2306 break;
2307 default: UNREACHABLE();
2308 }
2309 }
2310 }
2311
2312 systemSurface->UnlockRect();
2313
2314 systemSurface->Release();
2315}
2316
2317void Context::clear(GLbitfield mask)
2318{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002319 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002320
2321 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2322 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002323 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002324 }
2325
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002326 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327 IDirect3DDevice9 *device = getDevice();
2328 DWORD flags = 0;
2329
2330 if (mask & GL_COLOR_BUFFER_BIT)
2331 {
2332 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002333
2334 if (framebufferObject->getColorbufferType() != GL_NONE)
2335 {
2336 flags |= D3DCLEAR_TARGET;
2337 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338 }
2339
2340 if (mask & GL_DEPTH_BUFFER_BIT)
2341 {
2342 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002343 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344 {
2345 flags |= D3DCLEAR_ZBUFFER;
2346 }
2347 }
2348
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349 GLuint stencilUnmasked = 0x0;
2350
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002351 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002354 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002356 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002357 if (!depthStencil)
2358 {
2359 ERR("Depth stencil pointer unexpectedly null.");
2360 return;
2361 }
2362
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002363 D3DSURFACE_DESC desc;
2364 depthStencil->GetDesc(&desc);
2365
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002366 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002367 stencilUnmasked = (0x1 << stencilSize) - 1;
2368
2369 if (stencilUnmasked != 0x0)
2370 {
2371 flags |= D3DCLEAR_STENCIL;
2372 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373 }
2374 }
2375
2376 if (mask != 0)
2377 {
2378 return error(GL_INVALID_VALUE);
2379 }
2380
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002381 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2382 {
2383 return;
2384 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002386 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002387 unorm<8>(mState.colorClearValue.red),
2388 unorm<8>(mState.colorClearValue.green),
2389 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002390 float depth = clamp01(mState.depthClearValue);
2391 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392
2393 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2394
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002395 if (!renderTarget)
2396 {
2397 return; // Context must be lost, return silently
2398 }
2399
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 D3DSURFACE_DESC desc;
2401 renderTarget->GetDesc(&desc);
2402
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002403 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404
2405 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002406 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002408 !(mState.colorMaskRed && mState.colorMaskGreen &&
2409 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410
2411 if (needMaskedColorClear || needMaskedStencilClear)
2412 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002413 // State which is altered in all paths from this point to the clear call is saved.
2414 // State which is altered in only some paths will be flagged dirty in the case that
2415 // that path is taken.
2416 HRESULT hr;
2417 if (mMaskedClearSavedState == NULL)
2418 {
2419 hr = device->BeginStateBlock();
2420 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2421
2422 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2423 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2424 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2425 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2426 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2427 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2428 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2429 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2430 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2431 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2432 device->SetPixelShader(NULL);
2433 device->SetVertexShader(NULL);
2434 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002435 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2436 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2437 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2438 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2439 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2440 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2441 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002442
2443 hr = device->EndStateBlock(&mMaskedClearSavedState);
2444 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2445 }
2446
2447 ASSERT(mMaskedClearSavedState != NULL);
2448
2449 if (mMaskedClearSavedState != NULL)
2450 {
2451 hr = mMaskedClearSavedState->Capture();
2452 ASSERT(SUCCEEDED(hr));
2453 }
2454
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002455 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2456 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2457 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2458 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2459 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2460 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2461 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2462 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2463
2464 if (flags & D3DCLEAR_TARGET)
2465 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002466 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 }
2468 else
2469 {
2470 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2471 }
2472
2473 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2474 {
2475 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2476 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2477 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2478 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002479 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002480 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2482 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002483 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484 }
2485 else
2486 {
2487 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2488 }
2489
2490 device->SetPixelShader(NULL);
2491 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002492 device->SetFVF(D3DFVF_XYZRHW);
2493 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2494 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2495 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2496 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2497 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2498 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2499 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002500
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002501 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2502 quad[0][0] = -0.5f;
2503 quad[0][1] = desc.Height - 0.5f;
2504 quad[0][2] = 0.0f;
2505 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002507 quad[1][0] = desc.Width - 0.5f;
2508 quad[1][1] = desc.Height - 0.5f;
2509 quad[1][2] = 0.0f;
2510 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002511
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002512 quad[2][0] = -0.5f;
2513 quad[2][1] = -0.5f;
2514 quad[2][2] = 0.0f;
2515 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002517 quad[3][0] = desc.Width - 0.5f;
2518 quad[3][1] = -0.5f;
2519 quad[3][2] = 0.0f;
2520 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002521
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002522 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002523 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524
2525 if (flags & D3DCLEAR_ZBUFFER)
2526 {
2527 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2528 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2529 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2530 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002531
2532 if (mMaskedClearSavedState != NULL)
2533 {
2534 mMaskedClearSavedState->Apply();
2535 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002536 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002537 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538 {
2539 device->Clear(0, NULL, flags, color, depth, stencil);
2540 }
2541}
2542
2543void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2544{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002545 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546 {
2547 return error(GL_INVALID_OPERATION);
2548 }
2549
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002550 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551 IDirect3DDevice9 *device = getDevice();
2552 D3DPRIMITIVETYPE primitiveType;
2553 int primitiveCount;
2554
2555 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2556 return error(GL_INVALID_ENUM);
2557
2558 if (primitiveCount <= 0)
2559 {
2560 return;
2561 }
2562
2563 if (!applyRenderTarget(false))
2564 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002565 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002566 }
2567
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002568 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002569
daniel@transgaming.com83921382011-01-08 05:46:00 +00002570 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002571 if (err != GL_NO_ERROR)
2572 {
2573 return error(err);
2574 }
2575
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002576 applyShaders();
2577 applyTextures();
2578
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002579 if (!getCurrentProgram()->validateSamplers())
2580 {
2581 return error(GL_INVALID_OPERATION);
2582 }
2583
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002584 if (!cullSkipsDraw(mode))
2585 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002586 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002587
2588 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002589
2590 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2591 {
2592 drawClosingLine(first, first + count - 1);
2593 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002594 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595}
2596
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002597void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002599 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600 {
2601 return error(GL_INVALID_OPERATION);
2602 }
2603
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002604 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002605 {
2606 return error(GL_INVALID_OPERATION);
2607 }
2608
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002609 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002610 IDirect3DDevice9 *device = getDevice();
2611 D3DPRIMITIVETYPE primitiveType;
2612 int primitiveCount;
2613
2614 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2615 return error(GL_INVALID_ENUM);
2616
2617 if (primitiveCount <= 0)
2618 {
2619 return;
2620 }
2621
2622 if (!applyRenderTarget(false))
2623 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002624 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002625 }
2626
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002627 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002628
2629 TranslatedIndexData indexInfo;
2630 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2631 if (err != GL_NO_ERROR)
2632 {
2633 return error(err);
2634 }
2635
daniel@transgaming.com83921382011-01-08 05:46:00 +00002636 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2637 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002638 if (err != GL_NO_ERROR)
2639 {
2640 return error(err);
2641 }
2642
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002643 applyShaders();
2644 applyTextures();
2645
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002646 if (!getCurrentProgram()->validateSamplers())
2647 {
2648 return error(GL_INVALID_OPERATION);
2649 }
2650
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002651 if (!cullSkipsDraw(mode))
2652 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002653 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002654
daniel@transgaming.com83921382011-01-08 05:46:00 +00002655 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002656
2657 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2658 {
2659 drawClosingLine(count, type, indices);
2660 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002661 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002662}
2663
2664void Context::finish()
2665{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002666 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002667 IDirect3DDevice9 *device = getDevice();
2668 IDirect3DQuery9 *occlusionQuery = NULL;
2669
2670 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2671
2672 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2673 {
2674 return error(GL_OUT_OF_MEMORY);
2675 }
2676
2677 ASSERT(SUCCEEDED(result));
2678
2679 if (occlusionQuery)
2680 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002681 IDirect3DStateBlock9 *savedState = NULL;
2682 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2683
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002684 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2685 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686
2687 // Render something outside the render target
2688 device->SetPixelShader(NULL);
2689 device->SetVertexShader(NULL);
2690 device->SetFVF(D3DFVF_XYZRHW);
2691 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002692 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002693 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002695 result = occlusionQuery->Issue(D3DISSUE_END);
2696 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697
2698 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2699 {
2700 // Keep polling, but allow other threads to do something useful first
2701 Sleep(0);
2702 }
2703
2704 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002705
2706 if (savedState)
2707 {
2708 savedState->Apply();
2709 savedState->Release();
2710 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711 }
2712}
2713
2714void Context::flush()
2715{
2716 IDirect3DDevice9 *device = getDevice();
2717 IDirect3DQuery9 *eventQuery = NULL;
2718
2719 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2720
2721 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2722 {
2723 return error(GL_OUT_OF_MEMORY);
2724 }
2725
2726 ASSERT(SUCCEEDED(result));
2727
2728 if (eventQuery)
2729 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002730 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2731 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002732
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002733 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002735
2736 if (result == D3DERR_DEVICELOST)
2737 {
2738 error(GL_OUT_OF_MEMORY);
2739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740 }
2741}
2742
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002743void Context::drawClosingLine(unsigned int first, unsigned int last)
2744{
2745 IDirect3DDevice9 *device = getDevice();
2746 IDirect3DIndexBuffer9 *indexBuffer = NULL;
2747 HRESULT result = D3DERR_INVALIDCALL;
2748
2749 if (supports32bitIndices())
2750 {
2751 result = device->CreateIndexBuffer(8, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &indexBuffer, 0);
2752
2753 if (SUCCEEDED(result))
2754 {
2755 unsigned int *data;
2756 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2757
2758 if (SUCCEEDED(result))
2759 {
2760 data[0] = last;
2761 data[1] = first;
2762 }
2763 }
2764 }
2765 else
2766 {
2767 result = device->CreateIndexBuffer(4, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexBuffer, 0);
2768
2769 if (SUCCEEDED(result))
2770 {
2771 unsigned short *data;
2772 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2773
2774 if (SUCCEEDED(result))
2775 {
2776 data[0] = last;
2777 data[1] = first;
2778 }
2779 }
2780 }
2781
2782 if (SUCCEEDED(result))
2783 {
2784 indexBuffer->Unlock();
2785 device->SetIndices(indexBuffer);
2786
2787 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, 0, 1);
2788
2789 indexBuffer->Release();
2790 }
2791 else
2792 {
2793 ERR("Could not create an index buffer for closing a line loop.");
2794 error(GL_OUT_OF_MEMORY);
2795 }
2796}
2797
2798void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2799{
2800 unsigned int first = 0;
2801 unsigned int last = 0;
2802
2803 if (mState.elementArrayBuffer.get())
2804 {
2805 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2806 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2807 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2808 }
2809
2810 switch (type)
2811 {
2812 case GL_UNSIGNED_BYTE:
2813 first = static_cast<const GLubyte*>(indices)[0];
2814 last = static_cast<const GLubyte*>(indices)[count - 1];
2815 break;
2816 case GL_UNSIGNED_SHORT:
2817 first = static_cast<const GLushort*>(indices)[0];
2818 last = static_cast<const GLushort*>(indices)[count - 1];
2819 break;
2820 case GL_UNSIGNED_INT:
2821 first = static_cast<const GLuint*>(indices)[0];
2822 last = static_cast<const GLuint*>(indices)[count - 1];
2823 break;
2824 default: UNREACHABLE();
2825 }
2826
2827 drawClosingLine(first, last);
2828}
2829
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002830void Context::recordInvalidEnum()
2831{
2832 mInvalidEnum = true;
2833}
2834
2835void Context::recordInvalidValue()
2836{
2837 mInvalidValue = true;
2838}
2839
2840void Context::recordInvalidOperation()
2841{
2842 mInvalidOperation = true;
2843}
2844
2845void Context::recordOutOfMemory()
2846{
2847 mOutOfMemory = true;
2848}
2849
2850void Context::recordInvalidFramebufferOperation()
2851{
2852 mInvalidFramebufferOperation = true;
2853}
2854
2855// Get one of the recorded errors and clear its flag, if any.
2856// [OpenGL ES 2.0.24] section 2.5 page 13.
2857GLenum Context::getError()
2858{
2859 if (mInvalidEnum)
2860 {
2861 mInvalidEnum = false;
2862
2863 return GL_INVALID_ENUM;
2864 }
2865
2866 if (mInvalidValue)
2867 {
2868 mInvalidValue = false;
2869
2870 return GL_INVALID_VALUE;
2871 }
2872
2873 if (mInvalidOperation)
2874 {
2875 mInvalidOperation = false;
2876
2877 return GL_INVALID_OPERATION;
2878 }
2879
2880 if (mOutOfMemory)
2881 {
2882 mOutOfMemory = false;
2883
2884 return GL_OUT_OF_MEMORY;
2885 }
2886
2887 if (mInvalidFramebufferOperation)
2888 {
2889 mInvalidFramebufferOperation = false;
2890
2891 return GL_INVALID_FRAMEBUFFER_OPERATION;
2892 }
2893
2894 return GL_NO_ERROR;
2895}
2896
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002897bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002898{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002899 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002900}
2901
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002902int Context::getMaximumVaryingVectors() const
2903{
2904 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2905}
2906
daniel@transgaming.com458da142010-11-28 02:03:02 +00002907int Context::getMaximumFragmentUniformVectors() const
2908{
2909 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2910}
2911
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002912int Context::getMaxSupportedSamples() const
2913{
2914 return mMaxSupportedSamples;
2915}
2916
2917int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2918{
2919 if (requested == 0)
2920 {
2921 return requested;
2922 }
2923
2924 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2925 if (itr == mMultiSampleSupport.end())
2926 {
2927 return -1;
2928 }
2929
2930 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2931 {
2932 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2933 {
2934 return i;
2935 }
2936 }
2937
2938 return -1;
2939}
2940
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002941bool Context::supportsEventQueries() const
2942{
2943 return mSupportsEventQueries;
2944}
2945
daniel@transgaming.com01868132010-08-24 19:21:17 +00002946bool Context::supportsCompressedTextures() const
2947{
2948 return mSupportsCompressedTextures;
2949}
2950
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002951bool Context::supportsFloatTextures() const
2952{
2953 return mSupportsFloatTextures;
2954}
2955
2956bool Context::supportsFloatLinearFilter() const
2957{
2958 return mSupportsFloatLinearFilter;
2959}
2960
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002961bool Context::supportsFloatRenderableTextures() const
2962{
2963 return mSupportsFloatRenderableTextures;
2964}
2965
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002966bool Context::supportsHalfFloatTextures() const
2967{
2968 return mSupportsHalfFloatTextures;
2969}
2970
2971bool Context::supportsHalfFloatLinearFilter() const
2972{
2973 return mSupportsHalfFloatLinearFilter;
2974}
2975
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002976bool Context::supportsHalfFloatRenderableTextures() const
2977{
2978 return mSupportsHalfFloatRenderableTextures;
2979}
2980
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00002981int Context::getMaximumRenderbufferDimension() const
2982{
2983 return mMaxRenderbufferDimension;
2984}
2985
2986int Context::getMaximumTextureDimension() const
2987{
2988 return mMaxTextureDimension;
2989}
2990
2991int Context::getMaximumCubeTextureDimension() const
2992{
2993 return mMaxCubeTextureDimension;
2994}
2995
2996int Context::getMaximumTextureLevel() const
2997{
2998 return mMaxTextureLevel;
2999}
3000
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003001bool Context::supportsLuminanceTextures() const
3002{
3003 return mSupportsLuminanceTextures;
3004}
3005
3006bool Context::supportsLuminanceAlphaTextures() const
3007{
3008 return mSupportsLuminanceAlphaTextures;
3009}
3010
daniel@transgaming.com83921382011-01-08 05:46:00 +00003011bool Context::supports32bitIndices() const
3012{
3013 return mSupports32bitIndices;
3014}
3015
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003016void Context::detachBuffer(GLuint buffer)
3017{
3018 // [OpenGL ES 2.0.24] section 2.9 page 22:
3019 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3020 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3021
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003022 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003023 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003024 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003025 }
3026
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003027 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003028 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003029 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003030 }
3031
3032 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3033 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003034 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003035 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003036 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003037 }
3038 }
3039}
3040
3041void Context::detachTexture(GLuint texture)
3042{
3043 // [OpenGL ES 2.0.24] section 3.8 page 84:
3044 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3045 // rebound to texture object zero
3046
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003047 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003048 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003049 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003050 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003051 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003052 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003053 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003054 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003055 }
3056 }
3057
3058 // [OpenGL ES 2.0.24] section 4.4 page 112:
3059 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3060 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3061 // image was attached in the currently bound framebuffer.
3062
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003063 Framebuffer *readFramebuffer = getReadFramebuffer();
3064 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003065
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003066 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003067 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003068 readFramebuffer->detachTexture(texture);
3069 }
3070
3071 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3072 {
3073 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003074 }
3075}
3076
3077void Context::detachFramebuffer(GLuint framebuffer)
3078{
3079 // [OpenGL ES 2.0.24] section 4.4 page 107:
3080 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3081 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3082
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003083 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003084 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003085 bindReadFramebuffer(0);
3086 }
3087
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003088 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003089 {
3090 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003091 }
3092}
3093
3094void Context::detachRenderbuffer(GLuint renderbuffer)
3095{
3096 // [OpenGL ES 2.0.24] section 4.4 page 109:
3097 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3098 // had been executed with the target RENDERBUFFER and name of zero.
3099
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003100 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003101 {
3102 bindRenderbuffer(0);
3103 }
3104
3105 // [OpenGL ES 2.0.24] section 4.4 page 111:
3106 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3107 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3108 // point to which this image was attached in the currently bound framebuffer.
3109
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003110 Framebuffer *readFramebuffer = getReadFramebuffer();
3111 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003112
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003113 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003114 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003115 readFramebuffer->detachRenderbuffer(renderbuffer);
3116 }
3117
3118 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3119 {
3120 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003121 }
3122}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003123
3124Texture *Context::getIncompleteTexture(SamplerType type)
3125{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003126 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003127
3128 if (t == NULL)
3129 {
3130 static const GLubyte color[] = { 0, 0, 0, 255 };
3131
3132 switch (type)
3133 {
3134 default:
3135 UNREACHABLE();
3136 // default falls through to SAMPLER_2D
3137
3138 case SAMPLER_2D:
3139 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003140 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003141 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003142 t = incomplete2d;
3143 }
3144 break;
3145
3146 case SAMPLER_CUBE:
3147 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003148 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003149
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003150 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3151 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3152 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3153 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3154 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3155 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003156
3157 t = incompleteCube;
3158 }
3159 break;
3160 }
3161
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003162 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003163 }
3164
3165 return t;
3166}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003167
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003168bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003169{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003170 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003171}
3172
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003173bool Context::isTriangleMode(GLenum drawMode)
3174{
3175 switch (drawMode)
3176 {
3177 case GL_TRIANGLES:
3178 case GL_TRIANGLE_FAN:
3179 case GL_TRIANGLE_STRIP:
3180 return true;
3181 case GL_POINTS:
3182 case GL_LINES:
3183 case GL_LINE_LOOP:
3184 case GL_LINE_STRIP:
3185 return false;
3186 default: UNREACHABLE();
3187 }
3188
3189 return false;
3190}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003191
3192void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3193{
3194 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3195
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003196 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3197 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3198 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3199 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003200
daniel@transgaming.com83921382011-01-08 05:46:00 +00003201 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003202}
3203
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003204void Context::initExtensionString()
3205{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003206 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003207 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3208 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003209 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003210 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003211 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003212
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003213 if (supportsEventQueries())
3214 {
3215 mExtensionString += "GL_NV_fence ";
3216 }
3217
daniel@transgaming.com01868132010-08-24 19:21:17 +00003218 if (supportsCompressedTextures())
3219 {
3220 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3221 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003222
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003223 if (supportsFloatTextures())
3224 {
3225 mExtensionString += "GL_OES_texture_float ";
3226 }
3227
3228 if (supportsHalfFloatTextures())
3229 {
3230 mExtensionString += "GL_OES_texture_half_float ";
3231 }
3232
3233 if (supportsFloatLinearFilter())
3234 {
3235 mExtensionString += "GL_OES_texture_float_linear ";
3236 }
3237
3238 if (supportsHalfFloatLinearFilter())
3239 {
3240 mExtensionString += "GL_OES_texture_half_float_linear ";
3241 }
3242
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003243 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003244 {
3245 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3246 }
3247
daniel@transgaming.com83921382011-01-08 05:46:00 +00003248 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003249 {
3250 mExtensionString += "GL_OES_element_index_uint ";
3251 }
3252
3253 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3254 if (end != std::string::npos)
3255 {
3256 mExtensionString.resize(end+1);
3257 }
3258}
3259
3260const char *Context::getExtensionString() const
3261{
3262 return mExtensionString.c_str();
3263}
3264
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003265void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3266 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3267 GLbitfield mask)
3268{
3269 IDirect3DDevice9 *device = getDevice();
3270
3271 Framebuffer *readFramebuffer = getReadFramebuffer();
3272 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3273
3274 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3275 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3276 {
3277 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3278 }
3279
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003280 if (drawFramebuffer->getSamples() != 0)
3281 {
3282 return error(GL_INVALID_OPERATION);
3283 }
3284
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003285 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3286 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3287 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3288 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3289
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003290 RECT sourceRect;
3291 RECT destRect;
3292
3293 if (srcX0 < srcX1)
3294 {
3295 sourceRect.left = srcX0;
3296 sourceRect.right = srcX1;
3297 destRect.left = dstX0;
3298 destRect.right = dstX1;
3299 }
3300 else
3301 {
3302 sourceRect.left = srcX1;
3303 destRect.left = dstX1;
3304 sourceRect.right = srcX0;
3305 destRect.right = dstX0;
3306 }
3307
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003308 if (srcY0 < srcY1)
3309 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003310 sourceRect.top = readBufferHeight - srcY1;
3311 destRect.top = drawBufferHeight - dstY1;
3312 sourceRect.bottom = readBufferHeight - srcY0;
3313 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003314 }
3315 else
3316 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003317 sourceRect.top = readBufferHeight - srcY0;
3318 destRect.top = drawBufferHeight - dstY0;
3319 sourceRect.bottom = readBufferHeight - srcY1;
3320 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003321 }
3322
3323 RECT sourceScissoredRect = sourceRect;
3324 RECT destScissoredRect = destRect;
3325
3326 if (mState.scissorTest)
3327 {
3328 // Only write to parts of the destination framebuffer which pass the scissor test
3329 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3330 // rect will be checked against scissorY, rather than the bottom.
3331 if (destRect.left < mState.scissorX)
3332 {
3333 int xDiff = mState.scissorX - destRect.left;
3334 destScissoredRect.left = mState.scissorX;
3335 sourceScissoredRect.left += xDiff;
3336 }
3337
3338 if (destRect.right > mState.scissorX + mState.scissorWidth)
3339 {
3340 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3341 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3342 sourceScissoredRect.right -= xDiff;
3343 }
3344
3345 if (destRect.top < mState.scissorY)
3346 {
3347 int yDiff = mState.scissorY - destRect.top;
3348 destScissoredRect.top = mState.scissorY;
3349 sourceScissoredRect.top += yDiff;
3350 }
3351
3352 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3353 {
3354 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3355 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3356 sourceScissoredRect.bottom -= yDiff;
3357 }
3358 }
3359
3360 bool blitRenderTarget = false;
3361 bool blitDepthStencil = false;
3362
3363 RECT sourceTrimmedRect = sourceScissoredRect;
3364 RECT destTrimmedRect = destScissoredRect;
3365
3366 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3367 // the actual draw and read surfaces.
3368 if (sourceTrimmedRect.left < 0)
3369 {
3370 int xDiff = 0 - sourceTrimmedRect.left;
3371 sourceTrimmedRect.left = 0;
3372 destTrimmedRect.left += xDiff;
3373 }
3374
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003375 if (sourceTrimmedRect.right > readBufferWidth)
3376 {
3377 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3378 sourceTrimmedRect.right = readBufferWidth;
3379 destTrimmedRect.right -= xDiff;
3380 }
3381
3382 if (sourceTrimmedRect.top < 0)
3383 {
3384 int yDiff = 0 - sourceTrimmedRect.top;
3385 sourceTrimmedRect.top = 0;
3386 destTrimmedRect.top += yDiff;
3387 }
3388
3389 if (sourceTrimmedRect.bottom > readBufferHeight)
3390 {
3391 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3392 sourceTrimmedRect.bottom = readBufferHeight;
3393 destTrimmedRect.bottom -= yDiff;
3394 }
3395
3396 if (destTrimmedRect.left < 0)
3397 {
3398 int xDiff = 0 - destTrimmedRect.left;
3399 destTrimmedRect.left = 0;
3400 sourceTrimmedRect.left += xDiff;
3401 }
3402
3403 if (destTrimmedRect.right > drawBufferWidth)
3404 {
3405 int xDiff = destTrimmedRect.right - drawBufferWidth;
3406 destTrimmedRect.right = drawBufferWidth;
3407 sourceTrimmedRect.right -= xDiff;
3408 }
3409
3410 if (destTrimmedRect.top < 0)
3411 {
3412 int yDiff = 0 - destTrimmedRect.top;
3413 destTrimmedRect.top = 0;
3414 sourceTrimmedRect.top += yDiff;
3415 }
3416
3417 if (destTrimmedRect.bottom > drawBufferHeight)
3418 {
3419 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3420 destTrimmedRect.bottom = drawBufferHeight;
3421 sourceTrimmedRect.bottom -= yDiff;
3422 }
3423
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003424 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003425 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3426 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3427 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3428 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003429 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3430 {
3431 partialBufferCopy = true;
3432 }
3433
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003434 if (mask & GL_COLOR_BUFFER_BIT)
3435 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003436 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3437 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3438 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3439 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3440 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003441 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3442 {
3443 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3444 return error(GL_INVALID_OPERATION);
3445 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003446
3447 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3448 {
3449 return error(GL_INVALID_OPERATION);
3450 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003451
3452 blitRenderTarget = true;
3453
3454 }
3455
3456 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3457 {
3458 DepthStencilbuffer *readDSBuffer = NULL;
3459 DepthStencilbuffer *drawDSBuffer = NULL;
3460
3461 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3462 // both a depth and stencil buffer, it will be the same buffer.
3463
3464 if (mask & GL_DEPTH_BUFFER_BIT)
3465 {
3466 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3467 {
3468 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3469 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3470 {
3471 return error(GL_INVALID_OPERATION);
3472 }
3473
3474 blitDepthStencil = true;
3475 readDSBuffer = readFramebuffer->getDepthbuffer();
3476 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3477 }
3478 }
3479
3480 if (mask & GL_STENCIL_BUFFER_BIT)
3481 {
3482 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3483 {
3484 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3485 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3486 {
3487 return error(GL_INVALID_OPERATION);
3488 }
3489
3490 blitDepthStencil = true;
3491 readDSBuffer = readFramebuffer->getStencilbuffer();
3492 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3493 }
3494 }
3495
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003496 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003497 {
3498 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3499 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3500 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003501
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003502 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3503 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003504 {
3505 return error(GL_INVALID_OPERATION);
3506 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003507 }
3508
3509 if (blitRenderTarget || blitDepthStencil)
3510 {
3511 egl::Display *display = getDisplay();
3512 display->endScene();
3513
3514 if (blitRenderTarget)
3515 {
3516 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3517 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3518
3519 if (FAILED(result))
3520 {
3521 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3522 return;
3523 }
3524 }
3525
3526 if (blitDepthStencil)
3527 {
3528 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3529
3530 if (FAILED(result))
3531 {
3532 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3533 return;
3534 }
3535 }
3536 }
3537}
3538
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003539VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3540{
3541 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3542 {
3543 mVertexDeclCache[i].vertexDeclaration = NULL;
3544 mVertexDeclCache[i].lruCount = 0;
3545 }
3546}
3547
3548VertexDeclarationCache::~VertexDeclarationCache()
3549{
3550 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3551 {
3552 if (mVertexDeclCache[i].vertexDeclaration)
3553 {
3554 mVertexDeclCache[i].vertexDeclaration->Release();
3555 }
3556 }
3557}
3558
3559GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3560{
3561 IDirect3DDevice9 *device = getDevice();
3562
3563 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3564 D3DVERTEXELEMENT9 *element = &elements[0];
3565
3566 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3567 {
3568 if (attributes[i].active)
3569 {
3570 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3571
3572 element->Stream = i;
3573 element->Offset = 0;
3574 element->Type = attributes[i].type;
3575 element->Method = D3DDECLMETHOD_DEFAULT;
3576 element->Usage = D3DDECLUSAGE_TEXCOORD;
3577 element->UsageIndex = program->getSemanticIndex(i);
3578 element++;
3579 }
3580 }
3581
3582 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3583 *(element++) = end;
3584
3585 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3586 {
3587 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3588 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3589 {
3590 entry->lruCount = ++mMaxLru;
3591 device->SetVertexDeclaration(entry->vertexDeclaration);
3592
3593 return GL_NO_ERROR;
3594 }
3595 }
3596
3597 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3598
3599 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3600 {
3601 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3602 {
3603 lastCache = &mVertexDeclCache[i];
3604 }
3605 }
3606
3607 if (lastCache->vertexDeclaration != NULL)
3608 {
3609 lastCache->vertexDeclaration->Release();
3610 lastCache->vertexDeclaration = NULL;
3611 }
3612
3613 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3614 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3615 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3616 lastCache->lruCount = ++mMaxLru;
3617
3618 return GL_NO_ERROR;
3619}
3620
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003621}
3622
3623extern "C"
3624{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003625gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003626{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003627 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003628}
3629
3630void glDestroyContext(gl::Context *context)
3631{
3632 delete context;
3633
3634 if (context == gl::getContext())
3635 {
3636 gl::makeCurrent(NULL, NULL, NULL);
3637 }
3638}
3639
3640void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3641{
3642 gl::makeCurrent(context, display, surface);
3643}
3644
3645gl::Context *glGetCurrentContext()
3646{
3647 return gl::getContext();
3648}
3649}