blob: 86b4ad84fc0b143bad9f108c02e4857bce01c4a2 [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
jbauman@chromium.org399c35f2011-04-28 23:19:51 +000034namespace
35{
36 enum { CLOSING_INDEX_BUFFER_SIZE = 4096 };
37}
38
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039namespace gl
40{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +000041Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042{
benvanik@google.com1a233342011-04-28 19:44:39 +000043 mFenceHandleAllocator.setBaseHandle(0);
44
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000046
daniel@transgaming.com428d1582010-05-04 03:35:25 +000047 mState.depthClearValue = 1.0f;
48 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049
daniel@transgaming.com428d1582010-05-04 03:35:25 +000050 mState.cullFace = false;
51 mState.cullMode = GL_BACK;
52 mState.frontFace = GL_CCW;
53 mState.depthTest = false;
54 mState.depthFunc = GL_LESS;
55 mState.blend = false;
56 mState.sourceBlendRGB = GL_ONE;
57 mState.sourceBlendAlpha = GL_ONE;
58 mState.destBlendRGB = GL_ZERO;
59 mState.destBlendAlpha = GL_ZERO;
60 mState.blendEquationRGB = GL_FUNC_ADD;
61 mState.blendEquationAlpha = GL_FUNC_ADD;
62 mState.blendColor.red = 0;
63 mState.blendColor.green = 0;
64 mState.blendColor.blue = 0;
65 mState.blendColor.alpha = 0;
66 mState.stencilTest = false;
67 mState.stencilFunc = GL_ALWAYS;
68 mState.stencilRef = 0;
69 mState.stencilMask = -1;
70 mState.stencilWritemask = -1;
71 mState.stencilBackFunc = GL_ALWAYS;
72 mState.stencilBackRef = 0;
73 mState.stencilBackMask = - 1;
74 mState.stencilBackWritemask = -1;
75 mState.stencilFail = GL_KEEP;
76 mState.stencilPassDepthFail = GL_KEEP;
77 mState.stencilPassDepthPass = GL_KEEP;
78 mState.stencilBackFail = GL_KEEP;
79 mState.stencilBackPassDepthFail = GL_KEEP;
80 mState.stencilBackPassDepthPass = GL_KEEP;
81 mState.polygonOffsetFill = false;
82 mState.polygonOffsetFactor = 0.0f;
83 mState.polygonOffsetUnits = 0.0f;
84 mState.sampleAlphaToCoverage = false;
85 mState.sampleCoverage = false;
86 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000087 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.scissorTest = false;
89 mState.dither = true;
90 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000091 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092
daniel@transgaming.com428d1582010-05-04 03:35:25 +000093 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000094
daniel@transgaming.com428d1582010-05-04 03:35:25 +000095 mState.viewportX = 0;
96 mState.viewportY = 0;
97 mState.viewportWidth = config->mDisplayMode.Width;
98 mState.viewportHeight = config->mDisplayMode.Height;
99 mState.zNear = 0.0f;
100 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000102 mState.scissorX = 0;
103 mState.scissorY = 0;
104 mState.scissorWidth = config->mDisplayMode.Width;
105 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000107 mState.colorMaskRed = true;
108 mState.colorMaskGreen = true;
109 mState.colorMaskBlue = true;
110 mState.colorMaskAlpha = true;
111 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000113 if (shareContext != NULL)
114 {
115 mResourceManager = shareContext->mResourceManager;
116 mResourceManager->addRef();
117 }
118 else
119 {
120 mResourceManager = new ResourceManager();
121 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 // [OpenGL ES 2.0.24] section 3.7 page 83:
124 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
125 // and cube map texture state vectors respectively associated with them.
126 // In order that access to these initial textures not be lost, they are treated as texture
127 // objects all of whose names are 0.
128
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000129 mTexture2DZero.set(new Texture2D(0));
130 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000132 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000133 bindArrayBuffer(0);
134 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindTextureCubeMap(0);
136 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000137 bindReadFramebuffer(0);
138 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 bindRenderbuffer(0);
140
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000141 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.packAlignment = 4;
144 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000145
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000147 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000148 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000149 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000150
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 mInvalidEnum = false;
152 mInvalidValue = false;
153 mInvalidOperation = false;
154 mOutOfMemory = false;
155 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000156
157 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000158
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000159 mSupportsCompressedTextures = false;
160 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000161 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000162 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000163 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
166Context::~Context()
167{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000168 if (mState.currentProgram != 0)
169 {
170 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
171 if (programObject)
172 {
173 programObject->release();
174 }
175 mState.currentProgram = 0;
176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000178 while (!mFramebufferMap.empty())
179 {
180 deleteFramebuffer(mFramebufferMap.begin()->first);
181 }
182
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000183 while (!mFenceMap.empty())
184 {
185 deleteFence(mFenceMap.begin()->first);
186 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000187
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000188 while (!mMultiSampleSupport.empty())
189 {
190 delete [] mMultiSampleSupport.begin()->second;
191 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
192 }
193
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000194 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
195 {
196 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
197 {
198 mState.samplerTexture[type][sampler].set(NULL);
199 }
200 }
201
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000202 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
203 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000204 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000205 }
206
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000207 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
208 {
209 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
210 }
211
212 mState.arrayBuffer.set(NULL);
213 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000214 mState.renderbuffer.set(NULL);
215
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000216 mTexture2DZero.set(NULL);
217 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000219 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000220 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000221 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000222 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000223
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000224 if (mMaskedClearSavedState)
225 {
226 mMaskedClearSavedState->Release();
227 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000228
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000229 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230}
231
232void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
233{
234 IDirect3DDevice9 *device = display->getDevice();
235
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000236 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000237 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000238 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000239
daniel@transgaming.com83921382011-01-08 05:46:00 +0000240 mVertexDataManager = new VertexDataManager(this, device);
241 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000242 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000243
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000244 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
245
246 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
247 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
248 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
249 mMaxRenderbufferDimension = mMaxTextureDimension;
250 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
251 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
252 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
253
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000254 const D3DFORMAT renderBufferFormats[] =
255 {
256 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000257 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000258 D3DFMT_R5G6B5,
259 D3DFMT_D24S8
260 };
261
262 int max = 0;
263 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
264 {
265 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
266 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
267 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
268
269 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
270 {
271 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
272 {
273 max = j;
274 }
275 }
276 }
277
278 mMaxSupportedSamples = max;
279
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000280 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000281 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000282 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
283 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000284 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
285 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000286
daniel@transgaming.com83921382011-01-08 05:46:00 +0000287 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
288
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000289 initExtensionString();
290
291 mState.viewportX = 0;
292 mState.viewportY = 0;
293 mState.viewportWidth = surface->getWidth();
294 mState.viewportHeight = surface->getHeight();
295
296 mState.scissorX = 0;
297 mState.scissorY = 0;
298 mState.scissorWidth = surface->getWidth();
299 mState.scissorHeight = surface->getHeight();
300
301 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000302 }
303
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
305 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000306 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000309 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000310 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
312 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000314 if (defaultRenderTarget)
315 {
316 defaultRenderTarget->Release();
317 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000319 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000321 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000323
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000324 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325}
326
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000327// 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 +0000328void Context::markAllStateDirty()
329{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000330 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
331 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000332 mAppliedTextureSerial[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000333 }
334
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000335 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000336 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000337 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000338 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000339 mDepthStencilInitialized = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000340
341 mClearStateDirty = true;
342 mCullStateDirty = true;
343 mDepthStateDirty = true;
344 mMaskStateDirty = true;
345 mBlendStateDirty = true;
346 mStencilStateDirty = true;
347 mPolygonOffsetStateDirty = true;
348 mScissorStateDirty = true;
349 mSampleStateDirty = true;
350 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000351 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000352}
353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354void Context::setClearColor(float red, float green, float blue, float alpha)
355{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000356 mState.colorClearValue.red = red;
357 mState.colorClearValue.green = green;
358 mState.colorClearValue.blue = blue;
359 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
362void Context::setClearDepth(float depth)
363{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000364 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365}
366
367void Context::setClearStencil(int stencil)
368{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000369 mState.stencilClearValue = stencil;
370}
371
372void Context::setCullFace(bool enabled)
373{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000374 if (mState.cullFace != enabled)
375 {
376 mState.cullFace = enabled;
377 mCullStateDirty = true;
378 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000379}
380
381bool Context::isCullFaceEnabled() const
382{
383 return mState.cullFace;
384}
385
386void Context::setCullMode(GLenum mode)
387{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000388 if (mState.cullMode != mode)
389 {
390 mState.cullMode = mode;
391 mCullStateDirty = true;
392 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000393}
394
395void Context::setFrontFace(GLenum front)
396{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000397 if (mState.frontFace != front)
398 {
399 mState.frontFace = front;
400 mFrontFaceDirty = true;
401 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000402}
403
404void Context::setDepthTest(bool enabled)
405{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000406 if (mState.depthTest != enabled)
407 {
408 mState.depthTest = enabled;
409 mDepthStateDirty = true;
410 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000411}
412
413bool Context::isDepthTestEnabled() const
414{
415 return mState.depthTest;
416}
417
418void Context::setDepthFunc(GLenum depthFunc)
419{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000420 if (mState.depthFunc != depthFunc)
421 {
422 mState.depthFunc = depthFunc;
423 mDepthStateDirty = true;
424 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000425}
426
427void Context::setDepthRange(float zNear, float zFar)
428{
429 mState.zNear = zNear;
430 mState.zFar = zFar;
431}
432
433void Context::setBlend(bool enabled)
434{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000435 if (mState.blend != enabled)
436 {
437 mState.blend = enabled;
438 mBlendStateDirty = true;
439 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000440}
441
442bool Context::isBlendEnabled() const
443{
444 return mState.blend;
445}
446
447void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
448{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000449 if (mState.sourceBlendRGB != sourceRGB ||
450 mState.sourceBlendAlpha != sourceAlpha ||
451 mState.destBlendRGB != destRGB ||
452 mState.destBlendAlpha != destAlpha)
453 {
454 mState.sourceBlendRGB = sourceRGB;
455 mState.destBlendRGB = destRGB;
456 mState.sourceBlendAlpha = sourceAlpha;
457 mState.destBlendAlpha = destAlpha;
458 mBlendStateDirty = true;
459 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000460}
461
462void Context::setBlendColor(float red, float green, float blue, float alpha)
463{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000464 if (mState.blendColor.red != red ||
465 mState.blendColor.green != green ||
466 mState.blendColor.blue != blue ||
467 mState.blendColor.alpha != alpha)
468 {
469 mState.blendColor.red = red;
470 mState.blendColor.green = green;
471 mState.blendColor.blue = blue;
472 mState.blendColor.alpha = alpha;
473 mBlendStateDirty = true;
474 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000475}
476
477void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
478{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000479 if (mState.blendEquationRGB != rgbEquation ||
480 mState.blendEquationAlpha != alphaEquation)
481 {
482 mState.blendEquationRGB = rgbEquation;
483 mState.blendEquationAlpha = alphaEquation;
484 mBlendStateDirty = true;
485 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000486}
487
488void Context::setStencilTest(bool enabled)
489{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000490 if (mState.stencilTest != enabled)
491 {
492 mState.stencilTest = enabled;
493 mStencilStateDirty = true;
494 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000495}
496
497bool Context::isStencilTestEnabled() const
498{
499 return mState.stencilTest;
500}
501
502void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
503{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000504 if (mState.stencilFunc != stencilFunc ||
505 mState.stencilRef != stencilRef ||
506 mState.stencilMask != stencilMask)
507 {
508 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000509 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 mState.stencilMask = stencilMask;
511 mStencilStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
516{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 if (mState.stencilBackFunc != stencilBackFunc ||
518 mState.stencilBackRef != stencilBackRef ||
519 mState.stencilBackMask != stencilBackMask)
520 {
521 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000522 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000523 mState.stencilBackMask = stencilBackMask;
524 mStencilStateDirty = true;
525 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000526}
527
528void Context::setStencilWritemask(GLuint stencilWritemask)
529{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000530 if (mState.stencilWritemask != stencilWritemask)
531 {
532 mState.stencilWritemask = stencilWritemask;
533 mStencilStateDirty = true;
534 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000535}
536
537void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
538{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000539 if (mState.stencilBackWritemask != stencilBackWritemask)
540 {
541 mState.stencilBackWritemask = stencilBackWritemask;
542 mStencilStateDirty = true;
543 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000544}
545
546void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
547{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000548 if (mState.stencilFail != stencilFail ||
549 mState.stencilPassDepthFail != stencilPassDepthFail ||
550 mState.stencilPassDepthPass != stencilPassDepthPass)
551 {
552 mState.stencilFail = stencilFail;
553 mState.stencilPassDepthFail = stencilPassDepthFail;
554 mState.stencilPassDepthPass = stencilPassDepthPass;
555 mStencilStateDirty = true;
556 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000557}
558
559void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
560{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000561 if (mState.stencilBackFail != stencilBackFail ||
562 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
563 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
564 {
565 mState.stencilBackFail = stencilBackFail;
566 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
567 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
568 mStencilStateDirty = true;
569 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000570}
571
572void Context::setPolygonOffsetFill(bool enabled)
573{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000574 if (mState.polygonOffsetFill != enabled)
575 {
576 mState.polygonOffsetFill = enabled;
577 mPolygonOffsetStateDirty = true;
578 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000579}
580
581bool Context::isPolygonOffsetFillEnabled() const
582{
583 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000584
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000585}
586
587void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
588{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000589 if (mState.polygonOffsetFactor != factor ||
590 mState.polygonOffsetUnits != units)
591 {
592 mState.polygonOffsetFactor = factor;
593 mState.polygonOffsetUnits = units;
594 mPolygonOffsetStateDirty = true;
595 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000596}
597
598void Context::setSampleAlphaToCoverage(bool enabled)
599{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000600 if (mState.sampleAlphaToCoverage != enabled)
601 {
602 mState.sampleAlphaToCoverage = enabled;
603 mSampleStateDirty = true;
604 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000605}
606
607bool Context::isSampleAlphaToCoverageEnabled() const
608{
609 return mState.sampleAlphaToCoverage;
610}
611
612void Context::setSampleCoverage(bool enabled)
613{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000614 if (mState.sampleCoverage != enabled)
615 {
616 mState.sampleCoverage = enabled;
617 mSampleStateDirty = true;
618 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000619}
620
621bool Context::isSampleCoverageEnabled() const
622{
623 return mState.sampleCoverage;
624}
625
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000626void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000627{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000628 if (mState.sampleCoverageValue != value ||
629 mState.sampleCoverageInvert != invert)
630 {
631 mState.sampleCoverageValue = value;
632 mState.sampleCoverageInvert = invert;
633 mSampleStateDirty = true;
634 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000635}
636
637void Context::setScissorTest(bool enabled)
638{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000639 if (mState.scissorTest != enabled)
640 {
641 mState.scissorTest = enabled;
642 mScissorStateDirty = true;
643 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000644}
645
646bool Context::isScissorTestEnabled() const
647{
648 return mState.scissorTest;
649}
650
651void Context::setDither(bool enabled)
652{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000653 if (mState.dither != enabled)
654 {
655 mState.dither = enabled;
656 mDitherStateDirty = true;
657 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000658}
659
660bool Context::isDitherEnabled() const
661{
662 return mState.dither;
663}
664
665void Context::setLineWidth(GLfloat width)
666{
667 mState.lineWidth = width;
668}
669
670void Context::setGenerateMipmapHint(GLenum hint)
671{
672 mState.generateMipmapHint = hint;
673}
674
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000675void Context::setFragmentShaderDerivativeHint(GLenum hint)
676{
677 mState.fragmentShaderDerivativeHint = hint;
678 // TODO: Propagate the hint to shader translator so we can write
679 // ddx, ddx_coarse, or ddx_fine depending on the hint.
680 // Ignore for now. It is valid for implementations to ignore hint.
681}
682
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000683void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
684{
685 mState.viewportX = x;
686 mState.viewportY = y;
687 mState.viewportWidth = width;
688 mState.viewportHeight = height;
689}
690
691void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
692{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000693 if (mState.scissorX != x || mState.scissorY != y ||
694 mState.scissorWidth != width || mState.scissorHeight != height)
695 {
696 mState.scissorX = x;
697 mState.scissorY = y;
698 mState.scissorWidth = width;
699 mState.scissorHeight = height;
700 mScissorStateDirty = true;
701 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000702}
703
704void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
705{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000706 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
707 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
708 {
709 mState.colorMaskRed = red;
710 mState.colorMaskGreen = green;
711 mState.colorMaskBlue = blue;
712 mState.colorMaskAlpha = alpha;
713 mMaskStateDirty = true;
714 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000715}
716
717void Context::setDepthMask(bool mask)
718{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000719 if (mState.depthMask != mask)
720 {
721 mState.depthMask = mask;
722 mMaskStateDirty = true;
723 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000724}
725
726void Context::setActiveSampler(int active)
727{
728 mState.activeSampler = active;
729}
730
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000731GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000732{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000733 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000734}
735
736GLuint Context::getDrawFramebufferHandle() const
737{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000738 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739}
740
741GLuint Context::getRenderbufferHandle() const
742{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000743 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000744}
745
746GLuint Context::getArrayBufferHandle() const
747{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000748 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749}
750
daniel@transgaming.com83921382011-01-08 05:46:00 +0000751void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000752{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000753 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000754}
755
daniel@transgaming.com83921382011-01-08 05:46:00 +0000756const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000757{
758 return mState.vertexAttribute[attribNum];
759}
760
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000761void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000762 GLsizei stride, const void *pointer)
763{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000764 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000765 mState.vertexAttribute[attribNum].mSize = size;
766 mState.vertexAttribute[attribNum].mType = type;
767 mState.vertexAttribute[attribNum].mNormalized = normalized;
768 mState.vertexAttribute[attribNum].mStride = stride;
769 mState.vertexAttribute[attribNum].mPointer = pointer;
770}
771
772const void *Context::getVertexAttribPointer(unsigned int attribNum) const
773{
774 return mState.vertexAttribute[attribNum].mPointer;
775}
776
daniel@transgaming.com83921382011-01-08 05:46:00 +0000777const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000778{
779 return mState.vertexAttribute;
780}
781
782void Context::setPackAlignment(GLint alignment)
783{
784 mState.packAlignment = alignment;
785}
786
787GLint Context::getPackAlignment() const
788{
789 return mState.packAlignment;
790}
791
792void Context::setUnpackAlignment(GLint alignment)
793{
794 mState.unpackAlignment = alignment;
795}
796
797GLint Context::getUnpackAlignment() const
798{
799 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000800}
801
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802GLuint Context::createBuffer()
803{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000804 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805}
806
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807GLuint Context::createProgram()
808{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000809 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810}
811
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000812GLuint Context::createShader(GLenum type)
813{
814 return mResourceManager->createShader(type);
815}
816
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817GLuint Context::createTexture()
818{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000819 return mResourceManager->createTexture();
820}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000822GLuint Context::createRenderbuffer()
823{
824 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825}
826
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000827// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828GLuint Context::createFramebuffer()
829{
benvanik@google.com1a233342011-04-28 19:44:39 +0000830 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000831
832 mFramebufferMap[handle] = NULL;
833
834 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835}
836
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000837GLuint Context::createFence()
838{
benvanik@google.com1a233342011-04-28 19:44:39 +0000839 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000840
841 mFenceMap[handle] = new Fence;
842
843 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000844}
845
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846void Context::deleteBuffer(GLuint buffer)
847{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000848 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849 {
850 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000852
853 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854}
855
856void Context::deleteShader(GLuint shader)
857{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000858 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
861void Context::deleteProgram(GLuint program)
862{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000863 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864}
865
866void Context::deleteTexture(GLuint texture)
867{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000868 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869 {
870 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000872
873 mResourceManager->deleteTexture(texture);
874}
875
876void Context::deleteRenderbuffer(GLuint renderbuffer)
877{
878 if (mResourceManager->getRenderbuffer(renderbuffer))
879 {
880 detachRenderbuffer(renderbuffer);
881 }
882
883 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884}
885
886void Context::deleteFramebuffer(GLuint framebuffer)
887{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000888 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
889
890 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891 {
892 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000893
benvanik@google.com1a233342011-04-28 19:44:39 +0000894 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000895 delete framebufferObject->second;
896 mFramebufferMap.erase(framebufferObject);
897 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000899
900void Context::deleteFence(GLuint fence)
901{
902 FenceMap::iterator fenceObject = mFenceMap.find(fence);
903
904 if (fenceObject != mFenceMap.end())
905 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000906 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000907 delete fenceObject->second;
908 mFenceMap.erase(fenceObject);
909 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000910}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000912Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000914 return mResourceManager->getBuffer(handle);
915}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000917Shader *Context::getShader(GLuint handle)
918{
919 return mResourceManager->getShader(handle);
920}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000922Program *Context::getProgram(GLuint handle)
923{
924 return mResourceManager->getProgram(handle);
925}
926
927Texture *Context::getTexture(GLuint handle)
928{
929 return mResourceManager->getTexture(handle);
930}
931
932Renderbuffer *Context::getRenderbuffer(GLuint handle)
933{
934 return mResourceManager->getRenderbuffer(handle);
935}
936
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000937Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000938{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000939 return getFramebuffer(mState.readFramebuffer);
940}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000941
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000942Framebuffer *Context::getDrawFramebuffer()
943{
944 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945}
946
947void Context::bindArrayBuffer(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.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
954void Context::bindElementArrayBuffer(unsigned int buffer)
955{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000956 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000958 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959}
960
961void Context::bindTexture2D(GLuint texture)
962{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000963 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000965 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
968void Context::bindTextureCubeMap(GLuint texture)
969{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000970 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000972 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000975void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000977 if (!getFramebuffer(framebuffer))
978 {
979 mFramebufferMap[framebuffer] = new Framebuffer();
980 }
981
982 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000983}
984
985void Context::bindDrawFramebuffer(GLuint framebuffer)
986{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000987 if (!getFramebuffer(framebuffer))
988 {
989 mFramebufferMap[framebuffer] = new Framebuffer();
990 }
991
992 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
995void Context::bindRenderbuffer(GLuint renderbuffer)
996{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000997 mResourceManager->checkRenderbufferAllocation(renderbuffer);
998
999 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000}
1001
1002void Context::useProgram(GLuint program)
1003{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001004 GLuint priorProgram = mState.currentProgram;
1005 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001006
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001007 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001009 Program *newProgram = mResourceManager->getProgram(program);
1010 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1011
1012 if (newProgram)
1013 {
1014 newProgram->addRef();
1015 }
1016
1017 if (oldProgram)
1018 {
1019 oldProgram->release();
1020 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022}
1023
1024void Context::setFramebufferZero(Framebuffer *buffer)
1025{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001026 delete mFramebufferMap[0];
1027 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001030void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001032 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1033 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034}
1035
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001036Framebuffer *Context::getFramebuffer(unsigned int handle)
1037{
1038 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1039
1040 if (framebuffer == mFramebufferMap.end())
1041 {
1042 return NULL;
1043 }
1044 else
1045 {
1046 return framebuffer->second;
1047 }
1048}
1049
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001050Fence *Context::getFence(unsigned int handle)
1051{
1052 FenceMap::iterator fence = mFenceMap.find(handle);
1053
1054 if (fence == mFenceMap.end())
1055 {
1056 return NULL;
1057 }
1058 else
1059 {
1060 return fence->second;
1061 }
1062}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001063
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064Buffer *Context::getArrayBuffer()
1065{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001066 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067}
1068
1069Buffer *Context::getElementArrayBuffer()
1070{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001071 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072}
1073
1074Program *Context::getCurrentProgram()
1075{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001076 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
1079Texture2D *Context::getTexture2D()
1080{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001081 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, SAMPLER_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082}
1083
1084TextureCubeMap *Context::getTextureCubeMap()
1085{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001086 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, SAMPLER_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087}
1088
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001089Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001091 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001092
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001093 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001094 {
1095 switch (type)
1096 {
1097 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001098 case SAMPLER_2D: return mTexture2DZero.get();
1099 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001100 }
1101 }
1102
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001103 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104}
1105
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001106bool Context::getBooleanv(GLenum pname, GLboolean *params)
1107{
1108 switch (pname)
1109 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001110 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1111 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1112 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001113 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001114 params[0] = mState.colorMaskRed;
1115 params[1] = mState.colorMaskGreen;
1116 params[2] = mState.colorMaskBlue;
1117 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001118 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001119 case GL_CULL_FACE: *params = mState.cullFace; break;
1120 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1121 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1122 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1123 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1124 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1125 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1126 case GL_BLEND: *params = mState.blend; break;
1127 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001128 default:
1129 return false;
1130 }
1131
1132 return true;
1133}
1134
1135bool Context::getFloatv(GLenum pname, GLfloat *params)
1136{
1137 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1138 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1139 // GetIntegerv as its native query function. As it would require conversion in any
1140 // case, this should make no difference to the calling application.
1141 switch (pname)
1142 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001143 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1144 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1145 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1146 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1147 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001148 case GL_ALIASED_LINE_WIDTH_RANGE:
1149 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1150 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1151 break;
1152 case GL_ALIASED_POINT_SIZE_RANGE:
1153 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001154 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 +00001155 break;
1156 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001157 params[0] = mState.zNear;
1158 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001159 break;
1160 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 params[0] = mState.colorClearValue.red;
1162 params[1] = mState.colorClearValue.green;
1163 params[2] = mState.colorClearValue.blue;
1164 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001165 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001166 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001167 params[0] = mState.blendColor.red;
1168 params[1] = mState.blendColor.green;
1169 params[2] = mState.blendColor.blue;
1170 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001171 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001172 default:
1173 return false;
1174 }
1175
1176 return true;
1177}
1178
1179bool Context::getIntegerv(GLenum pname, GLint *params)
1180{
1181 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1182 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1183 // GetIntegerv as its native query function. As it would require conversion in any
1184 // case, this should make no difference to the calling application. You may find it in
1185 // Context::getFloatv.
1186 switch (pname)
1187 {
1188 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1189 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001190 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001191 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1192 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1193 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001194 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001195 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001196 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001197 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001198 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1199 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001200 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1201 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1202 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001203 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001204 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1205 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1206 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1207 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001208 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001209 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1210 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1211 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1212 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1213 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1214 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1215 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1216 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1217 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1218 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1219 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1220 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1221 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1222 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1223 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1224 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1225 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1226 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1227 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1228 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1229 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1230 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1231 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1232 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001233 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1234 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001235 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1236 {
1237 if (supportsCompressedTextures())
1238 {
1239 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1240 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1241 *params = 2;
1242 }
1243 else
1244 {
1245 *params = 0;
1246 }
1247 }
1248 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001249 case GL_MAX_SAMPLES_ANGLE:
1250 {
1251 GLsizei maxSamples = getMaxSupportedSamples();
1252 if (maxSamples != 0)
1253 {
1254 *params = maxSamples;
1255 }
1256 else
1257 {
1258 return false;
1259 }
1260
1261 break;
1262 }
1263 case GL_SAMPLE_BUFFERS:
1264 case GL_SAMPLES:
1265 {
1266 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1267 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1268 {
1269 switch (pname)
1270 {
1271 case GL_SAMPLE_BUFFERS:
1272 if (framebuffer->getSamples() != 0)
1273 {
1274 *params = 1;
1275 }
1276 else
1277 {
1278 *params = 0;
1279 }
1280 break;
1281 case GL_SAMPLES:
1282 *params = framebuffer->getSamples();
1283 break;
1284 }
1285 }
1286 else
1287 {
1288 *params = 0;
1289 }
1290 }
1291 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001292 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1293 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1294 case GL_MAX_VIEWPORT_DIMS:
1295 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001296 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001297 params[0] = maxDimension;
1298 params[1] = maxDimension;
1299 }
1300 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001301 case GL_COMPRESSED_TEXTURE_FORMATS:
1302 {
1303 if (supportsCompressedTextures())
1304 {
1305 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1306 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1307 }
1308 }
1309 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001310 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001311 params[0] = mState.viewportX;
1312 params[1] = mState.viewportY;
1313 params[2] = mState.viewportWidth;
1314 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001315 break;
1316 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001317 params[0] = mState.scissorX;
1318 params[1] = mState.scissorY;
1319 params[2] = mState.scissorWidth;
1320 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001321 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001322 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1323 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001324 case GL_RED_BITS:
1325 case GL_GREEN_BITS:
1326 case GL_BLUE_BITS:
1327 case GL_ALPHA_BITS:
1328 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001329 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001330 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1331
1332 if (colorbuffer)
1333 {
1334 switch (pname)
1335 {
1336 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1337 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1338 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1339 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1340 }
1341 }
1342 else
1343 {
1344 *params = 0;
1345 }
1346 }
1347 break;
1348 case GL_DEPTH_BITS:
1349 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001350 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001351 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001352
1353 if (depthbuffer)
1354 {
1355 *params = depthbuffer->getDepthSize();
1356 }
1357 else
1358 {
1359 *params = 0;
1360 }
1361 }
1362 break;
1363 case GL_STENCIL_BITS:
1364 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001365 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001366 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001367
1368 if (stencilbuffer)
1369 {
1370 *params = stencilbuffer->getStencilSize();
1371 }
1372 else
1373 {
1374 *params = 0;
1375 }
1376 }
1377 break;
1378 case GL_TEXTURE_BINDING_2D:
1379 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001380 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001381 {
1382 error(GL_INVALID_OPERATION);
1383 return false;
1384 }
1385
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001386 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001387 }
1388 break;
1389 case GL_TEXTURE_BINDING_CUBE_MAP:
1390 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001391 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001392 {
1393 error(GL_INVALID_OPERATION);
1394 return false;
1395 }
1396
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001397 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001398 }
1399 break;
1400 default:
1401 return false;
1402 }
1403
1404 return true;
1405}
1406
1407bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1408{
1409 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1410 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1411 // to the fact that it is stored internally as a float, and so would require conversion
1412 // if returned from Context::getIntegerv. Since this conversion is already implemented
1413 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1414 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1415 // application.
1416 switch (pname)
1417 {
1418 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1419 case GL_SHADER_BINARY_FORMATS:
1420 {
1421 *type = GL_INT;
1422 *numParams = 0;
1423 }
1424 break;
1425 case GL_MAX_VERTEX_ATTRIBS:
1426 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1427 case GL_MAX_VARYING_VECTORS:
1428 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1429 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1430 case GL_MAX_TEXTURE_IMAGE_UNITS:
1431 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1432 case GL_MAX_RENDERBUFFER_SIZE:
1433 case GL_NUM_SHADER_BINARY_FORMATS:
1434 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1435 case GL_ARRAY_BUFFER_BINDING:
1436 case GL_FRAMEBUFFER_BINDING:
1437 case GL_RENDERBUFFER_BINDING:
1438 case GL_CURRENT_PROGRAM:
1439 case GL_PACK_ALIGNMENT:
1440 case GL_UNPACK_ALIGNMENT:
1441 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001442 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001443 case GL_RED_BITS:
1444 case GL_GREEN_BITS:
1445 case GL_BLUE_BITS:
1446 case GL_ALPHA_BITS:
1447 case GL_DEPTH_BITS:
1448 case GL_STENCIL_BITS:
1449 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1450 case GL_CULL_FACE_MODE:
1451 case GL_FRONT_FACE:
1452 case GL_ACTIVE_TEXTURE:
1453 case GL_STENCIL_FUNC:
1454 case GL_STENCIL_VALUE_MASK:
1455 case GL_STENCIL_REF:
1456 case GL_STENCIL_FAIL:
1457 case GL_STENCIL_PASS_DEPTH_FAIL:
1458 case GL_STENCIL_PASS_DEPTH_PASS:
1459 case GL_STENCIL_BACK_FUNC:
1460 case GL_STENCIL_BACK_VALUE_MASK:
1461 case GL_STENCIL_BACK_REF:
1462 case GL_STENCIL_BACK_FAIL:
1463 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1464 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1465 case GL_DEPTH_FUNC:
1466 case GL_BLEND_SRC_RGB:
1467 case GL_BLEND_SRC_ALPHA:
1468 case GL_BLEND_DST_RGB:
1469 case GL_BLEND_DST_ALPHA:
1470 case GL_BLEND_EQUATION_RGB:
1471 case GL_BLEND_EQUATION_ALPHA:
1472 case GL_STENCIL_WRITEMASK:
1473 case GL_STENCIL_BACK_WRITEMASK:
1474 case GL_STENCIL_CLEAR_VALUE:
1475 case GL_SUBPIXEL_BITS:
1476 case GL_MAX_TEXTURE_SIZE:
1477 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1478 case GL_SAMPLE_BUFFERS:
1479 case GL_SAMPLES:
1480 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1481 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1482 case GL_TEXTURE_BINDING_2D:
1483 case GL_TEXTURE_BINDING_CUBE_MAP:
1484 {
1485 *type = GL_INT;
1486 *numParams = 1;
1487 }
1488 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001489 case GL_MAX_SAMPLES_ANGLE:
1490 {
1491 if (getMaxSupportedSamples() != 0)
1492 {
1493 *type = GL_INT;
1494 *numParams = 1;
1495 }
1496 else
1497 {
1498 return false;
1499 }
1500 }
1501 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001502 case GL_MAX_VIEWPORT_DIMS:
1503 {
1504 *type = GL_INT;
1505 *numParams = 2;
1506 }
1507 break;
1508 case GL_VIEWPORT:
1509 case GL_SCISSOR_BOX:
1510 {
1511 *type = GL_INT;
1512 *numParams = 4;
1513 }
1514 break;
1515 case GL_SHADER_COMPILER:
1516 case GL_SAMPLE_COVERAGE_INVERT:
1517 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001518 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1519 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1520 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1521 case GL_SAMPLE_COVERAGE:
1522 case GL_SCISSOR_TEST:
1523 case GL_STENCIL_TEST:
1524 case GL_DEPTH_TEST:
1525 case GL_BLEND:
1526 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001527 {
1528 *type = GL_BOOL;
1529 *numParams = 1;
1530 }
1531 break;
1532 case GL_COLOR_WRITEMASK:
1533 {
1534 *type = GL_BOOL;
1535 *numParams = 4;
1536 }
1537 break;
1538 case GL_POLYGON_OFFSET_FACTOR:
1539 case GL_POLYGON_OFFSET_UNITS:
1540 case GL_SAMPLE_COVERAGE_VALUE:
1541 case GL_DEPTH_CLEAR_VALUE:
1542 case GL_LINE_WIDTH:
1543 {
1544 *type = GL_FLOAT;
1545 *numParams = 1;
1546 }
1547 break;
1548 case GL_ALIASED_LINE_WIDTH_RANGE:
1549 case GL_ALIASED_POINT_SIZE_RANGE:
1550 case GL_DEPTH_RANGE:
1551 {
1552 *type = GL_FLOAT;
1553 *numParams = 2;
1554 }
1555 break;
1556 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001557 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001558 {
1559 *type = GL_FLOAT;
1560 *numParams = 4;
1561 }
1562 break;
1563 default:
1564 return false;
1565 }
1566
1567 return true;
1568}
1569
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001570// Applies the render target surface, depth stencil surface, viewport rectangle and
1571// scissor rectangle to the Direct3D 9 device
1572bool Context::applyRenderTarget(bool ignoreViewport)
1573{
1574 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001575
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001576 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577
1578 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1579 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001580 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001581 }
1582
1583 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001584
1585 if (!renderTarget)
1586 {
1587 return false; // Context must be lost
1588 }
1589
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001590 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001591
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001592 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1593 if (renderTargetSerial != mAppliedRenderTargetSerial)
1594 {
1595 device->SetRenderTarget(0, renderTarget);
1596 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001597 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 +00001598 }
1599
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001600 unsigned int depthbufferSerial = 0;
1601 unsigned int stencilbufferSerial = 0;
1602 if (framebufferObject->getDepthbufferType() != GL_NONE)
1603 {
1604 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001605 if (!depthStencil)
1606 {
1607 ERR("Depth stencil pointer unexpectedly null.");
1608 return false;
1609 }
1610
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001611 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1612 }
1613 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1614 {
1615 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001616 if (!depthStencil)
1617 {
1618 ERR("Depth stencil pointer unexpectedly null.");
1619 return false;
1620 }
1621
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001622 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1623 }
1624
1625 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001626 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001627 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001628 {
1629 device->SetDepthStencilSurface(depthStencil);
1630 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001631 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001632 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634
1635 D3DVIEWPORT9 viewport;
1636 D3DSURFACE_DESC desc;
1637 renderTarget->GetDesc(&desc);
1638
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001639 float zNear = clamp01(mState.zNear);
1640 float zFar = clamp01(mState.zFar);
1641
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642 if (ignoreViewport)
1643 {
1644 viewport.X = 0;
1645 viewport.Y = 0;
1646 viewport.Width = desc.Width;
1647 viewport.Height = desc.Height;
1648 viewport.MinZ = 0.0f;
1649 viewport.MaxZ = 1.0f;
1650 }
1651 else
1652 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001653 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1654 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1655 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1656 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1657 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 +00001658 viewport.MinZ = zNear;
1659 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001660 }
1661
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001662 if (viewport.Width <= 0 || viewport.Height <= 0)
1663 {
1664 return false; // Nothing to render
1665 }
1666
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 device->SetViewport(&viewport);
1668
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001669 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001671 if (mState.scissorTest)
1672 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001673 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1674 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1675 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1676 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1677 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001678 device->SetScissorRect(&rect);
1679 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1680 }
1681 else
1682 {
1683 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1684 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001685
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001686 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687 }
1688
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001689 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691 Program *programObject = getCurrentProgram();
1692
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001693 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001694 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001695 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001696
daniel@transgaming.com31754962010-11-28 02:02:52 +00001697 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001698 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1699 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1700 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001701 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001702
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001703 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001704 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001705 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001706
daniel@transgaming.com31754962010-11-28 02:02:52 +00001707 GLint depthRange = programObject->getDxDepthRangeLocation();
1708 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1709 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001710 }
1711
1712 return true;
1713}
1714
1715// 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 +00001716void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717{
1718 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001719 Program *programObject = getCurrentProgram();
1720
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001721 Framebuffer *framebufferObject = getDrawFramebuffer();
1722
1723 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1724
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001725 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001726 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001727 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001729 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001730 GLint alwaysFront = !isTriangleMode(drawMode);
1731 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1732
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001733 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001735 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001737 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 }
1739 else
1740 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 }
1743
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001744 mCullStateDirty = false;
1745 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 if (mDepthStateDirty)
1748 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001749 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1752 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753 }
1754 else
1755 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001758
1759 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 }
1761
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001762 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001764 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001765 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001766 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1767
1768 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1769 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1770 {
1771 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1772 }
1773 else
1774 {
1775 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1776 unorm<8>(mState.blendColor.alpha),
1777 unorm<8>(mState.blendColor.alpha),
1778 unorm<8>(mState.blendColor.alpha)));
1779 }
1780
1781 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1782 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1783 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1784
1785 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1786 mState.destBlendRGB != mState.destBlendAlpha ||
1787 mState.blendEquationRGB != mState.blendEquationAlpha)
1788 {
1789 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1790
1791 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1792 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1793 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001794 }
1795 else
1796 {
1797 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1798 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001799 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001800 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001801 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001802 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001803 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001804
1805 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806 }
1807
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001808 if (mStencilStateDirty || mFrontFaceDirty)
1809 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001810 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001811 {
1812 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1813 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1814
1815 // FIXME: Unsupported by D3D9
1816 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1817 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1818 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1819 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1820 mState.stencilRef != mState.stencilBackRef ||
1821 mState.stencilMask != mState.stencilBackMask)
1822 {
1823 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1824 return error(GL_INVALID_OPERATION);
1825 }
1826
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001827 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001828 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001829 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1830
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001831 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1832 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001833 es2dx::ConvertComparison(mState.stencilFunc));
1834
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001835 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1836 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001837
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001838 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001839 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001840 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001841 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001842 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001843 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1844
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001845 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1846 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001847 es2dx::ConvertComparison(mState.stencilBackFunc));
1848
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001849 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1850 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001851
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001852 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001853 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001854 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001855 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001856 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001857 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1858 }
1859 else
1860 {
1861 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1862 }
1863
1864 mStencilStateDirty = false;
1865 }
1866
1867 if (mMaskStateDirty)
1868 {
1869 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1870 mState.colorMaskBlue, mState.colorMaskAlpha));
1871 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1872
1873 mMaskStateDirty = false;
1874 }
1875
1876 if (mPolygonOffsetStateDirty)
1877 {
1878 if (mState.polygonOffsetFill)
1879 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001880 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001881 if (depthbuffer)
1882 {
1883 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1884 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1885 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1886 }
1887 }
1888 else
1889 {
1890 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1891 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1892 }
1893
1894 mPolygonOffsetStateDirty = false;
1895 }
1896
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001897 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001899 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001900 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001901 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001902 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001903 FIXME("Sample alpha to coverage is unimplemented.");
1904 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001905
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001906 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1907 if (mState.sampleCoverage)
1908 {
1909 unsigned int mask = 0;
1910 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001911 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001912 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001913
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001914 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001915 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001916 mask <<= 1;
1917
1918 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1919 {
1920 threshold += 1.0f;
1921 mask |= 1;
1922 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001923 }
1924 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001925
1926 if (mState.sampleCoverageInvert)
1927 {
1928 mask = ~mask;
1929 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001930
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001931 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1932 }
1933 else
1934 {
1935 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1936 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001937 }
1938 else
1939 {
1940 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001941 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001942
1943 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 }
1945
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001946 if (mDitherStateDirty)
1947 {
1948 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1949
1950 mDitherStateDirty = false;
1951 }
1952
1953 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954}
1955
daniel@transgaming.com83921382011-01-08 05:46:00 +00001956GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001957{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001958 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001959
daniel@transgaming.combaa74512011-04-13 14:56:47 +00001960 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001961 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001962 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001963 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001964 }
1965
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00001966 return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001967}
1968
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001969// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001970GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001971{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001972 IDirect3DDevice9 *device = getDevice();
1973 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001974
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001975 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001976 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001977 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001978 }
1979
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001980 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981}
1982
1983// Applies the shaders and shader constants to the Direct3D 9 device
1984void Context::applyShaders()
1985{
1986 IDirect3DDevice9 *device = getDevice();
1987 Program *programObject = getCurrentProgram();
1988 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1989 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1990
1991 device->SetVertexShader(vertexShader);
1992 device->SetPixelShader(pixelShader);
1993
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001994 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001995 {
1996 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00001997 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001998 }
1999
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000 programObject->applyUniforms();
2001}
2002
2003// Applies the textures and sampler states to the Direct3D 9 device
2004void Context::applyTextures()
2005{
2006 IDirect3DDevice9 *device = getDevice();
2007 Program *programObject = getCurrentProgram();
2008
2009 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2010 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002011 int textureUnit = programObject->getSamplerMapping(sampler);
2012 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002014 SamplerType textureType = programObject->getSamplerType(sampler);
2015
2016 Texture *texture = getSamplerTexture(textureUnit, textureType);
2017
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002018 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002019 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002020 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2021
2022 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002023 {
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002024 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyParameter())
2025 {
2026 GLenum wrapS = texture->getWrapS();
2027 GLenum wrapT = texture->getWrapT();
2028 GLenum minFilter = texture->getMinFilter();
2029 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002031 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2032 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002034 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2035 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2036 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2037 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2038 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
2039 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002041 if (mAppliedTextureSerial[sampler] != texture->getSerial() || texture->isDirtyImage())
2042 {
2043 device->SetTexture(sampler, d3dTexture);
2044 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002045 }
2046 else
2047 {
2048 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2049 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002050
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002051 mAppliedTextureSerial[sampler] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002052 texture->resetDirty();
2053 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002055 else
2056 {
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002057 if (mAppliedTextureSerial[sampler] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002058 {
2059 device->SetTexture(sampler, NULL);
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002060 mAppliedTextureSerial[sampler] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002061 }
2062 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002063 }
2064}
2065
2066void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2067{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002068 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002069
2070 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2071 {
2072 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2073 }
2074
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002075 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2076 {
2077 return error(GL_INVALID_OPERATION);
2078 }
2079
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002081
2082 if (!renderTarget)
2083 {
2084 return; // Context must be lost, return silently
2085 }
2086
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 IDirect3DDevice9 *device = getDevice();
2088
2089 D3DSURFACE_DESC desc;
2090 renderTarget->GetDesc(&desc);
2091
2092 IDirect3DSurface9 *systemSurface;
2093 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2094
2095 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2096 {
2097 return error(GL_OUT_OF_MEMORY);
2098 }
2099
2100 ASSERT(SUCCEEDED(result));
2101
2102 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2103 {
2104 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2105 }
2106
2107 result = device->GetRenderTargetData(renderTarget, systemSurface);
2108
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 if (FAILED(result))
2110 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111 systemSurface->Release();
2112
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002113 switch (result)
2114 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002115 case D3DERR_DRIVERINTERNALERROR:
2116 case D3DERR_DEVICELOST:
2117 return error(GL_OUT_OF_MEMORY);
2118 default:
2119 UNREACHABLE();
2120 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002121 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 }
2123
2124 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002125 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2126 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2127 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2128 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2129 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130
2131 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2132
2133 if (FAILED(result))
2134 {
2135 UNREACHABLE();
2136 systemSurface->Release();
2137
2138 return; // No sensible error to generate
2139 }
2140
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002141 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002143 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002144 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002145 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002146
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147 for (int j = 0; j < rect.bottom - rect.top; j++)
2148 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002149 if (desc.Format == D3DFMT_A8R8G8B8 &&
2150 format == GL_BGRA_EXT &&
2151 type == GL_UNSIGNED_BYTE)
2152 {
2153 // Fast path for EXT_read_format_bgra, given
2154 // an RGBA source buffer. Note that buffers with no
2155 // alpha go through the slow path below.
2156 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002157 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002158 (rect.right - rect.left) * 4);
2159 continue;
2160 }
2161
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 for (int i = 0; i < rect.right - rect.left; i++)
2163 {
2164 float r;
2165 float g;
2166 float b;
2167 float a;
2168
2169 switch (desc.Format)
2170 {
2171 case D3DFMT_R5G6B5:
2172 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002173 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174
2175 a = 1.0f;
2176 b = (rgb & 0x001F) * (1.0f / 0x001F);
2177 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2178 r = (rgb & 0xF800) * (1.0f / 0xF800);
2179 }
2180 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 case D3DFMT_A1R5G5B5:
2182 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002183 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002184
2185 a = (argb & 0x8000) ? 1.0f : 0.0f;
2186 b = (argb & 0x001F) * (1.0f / 0x001F);
2187 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2188 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2189 }
2190 break;
2191 case D3DFMT_A8R8G8B8:
2192 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002193 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002194
2195 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2196 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2197 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2198 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2199 }
2200 break;
2201 case D3DFMT_X8R8G8B8:
2202 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002203 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002204
2205 a = 1.0f;
2206 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2207 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2208 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2209 }
2210 break;
2211 case D3DFMT_A2R10G10B10:
2212 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002213 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214
2215 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2216 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2217 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2218 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2219 }
2220 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002221 case D3DFMT_A32B32G32R32F:
2222 {
2223 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002224 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2225 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2226 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2227 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002228 }
2229 break;
2230 case D3DFMT_A16B16G16R16F:
2231 {
2232 // float formats in D3D are stored rgba, rather than the other way round
2233 float abgr[4];
2234
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002235 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002236
2237 a = abgr[3];
2238 b = abgr[2];
2239 g = abgr[1];
2240 r = abgr[0];
2241 }
2242 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002243 default:
2244 UNIMPLEMENTED(); // FIXME
2245 UNREACHABLE();
2246 }
2247
2248 switch (format)
2249 {
2250 case GL_RGBA:
2251 switch (type)
2252 {
2253 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002254 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2255 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2256 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2257 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002258 break;
2259 default: UNREACHABLE();
2260 }
2261 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002262 case GL_BGRA_EXT:
2263 switch (type)
2264 {
2265 case GL_UNSIGNED_BYTE:
2266 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2267 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2268 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2269 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2270 break;
2271 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2272 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2273 // this type is packed as follows:
2274 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2275 // --------------------------------------------------------------------------------
2276 // | 4th | 3rd | 2nd | 1st component |
2277 // --------------------------------------------------------------------------------
2278 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2279 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2280 ((unsigned short)(15 * a + 0.5f) << 12)|
2281 ((unsigned short)(15 * r + 0.5f) << 8) |
2282 ((unsigned short)(15 * g + 0.5f) << 4) |
2283 ((unsigned short)(15 * b + 0.5f) << 0);
2284 break;
2285 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2286 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2287 // this type is packed as follows:
2288 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2289 // --------------------------------------------------------------------------------
2290 // | 4th | 3rd | 2nd | 1st component |
2291 // --------------------------------------------------------------------------------
2292 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2293 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2294 ((unsigned short)( a + 0.5f) << 15) |
2295 ((unsigned short)(31 * r + 0.5f) << 10) |
2296 ((unsigned short)(31 * g + 0.5f) << 5) |
2297 ((unsigned short)(31 * b + 0.5f) << 0);
2298 break;
2299 default: UNREACHABLE();
2300 }
2301 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002302 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 switch (type)
2304 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002305 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002306 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2307 ((unsigned short)(31 * b + 0.5f) << 0) |
2308 ((unsigned short)(63 * g + 0.5f) << 5) |
2309 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 break;
2311 default: UNREACHABLE();
2312 }
2313 break;
2314 default: UNREACHABLE();
2315 }
2316 }
2317 }
2318
2319 systemSurface->UnlockRect();
2320
2321 systemSurface->Release();
2322}
2323
2324void Context::clear(GLbitfield mask)
2325{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002326 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002327
2328 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2329 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002330 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002331 }
2332
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002333 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334 IDirect3DDevice9 *device = getDevice();
2335 DWORD flags = 0;
2336
2337 if (mask & GL_COLOR_BUFFER_BIT)
2338 {
2339 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002340
2341 if (framebufferObject->getColorbufferType() != GL_NONE)
2342 {
2343 flags |= D3DCLEAR_TARGET;
2344 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345 }
2346
2347 if (mask & GL_DEPTH_BUFFER_BIT)
2348 {
2349 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002350 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351 {
2352 flags |= D3DCLEAR_ZBUFFER;
2353 }
2354 }
2355
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002356 GLuint stencilUnmasked = 0x0;
2357
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002358 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002361 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002363 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002364 if (!depthStencil)
2365 {
2366 ERR("Depth stencil pointer unexpectedly null.");
2367 return;
2368 }
2369
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002370 D3DSURFACE_DESC desc;
2371 depthStencil->GetDesc(&desc);
2372
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002373 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002374 stencilUnmasked = (0x1 << stencilSize) - 1;
2375
2376 if (stencilUnmasked != 0x0)
2377 {
2378 flags |= D3DCLEAR_STENCIL;
2379 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380 }
2381 }
2382
2383 if (mask != 0)
2384 {
2385 return error(GL_INVALID_VALUE);
2386 }
2387
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002388 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2389 {
2390 return;
2391 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002393 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002394 unorm<8>(mState.colorClearValue.red),
2395 unorm<8>(mState.colorClearValue.green),
2396 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002397 float depth = clamp01(mState.depthClearValue);
2398 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399
2400 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2401
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002402 if (!renderTarget)
2403 {
2404 return; // Context must be lost, return silently
2405 }
2406
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407 D3DSURFACE_DESC desc;
2408 renderTarget->GetDesc(&desc);
2409
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002410 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411
2412 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002413 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002415 !(mState.colorMaskRed && mState.colorMaskGreen &&
2416 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417
2418 if (needMaskedColorClear || needMaskedStencilClear)
2419 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002420 // State which is altered in all paths from this point to the clear call is saved.
2421 // State which is altered in only some paths will be flagged dirty in the case that
2422 // that path is taken.
2423 HRESULT hr;
2424 if (mMaskedClearSavedState == NULL)
2425 {
2426 hr = device->BeginStateBlock();
2427 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2428
2429 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2430 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2431 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2432 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2433 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2434 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2435 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2436 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2437 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2438 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2439 device->SetPixelShader(NULL);
2440 device->SetVertexShader(NULL);
2441 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002442 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2443 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2444 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2445 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2446 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2447 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2448 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002449
2450 hr = device->EndStateBlock(&mMaskedClearSavedState);
2451 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2452 }
2453
2454 ASSERT(mMaskedClearSavedState != NULL);
2455
2456 if (mMaskedClearSavedState != NULL)
2457 {
2458 hr = mMaskedClearSavedState->Capture();
2459 ASSERT(SUCCEEDED(hr));
2460 }
2461
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2463 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2464 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2465 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2466 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2467 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2468 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2469 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2470
2471 if (flags & D3DCLEAR_TARGET)
2472 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002473 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474 }
2475 else
2476 {
2477 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2478 }
2479
2480 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2481 {
2482 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2483 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2484 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2485 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002486 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002487 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2489 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002490 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 }
2492 else
2493 {
2494 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2495 }
2496
2497 device->SetPixelShader(NULL);
2498 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002499 device->SetFVF(D3DFVF_XYZRHW);
2500 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2501 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2502 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2503 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2504 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2505 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2506 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002508 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2509 quad[0][0] = -0.5f;
2510 quad[0][1] = desc.Height - 0.5f;
2511 quad[0][2] = 0.0f;
2512 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002513
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002514 quad[1][0] = desc.Width - 0.5f;
2515 quad[1][1] = desc.Height - 0.5f;
2516 quad[1][2] = 0.0f;
2517 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002519 quad[2][0] = -0.5f;
2520 quad[2][1] = -0.5f;
2521 quad[2][2] = 0.0f;
2522 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002524 quad[3][0] = desc.Width - 0.5f;
2525 quad[3][1] = -0.5f;
2526 quad[3][2] = 0.0f;
2527 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002528
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002529 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002530 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531
2532 if (flags & D3DCLEAR_ZBUFFER)
2533 {
2534 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2535 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2536 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2537 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002538
2539 if (mMaskedClearSavedState != NULL)
2540 {
2541 mMaskedClearSavedState->Apply();
2542 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002544 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545 {
2546 device->Clear(0, NULL, flags, color, depth, stencil);
2547 }
2548}
2549
2550void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2551{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002552 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553 {
2554 return error(GL_INVALID_OPERATION);
2555 }
2556
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002557 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 IDirect3DDevice9 *device = getDevice();
2559 D3DPRIMITIVETYPE primitiveType;
2560 int primitiveCount;
2561
2562 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2563 return error(GL_INVALID_ENUM);
2564
2565 if (primitiveCount <= 0)
2566 {
2567 return;
2568 }
2569
2570 if (!applyRenderTarget(false))
2571 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002572 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002573 }
2574
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002575 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002576
daniel@transgaming.com83921382011-01-08 05:46:00 +00002577 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002578 if (err != GL_NO_ERROR)
2579 {
2580 return error(err);
2581 }
2582
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583 applyShaders();
2584 applyTextures();
2585
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002586 if (!getCurrentProgram()->validateSamplers())
2587 {
2588 return error(GL_INVALID_OPERATION);
2589 }
2590
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002591 if (!cullSkipsDraw(mode))
2592 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002593 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002594
2595 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002596
2597 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2598 {
2599 drawClosingLine(first, first + count - 1);
2600 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002601 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602}
2603
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002604void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002605{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002606 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002607 {
2608 return error(GL_INVALID_OPERATION);
2609 }
2610
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002611 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002612 {
2613 return error(GL_INVALID_OPERATION);
2614 }
2615
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002616 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617 IDirect3DDevice9 *device = getDevice();
2618 D3DPRIMITIVETYPE primitiveType;
2619 int primitiveCount;
2620
2621 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2622 return error(GL_INVALID_ENUM);
2623
2624 if (primitiveCount <= 0)
2625 {
2626 return;
2627 }
2628
2629 if (!applyRenderTarget(false))
2630 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002631 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632 }
2633
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002634 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002635
2636 TranslatedIndexData indexInfo;
2637 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2638 if (err != GL_NO_ERROR)
2639 {
2640 return error(err);
2641 }
2642
daniel@transgaming.com83921382011-01-08 05:46:00 +00002643 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2644 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002645 if (err != GL_NO_ERROR)
2646 {
2647 return error(err);
2648 }
2649
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002650 applyShaders();
2651 applyTextures();
2652
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002653 if (!getCurrentProgram()->validateSamplers())
2654 {
2655 return error(GL_INVALID_OPERATION);
2656 }
2657
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002658 if (!cullSkipsDraw(mode))
2659 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002660 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002661
daniel@transgaming.com83921382011-01-08 05:46:00 +00002662 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002663
2664 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2665 {
2666 drawClosingLine(count, type, indices);
2667 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002668 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002669}
2670
2671void Context::finish()
2672{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002673 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002674 IDirect3DDevice9 *device = getDevice();
2675 IDirect3DQuery9 *occlusionQuery = NULL;
2676
2677 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2678
2679 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2680 {
2681 return error(GL_OUT_OF_MEMORY);
2682 }
2683
2684 ASSERT(SUCCEEDED(result));
2685
2686 if (occlusionQuery)
2687 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002688 IDirect3DStateBlock9 *savedState = NULL;
2689 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2690
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002691 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2692 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002693
2694 // Render something outside the render target
2695 device->SetPixelShader(NULL);
2696 device->SetVertexShader(NULL);
2697 device->SetFVF(D3DFVF_XYZRHW);
2698 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002699 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002701
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002702 result = occlusionQuery->Issue(D3DISSUE_END);
2703 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002704
2705 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2706 {
2707 // Keep polling, but allow other threads to do something useful first
2708 Sleep(0);
2709 }
2710
2711 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002712
2713 if (savedState)
2714 {
2715 savedState->Apply();
2716 savedState->Release();
2717 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002718 }
2719}
2720
2721void Context::flush()
2722{
2723 IDirect3DDevice9 *device = getDevice();
2724 IDirect3DQuery9 *eventQuery = NULL;
2725
2726 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2727
2728 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2729 {
2730 return error(GL_OUT_OF_MEMORY);
2731 }
2732
2733 ASSERT(SUCCEEDED(result));
2734
2735 if (eventQuery)
2736 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002737 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2738 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002739
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002740 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002741 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002742
2743 if (result == D3DERR_DEVICELOST)
2744 {
2745 error(GL_OUT_OF_MEMORY);
2746 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002747 }
2748}
2749
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002750void Context::drawClosingLine(unsigned int first, unsigned int last)
2751{
2752 IDirect3DDevice9 *device = getDevice();
2753 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002754 bool succeeded = false;
2755 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002756
2757 if (supports32bitIndices())
2758 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002759 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002760
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002761 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002762 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002763 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
2764 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002765
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002766 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2767
2768 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2769 if (data)
2770 {
2771 data[0] = last;
2772 data[1] = first;
2773 mClosingIB->unmap();
2774 offset /= 4;
2775 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002776 }
2777 }
2778 else
2779 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002780 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002781
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002782 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002783 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002784 mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
2785 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002786
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002787 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2788
2789 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2790 if (data)
2791 {
2792 data[0] = last;
2793 data[1] = first;
2794 mClosingIB->unmap();
2795 offset /= 2;
2796 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002797 }
2798 }
2799
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002800 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002801 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002802 device->SetIndices(mClosingIB->getBuffer());
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002803
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002804 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002805 }
2806 else
2807 {
2808 ERR("Could not create an index buffer for closing a line loop.");
2809 error(GL_OUT_OF_MEMORY);
2810 }
2811}
2812
2813void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2814{
2815 unsigned int first = 0;
2816 unsigned int last = 0;
2817
2818 if (mState.elementArrayBuffer.get())
2819 {
2820 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2821 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2822 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2823 }
2824
2825 switch (type)
2826 {
2827 case GL_UNSIGNED_BYTE:
2828 first = static_cast<const GLubyte*>(indices)[0];
2829 last = static_cast<const GLubyte*>(indices)[count - 1];
2830 break;
2831 case GL_UNSIGNED_SHORT:
2832 first = static_cast<const GLushort*>(indices)[0];
2833 last = static_cast<const GLushort*>(indices)[count - 1];
2834 break;
2835 case GL_UNSIGNED_INT:
2836 first = static_cast<const GLuint*>(indices)[0];
2837 last = static_cast<const GLuint*>(indices)[count - 1];
2838 break;
2839 default: UNREACHABLE();
2840 }
2841
2842 drawClosingLine(first, last);
2843}
2844
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002845void Context::recordInvalidEnum()
2846{
2847 mInvalidEnum = true;
2848}
2849
2850void Context::recordInvalidValue()
2851{
2852 mInvalidValue = true;
2853}
2854
2855void Context::recordInvalidOperation()
2856{
2857 mInvalidOperation = true;
2858}
2859
2860void Context::recordOutOfMemory()
2861{
2862 mOutOfMemory = true;
2863}
2864
2865void Context::recordInvalidFramebufferOperation()
2866{
2867 mInvalidFramebufferOperation = true;
2868}
2869
2870// Get one of the recorded errors and clear its flag, if any.
2871// [OpenGL ES 2.0.24] section 2.5 page 13.
2872GLenum Context::getError()
2873{
2874 if (mInvalidEnum)
2875 {
2876 mInvalidEnum = false;
2877
2878 return GL_INVALID_ENUM;
2879 }
2880
2881 if (mInvalidValue)
2882 {
2883 mInvalidValue = false;
2884
2885 return GL_INVALID_VALUE;
2886 }
2887
2888 if (mInvalidOperation)
2889 {
2890 mInvalidOperation = false;
2891
2892 return GL_INVALID_OPERATION;
2893 }
2894
2895 if (mOutOfMemory)
2896 {
2897 mOutOfMemory = false;
2898
2899 return GL_OUT_OF_MEMORY;
2900 }
2901
2902 if (mInvalidFramebufferOperation)
2903 {
2904 mInvalidFramebufferOperation = false;
2905
2906 return GL_INVALID_FRAMEBUFFER_OPERATION;
2907 }
2908
2909 return GL_NO_ERROR;
2910}
2911
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002912bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002913{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002914 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002915}
2916
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002917int Context::getMaximumVaryingVectors() const
2918{
2919 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2920}
2921
daniel@transgaming.com458da142010-11-28 02:03:02 +00002922int Context::getMaximumFragmentUniformVectors() const
2923{
2924 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2925}
2926
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002927int Context::getMaxSupportedSamples() const
2928{
2929 return mMaxSupportedSamples;
2930}
2931
2932int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2933{
2934 if (requested == 0)
2935 {
2936 return requested;
2937 }
2938
2939 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2940 if (itr == mMultiSampleSupport.end())
2941 {
2942 return -1;
2943 }
2944
2945 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2946 {
2947 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2948 {
2949 return i;
2950 }
2951 }
2952
2953 return -1;
2954}
2955
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002956bool Context::supportsEventQueries() const
2957{
2958 return mSupportsEventQueries;
2959}
2960
daniel@transgaming.com01868132010-08-24 19:21:17 +00002961bool Context::supportsCompressedTextures() const
2962{
2963 return mSupportsCompressedTextures;
2964}
2965
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002966bool Context::supportsFloatTextures() const
2967{
2968 return mSupportsFloatTextures;
2969}
2970
2971bool Context::supportsFloatLinearFilter() const
2972{
2973 return mSupportsFloatLinearFilter;
2974}
2975
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002976bool Context::supportsFloatRenderableTextures() const
2977{
2978 return mSupportsFloatRenderableTextures;
2979}
2980
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002981bool Context::supportsHalfFloatTextures() const
2982{
2983 return mSupportsHalfFloatTextures;
2984}
2985
2986bool Context::supportsHalfFloatLinearFilter() const
2987{
2988 return mSupportsHalfFloatLinearFilter;
2989}
2990
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002991bool Context::supportsHalfFloatRenderableTextures() const
2992{
2993 return mSupportsHalfFloatRenderableTextures;
2994}
2995
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00002996int Context::getMaximumRenderbufferDimension() const
2997{
2998 return mMaxRenderbufferDimension;
2999}
3000
3001int Context::getMaximumTextureDimension() const
3002{
3003 return mMaxTextureDimension;
3004}
3005
3006int Context::getMaximumCubeTextureDimension() const
3007{
3008 return mMaxCubeTextureDimension;
3009}
3010
3011int Context::getMaximumTextureLevel() const
3012{
3013 return mMaxTextureLevel;
3014}
3015
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003016bool Context::supportsLuminanceTextures() const
3017{
3018 return mSupportsLuminanceTextures;
3019}
3020
3021bool Context::supportsLuminanceAlphaTextures() const
3022{
3023 return mSupportsLuminanceAlphaTextures;
3024}
3025
daniel@transgaming.com83921382011-01-08 05:46:00 +00003026bool Context::supports32bitIndices() const
3027{
3028 return mSupports32bitIndices;
3029}
3030
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003031void Context::detachBuffer(GLuint buffer)
3032{
3033 // [OpenGL ES 2.0.24] section 2.9 page 22:
3034 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3035 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3036
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003037 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003038 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003039 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003040 }
3041
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003042 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003043 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003044 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003045 }
3046
3047 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3048 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003049 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003050 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003051 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003052 }
3053 }
3054}
3055
3056void Context::detachTexture(GLuint texture)
3057{
3058 // [OpenGL ES 2.0.24] section 3.8 page 84:
3059 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3060 // rebound to texture object zero
3061
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003062 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003063 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003064 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003065 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003066 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003067 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003068 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003069 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003070 }
3071 }
3072
3073 // [OpenGL ES 2.0.24] section 4.4 page 112:
3074 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3075 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3076 // image was attached in the currently bound framebuffer.
3077
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003078 Framebuffer *readFramebuffer = getReadFramebuffer();
3079 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003080
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003081 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003082 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003083 readFramebuffer->detachTexture(texture);
3084 }
3085
3086 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3087 {
3088 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003089 }
3090}
3091
3092void Context::detachFramebuffer(GLuint framebuffer)
3093{
3094 // [OpenGL ES 2.0.24] section 4.4 page 107:
3095 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3096 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3097
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003098 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003099 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003100 bindReadFramebuffer(0);
3101 }
3102
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003103 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003104 {
3105 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003106 }
3107}
3108
3109void Context::detachRenderbuffer(GLuint renderbuffer)
3110{
3111 // [OpenGL ES 2.0.24] section 4.4 page 109:
3112 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3113 // had been executed with the target RENDERBUFFER and name of zero.
3114
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003115 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003116 {
3117 bindRenderbuffer(0);
3118 }
3119
3120 // [OpenGL ES 2.0.24] section 4.4 page 111:
3121 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3122 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3123 // point to which this image was attached in the currently bound framebuffer.
3124
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003125 Framebuffer *readFramebuffer = getReadFramebuffer();
3126 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003127
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003128 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003129 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003130 readFramebuffer->detachRenderbuffer(renderbuffer);
3131 }
3132
3133 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3134 {
3135 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003136 }
3137}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003138
3139Texture *Context::getIncompleteTexture(SamplerType type)
3140{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003141 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003142
3143 if (t == NULL)
3144 {
3145 static const GLubyte color[] = { 0, 0, 0, 255 };
3146
3147 switch (type)
3148 {
3149 default:
3150 UNREACHABLE();
3151 // default falls through to SAMPLER_2D
3152
3153 case SAMPLER_2D:
3154 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003155 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003156 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003157 t = incomplete2d;
3158 }
3159 break;
3160
3161 case SAMPLER_CUBE:
3162 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003163 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003164
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003165 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3166 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3167 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3168 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3169 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3170 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003171
3172 t = incompleteCube;
3173 }
3174 break;
3175 }
3176
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003177 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003178 }
3179
3180 return t;
3181}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003182
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003183bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003184{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003185 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003186}
3187
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003188bool Context::isTriangleMode(GLenum drawMode)
3189{
3190 switch (drawMode)
3191 {
3192 case GL_TRIANGLES:
3193 case GL_TRIANGLE_FAN:
3194 case GL_TRIANGLE_STRIP:
3195 return true;
3196 case GL_POINTS:
3197 case GL_LINES:
3198 case GL_LINE_LOOP:
3199 case GL_LINE_STRIP:
3200 return false;
3201 default: UNREACHABLE();
3202 }
3203
3204 return false;
3205}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003206
3207void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3208{
3209 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3210
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003211 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3212 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3213 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3214 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003215
daniel@transgaming.com83921382011-01-08 05:46:00 +00003216 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003217}
3218
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003219void Context::initExtensionString()
3220{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003221 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003222 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3223 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003224 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003225 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003226 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003227
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003228 if (supportsEventQueries())
3229 {
3230 mExtensionString += "GL_NV_fence ";
3231 }
3232
daniel@transgaming.com01868132010-08-24 19:21:17 +00003233 if (supportsCompressedTextures())
3234 {
3235 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3236 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003237
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003238 if (supportsFloatTextures())
3239 {
3240 mExtensionString += "GL_OES_texture_float ";
3241 }
3242
3243 if (supportsHalfFloatTextures())
3244 {
3245 mExtensionString += "GL_OES_texture_half_float ";
3246 }
3247
3248 if (supportsFloatLinearFilter())
3249 {
3250 mExtensionString += "GL_OES_texture_float_linear ";
3251 }
3252
3253 if (supportsHalfFloatLinearFilter())
3254 {
3255 mExtensionString += "GL_OES_texture_half_float_linear ";
3256 }
3257
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003258 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003259 {
3260 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3261 }
3262
daniel@transgaming.com83921382011-01-08 05:46:00 +00003263 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003264 {
3265 mExtensionString += "GL_OES_element_index_uint ";
3266 }
3267
3268 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3269 if (end != std::string::npos)
3270 {
3271 mExtensionString.resize(end+1);
3272 }
3273}
3274
3275const char *Context::getExtensionString() const
3276{
3277 return mExtensionString.c_str();
3278}
3279
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003280void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3281 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3282 GLbitfield mask)
3283{
3284 IDirect3DDevice9 *device = getDevice();
3285
3286 Framebuffer *readFramebuffer = getReadFramebuffer();
3287 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3288
3289 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3290 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3291 {
3292 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3293 }
3294
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003295 if (drawFramebuffer->getSamples() != 0)
3296 {
3297 return error(GL_INVALID_OPERATION);
3298 }
3299
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003300 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3301 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3302 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3303 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3304
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003305 RECT sourceRect;
3306 RECT destRect;
3307
3308 if (srcX0 < srcX1)
3309 {
3310 sourceRect.left = srcX0;
3311 sourceRect.right = srcX1;
3312 destRect.left = dstX0;
3313 destRect.right = dstX1;
3314 }
3315 else
3316 {
3317 sourceRect.left = srcX1;
3318 destRect.left = dstX1;
3319 sourceRect.right = srcX0;
3320 destRect.right = dstX0;
3321 }
3322
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003323 if (srcY0 < srcY1)
3324 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003325 sourceRect.top = readBufferHeight - srcY1;
3326 destRect.top = drawBufferHeight - dstY1;
3327 sourceRect.bottom = readBufferHeight - srcY0;
3328 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003329 }
3330 else
3331 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003332 sourceRect.top = readBufferHeight - srcY0;
3333 destRect.top = drawBufferHeight - dstY0;
3334 sourceRect.bottom = readBufferHeight - srcY1;
3335 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003336 }
3337
3338 RECT sourceScissoredRect = sourceRect;
3339 RECT destScissoredRect = destRect;
3340
3341 if (mState.scissorTest)
3342 {
3343 // Only write to parts of the destination framebuffer which pass the scissor test
3344 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3345 // rect will be checked against scissorY, rather than the bottom.
3346 if (destRect.left < mState.scissorX)
3347 {
3348 int xDiff = mState.scissorX - destRect.left;
3349 destScissoredRect.left = mState.scissorX;
3350 sourceScissoredRect.left += xDiff;
3351 }
3352
3353 if (destRect.right > mState.scissorX + mState.scissorWidth)
3354 {
3355 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3356 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3357 sourceScissoredRect.right -= xDiff;
3358 }
3359
3360 if (destRect.top < mState.scissorY)
3361 {
3362 int yDiff = mState.scissorY - destRect.top;
3363 destScissoredRect.top = mState.scissorY;
3364 sourceScissoredRect.top += yDiff;
3365 }
3366
3367 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3368 {
3369 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3370 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3371 sourceScissoredRect.bottom -= yDiff;
3372 }
3373 }
3374
3375 bool blitRenderTarget = false;
3376 bool blitDepthStencil = false;
3377
3378 RECT sourceTrimmedRect = sourceScissoredRect;
3379 RECT destTrimmedRect = destScissoredRect;
3380
3381 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3382 // the actual draw and read surfaces.
3383 if (sourceTrimmedRect.left < 0)
3384 {
3385 int xDiff = 0 - sourceTrimmedRect.left;
3386 sourceTrimmedRect.left = 0;
3387 destTrimmedRect.left += xDiff;
3388 }
3389
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003390 if (sourceTrimmedRect.right > readBufferWidth)
3391 {
3392 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3393 sourceTrimmedRect.right = readBufferWidth;
3394 destTrimmedRect.right -= xDiff;
3395 }
3396
3397 if (sourceTrimmedRect.top < 0)
3398 {
3399 int yDiff = 0 - sourceTrimmedRect.top;
3400 sourceTrimmedRect.top = 0;
3401 destTrimmedRect.top += yDiff;
3402 }
3403
3404 if (sourceTrimmedRect.bottom > readBufferHeight)
3405 {
3406 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3407 sourceTrimmedRect.bottom = readBufferHeight;
3408 destTrimmedRect.bottom -= yDiff;
3409 }
3410
3411 if (destTrimmedRect.left < 0)
3412 {
3413 int xDiff = 0 - destTrimmedRect.left;
3414 destTrimmedRect.left = 0;
3415 sourceTrimmedRect.left += xDiff;
3416 }
3417
3418 if (destTrimmedRect.right > drawBufferWidth)
3419 {
3420 int xDiff = destTrimmedRect.right - drawBufferWidth;
3421 destTrimmedRect.right = drawBufferWidth;
3422 sourceTrimmedRect.right -= xDiff;
3423 }
3424
3425 if (destTrimmedRect.top < 0)
3426 {
3427 int yDiff = 0 - destTrimmedRect.top;
3428 destTrimmedRect.top = 0;
3429 sourceTrimmedRect.top += yDiff;
3430 }
3431
3432 if (destTrimmedRect.bottom > drawBufferHeight)
3433 {
3434 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3435 destTrimmedRect.bottom = drawBufferHeight;
3436 sourceTrimmedRect.bottom -= yDiff;
3437 }
3438
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003439 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003440 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3441 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3442 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3443 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003444 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3445 {
3446 partialBufferCopy = true;
3447 }
3448
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003449 if (mask & GL_COLOR_BUFFER_BIT)
3450 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003451 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3452 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3453 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3454 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3455 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003456 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3457 {
3458 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3459 return error(GL_INVALID_OPERATION);
3460 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003461
3462 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3463 {
3464 return error(GL_INVALID_OPERATION);
3465 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003466
3467 blitRenderTarget = true;
3468
3469 }
3470
3471 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3472 {
3473 DepthStencilbuffer *readDSBuffer = NULL;
3474 DepthStencilbuffer *drawDSBuffer = NULL;
3475
3476 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3477 // both a depth and stencil buffer, it will be the same buffer.
3478
3479 if (mask & GL_DEPTH_BUFFER_BIT)
3480 {
3481 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3482 {
3483 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3484 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3485 {
3486 return error(GL_INVALID_OPERATION);
3487 }
3488
3489 blitDepthStencil = true;
3490 readDSBuffer = readFramebuffer->getDepthbuffer();
3491 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3492 }
3493 }
3494
3495 if (mask & GL_STENCIL_BUFFER_BIT)
3496 {
3497 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3498 {
3499 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3500 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3501 {
3502 return error(GL_INVALID_OPERATION);
3503 }
3504
3505 blitDepthStencil = true;
3506 readDSBuffer = readFramebuffer->getStencilbuffer();
3507 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3508 }
3509 }
3510
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003511 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003512 {
3513 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3514 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3515 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003516
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003517 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3518 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003519 {
3520 return error(GL_INVALID_OPERATION);
3521 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003522 }
3523
3524 if (blitRenderTarget || blitDepthStencil)
3525 {
3526 egl::Display *display = getDisplay();
3527 display->endScene();
3528
3529 if (blitRenderTarget)
3530 {
3531 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3532 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3533
3534 if (FAILED(result))
3535 {
3536 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3537 return;
3538 }
3539 }
3540
3541 if (blitDepthStencil)
3542 {
3543 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3544
3545 if (FAILED(result))
3546 {
3547 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3548 return;
3549 }
3550 }
3551 }
3552}
3553
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003554VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3555{
3556 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3557 {
3558 mVertexDeclCache[i].vertexDeclaration = NULL;
3559 mVertexDeclCache[i].lruCount = 0;
3560 }
3561}
3562
3563VertexDeclarationCache::~VertexDeclarationCache()
3564{
3565 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3566 {
3567 if (mVertexDeclCache[i].vertexDeclaration)
3568 {
3569 mVertexDeclCache[i].vertexDeclaration->Release();
3570 }
3571 }
3572}
3573
3574GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program)
3575{
3576 IDirect3DDevice9 *device = getDevice();
3577
3578 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3579 D3DVERTEXELEMENT9 *element = &elements[0];
3580
3581 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3582 {
3583 if (attributes[i].active)
3584 {
3585 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3586
3587 element->Stream = i;
3588 element->Offset = 0;
3589 element->Type = attributes[i].type;
3590 element->Method = D3DDECLMETHOD_DEFAULT;
3591 element->Usage = D3DDECLUSAGE_TEXCOORD;
3592 element->UsageIndex = program->getSemanticIndex(i);
3593 element++;
3594 }
3595 }
3596
3597 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3598 *(element++) = end;
3599
3600 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3601 {
3602 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3603 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3604 {
3605 entry->lruCount = ++mMaxLru;
3606 device->SetVertexDeclaration(entry->vertexDeclaration);
3607
3608 return GL_NO_ERROR;
3609 }
3610 }
3611
3612 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3613
3614 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3615 {
3616 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3617 {
3618 lastCache = &mVertexDeclCache[i];
3619 }
3620 }
3621
3622 if (lastCache->vertexDeclaration != NULL)
3623 {
3624 lastCache->vertexDeclaration->Release();
3625 lastCache->vertexDeclaration = NULL;
3626 }
3627
3628 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3629 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3630 device->SetVertexDeclaration(lastCache->vertexDeclaration);
3631 lastCache->lruCount = ++mMaxLru;
3632
3633 return GL_NO_ERROR;
3634}
3635
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003636}
3637
3638extern "C"
3639{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003640gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003641{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003642 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003643}
3644
3645void glDestroyContext(gl::Context *context)
3646{
3647 delete context;
3648
3649 if (context == gl::getContext())
3650 {
3651 gl::makeCurrent(NULL, NULL, NULL);
3652 }
3653}
3654
3655void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3656{
3657 gl::makeCurrent(context, display, surface);
3658}
3659
3660gl::Context *glGetCurrentContext()
3661{
3662 return gl::getContext();
3663}
3664}