blob: c5c8ecae307eec5415a282933ae99dc12c169610 [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"
28#include "libGLESv2/geometry/backend.h"
29#include "libGLESv2/geometry/VertexDataManager.h"
30#include "libGLESv2/geometry/IndexDataManager.h"
31#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.com86487c22010-03-11 19:41:43 +000033#undef near
34#undef far
35
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036namespace gl
37{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000038Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040{
41 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000042
daniel@transgaming.com428d1582010-05-04 03:35:25 +000043 mState.depthClearValue = 1.0f;
44 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045
daniel@transgaming.com428d1582010-05-04 03:35:25 +000046 mState.cullFace = false;
47 mState.cullMode = GL_BACK;
48 mState.frontFace = GL_CCW;
49 mState.depthTest = false;
50 mState.depthFunc = GL_LESS;
51 mState.blend = false;
52 mState.sourceBlendRGB = GL_ONE;
53 mState.sourceBlendAlpha = GL_ONE;
54 mState.destBlendRGB = GL_ZERO;
55 mState.destBlendAlpha = GL_ZERO;
56 mState.blendEquationRGB = GL_FUNC_ADD;
57 mState.blendEquationAlpha = GL_FUNC_ADD;
58 mState.blendColor.red = 0;
59 mState.blendColor.green = 0;
60 mState.blendColor.blue = 0;
61 mState.blendColor.alpha = 0;
62 mState.stencilTest = false;
63 mState.stencilFunc = GL_ALWAYS;
64 mState.stencilRef = 0;
65 mState.stencilMask = -1;
66 mState.stencilWritemask = -1;
67 mState.stencilBackFunc = GL_ALWAYS;
68 mState.stencilBackRef = 0;
69 mState.stencilBackMask = - 1;
70 mState.stencilBackWritemask = -1;
71 mState.stencilFail = GL_KEEP;
72 mState.stencilPassDepthFail = GL_KEEP;
73 mState.stencilPassDepthPass = GL_KEEP;
74 mState.stencilBackFail = GL_KEEP;
75 mState.stencilBackPassDepthFail = GL_KEEP;
76 mState.stencilBackPassDepthPass = GL_KEEP;
77 mState.polygonOffsetFill = false;
78 mState.polygonOffsetFactor = 0.0f;
79 mState.polygonOffsetUnits = 0.0f;
80 mState.sampleAlphaToCoverage = false;
81 mState.sampleCoverage = false;
82 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000083 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000084 mState.scissorTest = false;
85 mState.dither = true;
86 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000087 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000090
daniel@transgaming.com428d1582010-05-04 03:35:25 +000091 mState.viewportX = 0;
92 mState.viewportY = 0;
93 mState.viewportWidth = config->mDisplayMode.Width;
94 mState.viewportHeight = config->mDisplayMode.Height;
95 mState.zNear = 0.0f;
96 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097
daniel@transgaming.com428d1582010-05-04 03:35:25 +000098 mState.scissorX = 0;
99 mState.scissorY = 0;
100 mState.scissorWidth = config->mDisplayMode.Width;
101 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000103 mState.colorMaskRed = true;
104 mState.colorMaskGreen = true;
105 mState.colorMaskBlue = true;
106 mState.colorMaskAlpha = true;
107 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000109 if (shareContext != NULL)
110 {
111 mResourceManager = shareContext->mResourceManager;
112 mResourceManager->addRef();
113 }
114 else
115 {
116 mResourceManager = new ResourceManager();
117 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 // [OpenGL ES 2.0.24] section 3.7 page 83:
120 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
121 // and cube map texture state vectors respectively associated with them.
122 // In order that access to these initial textures not be lost, they are treated as texture
123 // objects all of whose names are 0.
124
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000125 mTexture2DZero.set(new Texture2D(0));
126 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000128 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000129 bindArrayBuffer(0);
130 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 bindTextureCubeMap(0);
132 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000133 bindReadFramebuffer(0);
134 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindRenderbuffer(0);
136
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000137 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000139 mState.packAlignment = 4;
140 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000141
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000142 mBufferBackEnd = NULL;
143 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000144 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000145 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147 mInvalidEnum = false;
148 mInvalidValue = false;
149 mInvalidOperation = false;
150 mOutOfMemory = false;
151 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000152
153 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000154
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000155 mSupportsCompressedTextures = false;
156 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000157 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000158 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000159 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
162Context::~Context()
163{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000164 if (mState.currentProgram != 0)
165 {
166 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
167 if (programObject)
168 {
169 programObject->release();
170 }
171 mState.currentProgram = 0;
172 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000174 while (!mFramebufferMap.empty())
175 {
176 deleteFramebuffer(mFramebufferMap.begin()->first);
177 }
178
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000179 while (!mFenceMap.empty())
180 {
181 deleteFence(mFenceMap.begin()->first);
182 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000183
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000184 while (!mMultiSampleSupport.empty())
185 {
186 delete [] mMultiSampleSupport.begin()->second;
187 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
188 }
189
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000190 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
191 {
192 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
193 {
194 mState.samplerTexture[type][sampler].set(NULL);
195 }
196 }
197
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000198 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
199 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000200 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000201 }
202
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000203 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
204 {
205 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
206 }
207
208 mState.arrayBuffer.set(NULL);
209 mState.elementArrayBuffer.set(NULL);
210 mState.texture2D.set(NULL);
211 mState.textureCubeMap.set(NULL);
212 mState.renderbuffer.set(NULL);
213
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000214 mTexture2DZero.set(NULL);
215 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000217 delete mBufferBackEnd;
218 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000219 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000220 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000221
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000222 if (mMaskedClearSavedState)
223 {
224 mMaskedClearSavedState->Release();
225 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000226
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000227 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228}
229
230void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
231{
232 IDirect3DDevice9 *device = display->getDevice();
233
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000234 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000235 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000236 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000237
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000238 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000239 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000240 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000241 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000242
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000243 const D3DFORMAT renderBufferFormats[] =
244 {
245 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000246 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000247 D3DFMT_R5G6B5,
248 D3DFMT_D24S8
249 };
250
251 int max = 0;
252 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
253 {
254 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
255 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
256 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
257
258 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
259 {
260 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
261 {
262 max = j;
263 }
264 }
265 }
266
267 mMaxSupportedSamples = max;
268
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000269 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000270 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000271 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
272 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.com01868132010-08-24 19:21:17 +0000273
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000274 initExtensionString();
275
276 mState.viewportX = 0;
277 mState.viewportY = 0;
278 mState.viewportWidth = surface->getWidth();
279 mState.viewportHeight = surface->getHeight();
280
281 mState.scissorX = 0;
282 mState.scissorY = 0;
283 mState.scissorWidth = surface->getWidth();
284 mState.scissorHeight = surface->getHeight();
285
286 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000287 }
288
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
290 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000291 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000294 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000295 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
297 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000299 if (defaultRenderTarget)
300 {
301 defaultRenderTarget->Release();
302 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000304 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000306 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000308
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000309 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000310
311 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312}
313
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000314// 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 +0000315void Context::markAllStateDirty()
316{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000317 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000318 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000319 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000320 mDepthStencilInitialized = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000321 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000322
323 mClearStateDirty = true;
324 mCullStateDirty = true;
325 mDepthStateDirty = true;
326 mMaskStateDirty = true;
327 mBlendStateDirty = true;
328 mStencilStateDirty = true;
329 mPolygonOffsetStateDirty = true;
330 mScissorStateDirty = true;
331 mSampleStateDirty = true;
332 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000333 mFrontFaceDirty = true;
334
335 if (mBufferBackEnd != NULL)
336 {
337 mBufferBackEnd->invalidate();
338 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000339}
340
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341void Context::setClearColor(float red, float green, float blue, float alpha)
342{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000343 mState.colorClearValue.red = red;
344 mState.colorClearValue.green = green;
345 mState.colorClearValue.blue = blue;
346 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347}
348
349void Context::setClearDepth(float depth)
350{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000351 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352}
353
354void Context::setClearStencil(int stencil)
355{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000356 mState.stencilClearValue = stencil;
357}
358
359void Context::setCullFace(bool enabled)
360{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000361 if (mState.cullFace != enabled)
362 {
363 mState.cullFace = enabled;
364 mCullStateDirty = true;
365 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000366}
367
368bool Context::isCullFaceEnabled() const
369{
370 return mState.cullFace;
371}
372
373void Context::setCullMode(GLenum mode)
374{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000375 if (mState.cullMode != mode)
376 {
377 mState.cullMode = mode;
378 mCullStateDirty = true;
379 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000380}
381
382void Context::setFrontFace(GLenum front)
383{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000384 if (mState.frontFace != front)
385 {
386 mState.frontFace = front;
387 mFrontFaceDirty = true;
388 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000389}
390
391void Context::setDepthTest(bool enabled)
392{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000393 if (mState.depthTest != enabled)
394 {
395 mState.depthTest = enabled;
396 mDepthStateDirty = true;
397 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000398}
399
400bool Context::isDepthTestEnabled() const
401{
402 return mState.depthTest;
403}
404
405void Context::setDepthFunc(GLenum depthFunc)
406{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000407 if (mState.depthFunc != depthFunc)
408 {
409 mState.depthFunc = depthFunc;
410 mDepthStateDirty = true;
411 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000412}
413
414void Context::setDepthRange(float zNear, float zFar)
415{
416 mState.zNear = zNear;
417 mState.zFar = zFar;
418}
419
420void Context::setBlend(bool enabled)
421{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000422 if (mState.blend != enabled)
423 {
424 mState.blend = enabled;
425 mBlendStateDirty = true;
426 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000427}
428
429bool Context::isBlendEnabled() const
430{
431 return mState.blend;
432}
433
434void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
435{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000436 if (mState.sourceBlendRGB != sourceRGB ||
437 mState.sourceBlendAlpha != sourceAlpha ||
438 mState.destBlendRGB != destRGB ||
439 mState.destBlendAlpha != destAlpha)
440 {
441 mState.sourceBlendRGB = sourceRGB;
442 mState.destBlendRGB = destRGB;
443 mState.sourceBlendAlpha = sourceAlpha;
444 mState.destBlendAlpha = destAlpha;
445 mBlendStateDirty = true;
446 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000447}
448
449void Context::setBlendColor(float red, float green, float blue, float alpha)
450{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000451 if (mState.blendColor.red != red ||
452 mState.blendColor.green != green ||
453 mState.blendColor.blue != blue ||
454 mState.blendColor.alpha != alpha)
455 {
456 mState.blendColor.red = red;
457 mState.blendColor.green = green;
458 mState.blendColor.blue = blue;
459 mState.blendColor.alpha = alpha;
460 mBlendStateDirty = true;
461 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000462}
463
464void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
465{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000466 if (mState.blendEquationRGB != rgbEquation ||
467 mState.blendEquationAlpha != alphaEquation)
468 {
469 mState.blendEquationRGB = rgbEquation;
470 mState.blendEquationAlpha = alphaEquation;
471 mBlendStateDirty = true;
472 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000473}
474
475void Context::setStencilTest(bool enabled)
476{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000477 if (mState.stencilTest != enabled)
478 {
479 mState.stencilTest = enabled;
480 mStencilStateDirty = true;
481 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000482}
483
484bool Context::isStencilTestEnabled() const
485{
486 return mState.stencilTest;
487}
488
489void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.stencilFunc != stencilFunc ||
492 mState.stencilRef != stencilRef ||
493 mState.stencilMask != stencilMask)
494 {
495 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000496 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 mState.stencilMask = stencilMask;
498 mStencilStateDirty = true;
499 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000500}
501
502void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
503{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000504 if (mState.stencilBackFunc != stencilBackFunc ||
505 mState.stencilBackRef != stencilBackRef ||
506 mState.stencilBackMask != stencilBackMask)
507 {
508 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000509 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 mState.stencilBackMask = stencilBackMask;
511 mStencilStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515void Context::setStencilWritemask(GLuint stencilWritemask)
516{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 if (mState.stencilWritemask != stencilWritemask)
518 {
519 mState.stencilWritemask = stencilWritemask;
520 mStencilStateDirty = true;
521 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000522}
523
524void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
525{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000526 if (mState.stencilBackWritemask != stencilBackWritemask)
527 {
528 mState.stencilBackWritemask = stencilBackWritemask;
529 mStencilStateDirty = true;
530 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000531}
532
533void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
534{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000535 if (mState.stencilFail != stencilFail ||
536 mState.stencilPassDepthFail != stencilPassDepthFail ||
537 mState.stencilPassDepthPass != stencilPassDepthPass)
538 {
539 mState.stencilFail = stencilFail;
540 mState.stencilPassDepthFail = stencilPassDepthFail;
541 mState.stencilPassDepthPass = stencilPassDepthPass;
542 mStencilStateDirty = true;
543 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000544}
545
546void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
547{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000548 if (mState.stencilBackFail != stencilBackFail ||
549 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
550 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
551 {
552 mState.stencilBackFail = stencilBackFail;
553 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
554 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
555 mStencilStateDirty = true;
556 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000557}
558
559void Context::setPolygonOffsetFill(bool enabled)
560{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000561 if (mState.polygonOffsetFill != enabled)
562 {
563 mState.polygonOffsetFill = enabled;
564 mPolygonOffsetStateDirty = true;
565 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000566}
567
568bool Context::isPolygonOffsetFillEnabled() const
569{
570 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000571
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000572}
573
574void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
575{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576 if (mState.polygonOffsetFactor != factor ||
577 mState.polygonOffsetUnits != units)
578 {
579 mState.polygonOffsetFactor = factor;
580 mState.polygonOffsetUnits = units;
581 mPolygonOffsetStateDirty = true;
582 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000583}
584
585void Context::setSampleAlphaToCoverage(bool enabled)
586{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000587 if (mState.sampleAlphaToCoverage != enabled)
588 {
589 mState.sampleAlphaToCoverage = enabled;
590 mSampleStateDirty = true;
591 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000592}
593
594bool Context::isSampleAlphaToCoverageEnabled() const
595{
596 return mState.sampleAlphaToCoverage;
597}
598
599void Context::setSampleCoverage(bool enabled)
600{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000601 if (mState.sampleCoverage != enabled)
602 {
603 mState.sampleCoverage = enabled;
604 mSampleStateDirty = true;
605 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000606}
607
608bool Context::isSampleCoverageEnabled() const
609{
610 return mState.sampleCoverage;
611}
612
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000613void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000614{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000615 if (mState.sampleCoverageValue != value ||
616 mState.sampleCoverageInvert != invert)
617 {
618 mState.sampleCoverageValue = value;
619 mState.sampleCoverageInvert = invert;
620 mSampleStateDirty = true;
621 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622}
623
624void Context::setScissorTest(bool enabled)
625{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000626 if (mState.scissorTest != enabled)
627 {
628 mState.scissorTest = enabled;
629 mScissorStateDirty = true;
630 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000631}
632
633bool Context::isScissorTestEnabled() const
634{
635 return mState.scissorTest;
636}
637
638void Context::setDither(bool enabled)
639{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000640 if (mState.dither != enabled)
641 {
642 mState.dither = enabled;
643 mDitherStateDirty = true;
644 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000645}
646
647bool Context::isDitherEnabled() const
648{
649 return mState.dither;
650}
651
652void Context::setLineWidth(GLfloat width)
653{
654 mState.lineWidth = width;
655}
656
657void Context::setGenerateMipmapHint(GLenum hint)
658{
659 mState.generateMipmapHint = hint;
660}
661
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000662void Context::setFragmentShaderDerivativeHint(GLenum hint)
663{
664 mState.fragmentShaderDerivativeHint = hint;
665 // TODO: Propagate the hint to shader translator so we can write
666 // ddx, ddx_coarse, or ddx_fine depending on the hint.
667 // Ignore for now. It is valid for implementations to ignore hint.
668}
669
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000670void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
671{
672 mState.viewportX = x;
673 mState.viewportY = y;
674 mState.viewportWidth = width;
675 mState.viewportHeight = height;
676}
677
678void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
679{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000680 if (mState.scissorX != x || mState.scissorY != y ||
681 mState.scissorWidth != width || mState.scissorHeight != height)
682 {
683 mState.scissorX = x;
684 mState.scissorY = y;
685 mState.scissorWidth = width;
686 mState.scissorHeight = height;
687 mScissorStateDirty = true;
688 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000689}
690
691void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
692{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000693 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
694 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
695 {
696 mState.colorMaskRed = red;
697 mState.colorMaskGreen = green;
698 mState.colorMaskBlue = blue;
699 mState.colorMaskAlpha = alpha;
700 mMaskStateDirty = true;
701 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000702}
703
704void Context::setDepthMask(bool mask)
705{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000706 if (mState.depthMask != mask)
707 {
708 mState.depthMask = mask;
709 mMaskStateDirty = true;
710 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000711}
712
713void Context::setActiveSampler(int active)
714{
715 mState.activeSampler = active;
716}
717
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000718GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000719{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000720 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000721}
722
723GLuint Context::getDrawFramebufferHandle() const
724{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000725 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000726}
727
728GLuint Context::getRenderbufferHandle() const
729{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000730 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000731}
732
733GLuint Context::getArrayBufferHandle() const
734{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000735 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000736}
737
738void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
739{
740 mState.vertexAttribute[attribNum].mEnabled = enabled;
741}
742
743const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
744{
745 return mState.vertexAttribute[attribNum];
746}
747
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000748void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749 GLsizei stride, const void *pointer)
750{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000751 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000752 mState.vertexAttribute[attribNum].mSize = size;
753 mState.vertexAttribute[attribNum].mType = type;
754 mState.vertexAttribute[attribNum].mNormalized = normalized;
755 mState.vertexAttribute[attribNum].mStride = stride;
756 mState.vertexAttribute[attribNum].mPointer = pointer;
757}
758
759const void *Context::getVertexAttribPointer(unsigned int attribNum) const
760{
761 return mState.vertexAttribute[attribNum].mPointer;
762}
763
764// returns entire set of attributes as a block
765const AttributeState *Context::getVertexAttribBlock()
766{
767 return mState.vertexAttribute;
768}
769
770void Context::setPackAlignment(GLint alignment)
771{
772 mState.packAlignment = alignment;
773}
774
775GLint Context::getPackAlignment() const
776{
777 return mState.packAlignment;
778}
779
780void Context::setUnpackAlignment(GLint alignment)
781{
782 mState.unpackAlignment = alignment;
783}
784
785GLint Context::getUnpackAlignment() const
786{
787 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788}
789
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790GLuint Context::createBuffer()
791{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000792 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793}
794
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000795GLuint Context::createProgram()
796{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000797 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798}
799
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000800GLuint Context::createShader(GLenum type)
801{
802 return mResourceManager->createShader(type);
803}
804
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805GLuint Context::createTexture()
806{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000807 return mResourceManager->createTexture();
808}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000810GLuint Context::createRenderbuffer()
811{
812 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813}
814
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000815// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816GLuint Context::createFramebuffer()
817{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000818 unsigned int handle = 1;
819
820 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
821 {
822 handle++;
823 }
824
825 mFramebufferMap[handle] = NULL;
826
827 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828}
829
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000830GLuint Context::createFence()
831{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000832 unsigned int handle = 0;
833
834 while (mFenceMap.find(handle) != mFenceMap.end())
835 {
836 handle++;
837 }
838
839 mFenceMap[handle] = new Fence;
840
841 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000842}
843
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844void Context::deleteBuffer(GLuint buffer)
845{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000846 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847 {
848 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000850
851 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852}
853
854void Context::deleteShader(GLuint shader)
855{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000856 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
859void Context::deleteProgram(GLuint program)
860{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000861 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
864void Context::deleteTexture(GLuint texture)
865{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000866 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867 {
868 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000870
871 mResourceManager->deleteTexture(texture);
872}
873
874void Context::deleteRenderbuffer(GLuint renderbuffer)
875{
876 if (mResourceManager->getRenderbuffer(renderbuffer))
877 {
878 detachRenderbuffer(renderbuffer);
879 }
880
881 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882}
883
884void Context::deleteFramebuffer(GLuint framebuffer)
885{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000886 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
887
888 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889 {
890 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000891
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000892 delete framebufferObject->second;
893 mFramebufferMap.erase(framebufferObject);
894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000896
897void Context::deleteFence(GLuint fence)
898{
899 FenceMap::iterator fenceObject = mFenceMap.find(fence);
900
901 if (fenceObject != mFenceMap.end())
902 {
903 delete fenceObject->second;
904 mFenceMap.erase(fenceObject);
905 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000906}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000908Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000910 return mResourceManager->getBuffer(handle);
911}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000913Shader *Context::getShader(GLuint handle)
914{
915 return mResourceManager->getShader(handle);
916}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918Program *Context::getProgram(GLuint handle)
919{
920 return mResourceManager->getProgram(handle);
921}
922
923Texture *Context::getTexture(GLuint handle)
924{
925 return mResourceManager->getTexture(handle);
926}
927
928Renderbuffer *Context::getRenderbuffer(GLuint handle)
929{
930 return mResourceManager->getRenderbuffer(handle);
931}
932
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000933Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000934{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000935 return getFramebuffer(mState.readFramebuffer);
936}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000937
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000938Framebuffer *Context::getDrawFramebuffer()
939{
940 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941}
942
943void Context::bindArrayBuffer(unsigned int buffer)
944{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000945 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000947 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
950void Context::bindElementArrayBuffer(unsigned int buffer)
951{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000952 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000954 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955}
956
957void Context::bindTexture2D(GLuint texture)
958{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000959 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000961 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000963 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964}
965
966void Context::bindTextureCubeMap(GLuint texture)
967{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000968 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000970 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000972 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
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.com9ecb9f92010-07-28 19:21:12 +00001081 if (mState.texture2D.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001083 return mTexture2DZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084 }
1085
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001086 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087}
1088
1089TextureCubeMap *Context::getTextureCubeMap()
1090{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001091 if (mState.textureCubeMap.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001093 return mTextureCubeMapZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094 }
1095
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001096 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097}
1098
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001099Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001101 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001102
1103 if (texid == 0)
1104 {
1105 switch (type)
1106 {
1107 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001108 case SAMPLER_2D: return mTexture2DZero.get();
1109 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001110 }
1111 }
1112
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001113 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114}
1115
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001116bool Context::getBooleanv(GLenum pname, GLboolean *params)
1117{
1118 switch (pname)
1119 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001120 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1121 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1122 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001123 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001124 params[0] = mState.colorMaskRed;
1125 params[1] = mState.colorMaskGreen;
1126 params[2] = mState.colorMaskBlue;
1127 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001128 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001129 case GL_CULL_FACE: *params = mState.cullFace;
1130 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1131 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1132 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1133 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1134 case GL_STENCIL_TEST: *params = mState.stencilTest;
1135 case GL_DEPTH_TEST: *params = mState.depthTest;
1136 case GL_BLEND: *params = mState.blend;
1137 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001138 default:
1139 return false;
1140 }
1141
1142 return true;
1143}
1144
1145bool Context::getFloatv(GLenum pname, GLfloat *params)
1146{
1147 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1148 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1149 // GetIntegerv as its native query function. As it would require conversion in any
1150 // case, this should make no difference to the calling application.
1151 switch (pname)
1152 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001153 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1154 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1155 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1156 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1157 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001158 case GL_ALIASED_LINE_WIDTH_RANGE:
1159 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1160 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1161 break;
1162 case GL_ALIASED_POINT_SIZE_RANGE:
1163 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001164 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 +00001165 break;
1166 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001167 params[0] = mState.zNear;
1168 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001169 break;
1170 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001171 params[0] = mState.colorClearValue.red;
1172 params[1] = mState.colorClearValue.green;
1173 params[2] = mState.colorClearValue.blue;
1174 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001175 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001176 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001177 params[0] = mState.blendColor.red;
1178 params[1] = mState.blendColor.green;
1179 params[2] = mState.blendColor.blue;
1180 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001181 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001182 default:
1183 return false;
1184 }
1185
1186 return true;
1187}
1188
1189bool Context::getIntegerv(GLenum pname, GLint *params)
1190{
1191 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1192 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1193 // GetIntegerv as its native query function. As it would require conversion in any
1194 // case, this should make no difference to the calling application. You may find it in
1195 // Context::getFloatv.
1196 switch (pname)
1197 {
1198 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1199 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1200 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1201 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1202 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1203 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1204 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1205 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001206 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001207 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001208 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1209 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001210 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001211 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1212 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001213 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001214 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1215 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1216 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1217 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001218 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001219 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1220 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1221 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1222 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1223 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1224 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1225 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1226 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1227 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1228 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1229 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1230 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1231 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1232 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1233 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1234 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1235 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1236 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1237 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1238 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1239 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1240 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1241 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1242 case GL_SUBPIXEL_BITS: *params = 4; break;
1243 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1244 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001245 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1246 {
1247 if (supportsCompressedTextures())
1248 {
1249 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1250 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1251 *params = 2;
1252 }
1253 else
1254 {
1255 *params = 0;
1256 }
1257 }
1258 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001259 case GL_MAX_SAMPLES_ANGLE:
1260 {
1261 GLsizei maxSamples = getMaxSupportedSamples();
1262 if (maxSamples != 0)
1263 {
1264 *params = maxSamples;
1265 }
1266 else
1267 {
1268 return false;
1269 }
1270
1271 break;
1272 }
1273 case GL_SAMPLE_BUFFERS:
1274 case GL_SAMPLES:
1275 {
1276 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1277 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1278 {
1279 switch (pname)
1280 {
1281 case GL_SAMPLE_BUFFERS:
1282 if (framebuffer->getSamples() != 0)
1283 {
1284 *params = 1;
1285 }
1286 else
1287 {
1288 *params = 0;
1289 }
1290 break;
1291 case GL_SAMPLES:
1292 *params = framebuffer->getSamples();
1293 break;
1294 }
1295 }
1296 else
1297 {
1298 *params = 0;
1299 }
1300 }
1301 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001302 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1303 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1304 case GL_MAX_VIEWPORT_DIMS:
1305 {
1306 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1307 params[0] = maxDimension;
1308 params[1] = maxDimension;
1309 }
1310 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001311 case GL_COMPRESSED_TEXTURE_FORMATS:
1312 {
1313 if (supportsCompressedTextures())
1314 {
1315 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1316 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1317 }
1318 }
1319 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001320 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001321 params[0] = mState.viewportX;
1322 params[1] = mState.viewportY;
1323 params[2] = mState.viewportWidth;
1324 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001325 break;
1326 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001327 params[0] = mState.scissorX;
1328 params[1] = mState.scissorY;
1329 params[2] = mState.scissorWidth;
1330 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001331 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001332 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1333 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001334 case GL_RED_BITS:
1335 case GL_GREEN_BITS:
1336 case GL_BLUE_BITS:
1337 case GL_ALPHA_BITS:
1338 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001339 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001340 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1341
1342 if (colorbuffer)
1343 {
1344 switch (pname)
1345 {
1346 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1347 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1348 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1349 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1350 }
1351 }
1352 else
1353 {
1354 *params = 0;
1355 }
1356 }
1357 break;
1358 case GL_DEPTH_BITS:
1359 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001360 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001361 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001362
1363 if (depthbuffer)
1364 {
1365 *params = depthbuffer->getDepthSize();
1366 }
1367 else
1368 {
1369 *params = 0;
1370 }
1371 }
1372 break;
1373 case GL_STENCIL_BITS:
1374 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001375 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001376 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001377
1378 if (stencilbuffer)
1379 {
1380 *params = stencilbuffer->getStencilSize();
1381 }
1382 else
1383 {
1384 *params = 0;
1385 }
1386 }
1387 break;
1388 case GL_TEXTURE_BINDING_2D:
1389 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001390 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 {
1392 error(GL_INVALID_OPERATION);
1393 return false;
1394 }
1395
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001396 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001397 }
1398 break;
1399 case GL_TEXTURE_BINDING_CUBE_MAP:
1400 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001401 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001402 {
1403 error(GL_INVALID_OPERATION);
1404 return false;
1405 }
1406
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001407 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001408 }
1409 break;
1410 default:
1411 return false;
1412 }
1413
1414 return true;
1415}
1416
1417bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1418{
1419 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1420 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1421 // to the fact that it is stored internally as a float, and so would require conversion
1422 // if returned from Context::getIntegerv. Since this conversion is already implemented
1423 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1424 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1425 // application.
1426 switch (pname)
1427 {
1428 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1429 case GL_SHADER_BINARY_FORMATS:
1430 {
1431 *type = GL_INT;
1432 *numParams = 0;
1433 }
1434 break;
1435 case GL_MAX_VERTEX_ATTRIBS:
1436 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1437 case GL_MAX_VARYING_VECTORS:
1438 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1439 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1440 case GL_MAX_TEXTURE_IMAGE_UNITS:
1441 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1442 case GL_MAX_RENDERBUFFER_SIZE:
1443 case GL_NUM_SHADER_BINARY_FORMATS:
1444 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1445 case GL_ARRAY_BUFFER_BINDING:
1446 case GL_FRAMEBUFFER_BINDING:
1447 case GL_RENDERBUFFER_BINDING:
1448 case GL_CURRENT_PROGRAM:
1449 case GL_PACK_ALIGNMENT:
1450 case GL_UNPACK_ALIGNMENT:
1451 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001452 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001453 case GL_RED_BITS:
1454 case GL_GREEN_BITS:
1455 case GL_BLUE_BITS:
1456 case GL_ALPHA_BITS:
1457 case GL_DEPTH_BITS:
1458 case GL_STENCIL_BITS:
1459 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1460 case GL_CULL_FACE_MODE:
1461 case GL_FRONT_FACE:
1462 case GL_ACTIVE_TEXTURE:
1463 case GL_STENCIL_FUNC:
1464 case GL_STENCIL_VALUE_MASK:
1465 case GL_STENCIL_REF:
1466 case GL_STENCIL_FAIL:
1467 case GL_STENCIL_PASS_DEPTH_FAIL:
1468 case GL_STENCIL_PASS_DEPTH_PASS:
1469 case GL_STENCIL_BACK_FUNC:
1470 case GL_STENCIL_BACK_VALUE_MASK:
1471 case GL_STENCIL_BACK_REF:
1472 case GL_STENCIL_BACK_FAIL:
1473 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1474 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1475 case GL_DEPTH_FUNC:
1476 case GL_BLEND_SRC_RGB:
1477 case GL_BLEND_SRC_ALPHA:
1478 case GL_BLEND_DST_RGB:
1479 case GL_BLEND_DST_ALPHA:
1480 case GL_BLEND_EQUATION_RGB:
1481 case GL_BLEND_EQUATION_ALPHA:
1482 case GL_STENCIL_WRITEMASK:
1483 case GL_STENCIL_BACK_WRITEMASK:
1484 case GL_STENCIL_CLEAR_VALUE:
1485 case GL_SUBPIXEL_BITS:
1486 case GL_MAX_TEXTURE_SIZE:
1487 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1488 case GL_SAMPLE_BUFFERS:
1489 case GL_SAMPLES:
1490 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1491 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1492 case GL_TEXTURE_BINDING_2D:
1493 case GL_TEXTURE_BINDING_CUBE_MAP:
1494 {
1495 *type = GL_INT;
1496 *numParams = 1;
1497 }
1498 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001499 case GL_MAX_SAMPLES_ANGLE:
1500 {
1501 if (getMaxSupportedSamples() != 0)
1502 {
1503 *type = GL_INT;
1504 *numParams = 1;
1505 }
1506 else
1507 {
1508 return false;
1509 }
1510 }
1511 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001512 case GL_MAX_VIEWPORT_DIMS:
1513 {
1514 *type = GL_INT;
1515 *numParams = 2;
1516 }
1517 break;
1518 case GL_VIEWPORT:
1519 case GL_SCISSOR_BOX:
1520 {
1521 *type = GL_INT;
1522 *numParams = 4;
1523 }
1524 break;
1525 case GL_SHADER_COMPILER:
1526 case GL_SAMPLE_COVERAGE_INVERT:
1527 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001528 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1529 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1530 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1531 case GL_SAMPLE_COVERAGE:
1532 case GL_SCISSOR_TEST:
1533 case GL_STENCIL_TEST:
1534 case GL_DEPTH_TEST:
1535 case GL_BLEND:
1536 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001537 {
1538 *type = GL_BOOL;
1539 *numParams = 1;
1540 }
1541 break;
1542 case GL_COLOR_WRITEMASK:
1543 {
1544 *type = GL_BOOL;
1545 *numParams = 4;
1546 }
1547 break;
1548 case GL_POLYGON_OFFSET_FACTOR:
1549 case GL_POLYGON_OFFSET_UNITS:
1550 case GL_SAMPLE_COVERAGE_VALUE:
1551 case GL_DEPTH_CLEAR_VALUE:
1552 case GL_LINE_WIDTH:
1553 {
1554 *type = GL_FLOAT;
1555 *numParams = 1;
1556 }
1557 break;
1558 case GL_ALIASED_LINE_WIDTH_RANGE:
1559 case GL_ALIASED_POINT_SIZE_RANGE:
1560 case GL_DEPTH_RANGE:
1561 {
1562 *type = GL_FLOAT;
1563 *numParams = 2;
1564 }
1565 break;
1566 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001567 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001568 {
1569 *type = GL_FLOAT;
1570 *numParams = 4;
1571 }
1572 break;
1573 default:
1574 return false;
1575 }
1576
1577 return true;
1578}
1579
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001580// Applies the render target surface, depth stencil surface, viewport rectangle and
1581// scissor rectangle to the Direct3D 9 device
1582bool Context::applyRenderTarget(bool ignoreViewport)
1583{
1584 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001585
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001586 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587
1588 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1589 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001590 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1591
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592 return false;
1593 }
1594
1595 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001596
1597 if (!renderTarget)
1598 {
1599 return false; // Context must be lost
1600 }
1601
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001602 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001604 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1605 if (renderTargetSerial != mAppliedRenderTargetSerial)
1606 {
1607 device->SetRenderTarget(0, renderTarget);
1608 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001609 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 +00001610 }
1611
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001612 unsigned int depthbufferSerial = 0;
1613 unsigned int stencilbufferSerial = 0;
1614 if (framebufferObject->getDepthbufferType() != GL_NONE)
1615 {
1616 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1617 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1618 }
1619 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1620 {
1621 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1622 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
1639 if (ignoreViewport)
1640 {
1641 viewport.X = 0;
1642 viewport.Y = 0;
1643 viewport.Width = desc.Width;
1644 viewport.Height = desc.Height;
1645 viewport.MinZ = 0.0f;
1646 viewport.MaxZ = 1.0f;
1647 }
1648 else
1649 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001650 viewport.X = std::max(mState.viewportX, 0);
1651 viewport.Y = std::max(mState.viewportY, 0);
1652 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1653 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1654 viewport.MinZ = clamp01(mState.zNear);
1655 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656 }
1657
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001658 if (viewport.Width <= 0 || viewport.Height <= 0)
1659 {
1660 return false; // Nothing to render
1661 }
1662
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001663 device->SetViewport(&viewport);
1664
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001665 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001667 if (mState.scissorTest)
1668 {
1669 RECT rect = {mState.scissorX,
1670 mState.scissorY,
1671 mState.scissorX + mState.scissorWidth,
1672 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001673 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1674 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001675 device->SetScissorRect(&rect);
1676 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1677 }
1678 else
1679 {
1680 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1681 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001682
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001683 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684 }
1685
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001686 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 Program *programObject = getCurrentProgram();
1689
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001690 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001691 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001693
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001694 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1696 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1697 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001698 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1699
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001700 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001701 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001702 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1703
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001704 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001705 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001706
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001707 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001708 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001709
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001710 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001711 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001712 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001713 }
1714
1715 return true;
1716}
1717
1718// 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 +00001719void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001720{
1721 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001722 Program *programObject = getCurrentProgram();
1723
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001724 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001725 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001726 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001728 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001729 GLint alwaysFront = !isTriangleMode(drawMode);
1730 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1731
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001732 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001733
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001734 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001736 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001738 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 }
1740 else
1741 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001742 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 }
1744
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001745 mCullStateDirty = false;
1746 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 if (mDepthStateDirty)
1749 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001750 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001752 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1753 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 }
1755 else
1756 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001759
1760 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761 }
1762
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001766 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1768
1769 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1770 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1771 {
1772 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1773 }
1774 else
1775 {
1776 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1777 unorm<8>(mState.blendColor.alpha),
1778 unorm<8>(mState.blendColor.alpha),
1779 unorm<8>(mState.blendColor.alpha)));
1780 }
1781
1782 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1783 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1784 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1785
1786 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1787 mState.destBlendRGB != mState.destBlendAlpha ||
1788 mState.blendEquationRGB != mState.blendEquationAlpha)
1789 {
1790 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1791
1792 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1793 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1794 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1795
1796 }
1797 else
1798 {
1799 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1800 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001801 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001802 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001803 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001804 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001805 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001806
1807 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808 }
1809
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001810 if (mStencilStateDirty || mFrontFaceDirty)
1811 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001812 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001813 {
1814 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1815 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1816
1817 // FIXME: Unsupported by D3D9
1818 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1819 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1820 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1821 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1822 mState.stencilRef != mState.stencilBackRef ||
1823 mState.stencilMask != mState.stencilBackMask)
1824 {
1825 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1826 return error(GL_INVALID_OPERATION);
1827 }
1828
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001829 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001830 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001831 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1832
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001833 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1834 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1835 es2dx::ConvertComparison(mState.stencilFunc));
1836
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001837 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001838 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1839
1840 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1841 es2dx::ConvertStencilOp(mState.stencilFail));
1842 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1843 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1844 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1845 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1846
1847 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1848 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1849 es2dx::ConvertComparison(mState.stencilBackFunc));
1850
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001851 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001852 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1853
1854 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1855 es2dx::ConvertStencilOp(mState.stencilBackFail));
1856 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1857 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1858 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1859 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1860 }
1861 else
1862 {
1863 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1864 }
1865
1866 mStencilStateDirty = false;
1867 }
1868
1869 if (mMaskStateDirty)
1870 {
1871 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1872 mState.colorMaskBlue, mState.colorMaskAlpha));
1873 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1874
1875 mMaskStateDirty = false;
1876 }
1877
1878 if (mPolygonOffsetStateDirty)
1879 {
1880 if (mState.polygonOffsetFill)
1881 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001882 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001883 if (depthbuffer)
1884 {
1885 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1886 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1887 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1888 }
1889 }
1890 else
1891 {
1892 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1893 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1894 }
1895
1896 mPolygonOffsetStateDirty = false;
1897 }
1898
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001899 if (framebufferObject->isMultisample() && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001901 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001902 {
1903 FIXME("Sample alpha to coverage is unimplemented.");
1904 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001906 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001907 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001908 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1909 unsigned int mask = 0;
1910 if (mState.sampleCoverageValue != 0)
1911 {
1912 float threshold = 0.5f;
1913
1914 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1915 {
1916 mask <<= 1;
1917
1918 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1919 {
1920 threshold += 1.0f;
1921 mask |= 1;
1922 }
1923 }
1924 }
1925
1926 if (mState.sampleCoverageInvert)
1927 {
1928 mask = ~mask;
1929 }
1930
1931 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1932 }
1933 else
1934 {
1935 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001936 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001937
1938 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 }
1940
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001941 if (mDitherStateDirty)
1942 {
1943 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1944
1945 mDitherStateDirty = false;
1946 }
1947
1948 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949}
1950
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001951// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001952void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001954 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001956 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001958 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959 }
1960 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001961}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001963GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001964{
1965 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1966
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001967 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001968 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001969 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001970 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001971 }
1972
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001973 lookupAttributeMapping(translated);
1974
1975 mBufferBackEnd->setupAttributesPreDraw(translated);
1976
1977 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1978 {
1979 if (translated[i].enabled && translated[i].nonArray)
1980 {
1981 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1982 if (err != GL_NO_ERROR)
1983 {
1984 return err;
1985 }
1986
1987 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1988
1989 *useIndexing = true;
1990 return GL_NO_ERROR;
1991 }
1992 }
1993
1994 *useIndexing = false;
1995 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001996}
1997
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001998GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001999{
2000 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
2001
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002002 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002003
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002004 if (err == GL_NO_ERROR)
2005 {
2006 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002007
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002008 mBufferBackEnd->setupAttributesPreDraw(translated);
2009 }
2010
2011 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002012}
2013
2014// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002015GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002016{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002017 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002018
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002019 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002020 {
2021 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2022 }
2023
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002024 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025}
2026
2027// Applies the shaders and shader constants to the Direct3D 9 device
2028void Context::applyShaders()
2029{
2030 IDirect3DDevice9 *device = getDevice();
2031 Program *programObject = getCurrentProgram();
2032 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2033 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2034
2035 device->SetVertexShader(vertexShader);
2036 device->SetPixelShader(pixelShader);
2037
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002038 if (programObject->getSerial() != mAppliedProgram)
2039 {
2040 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002041 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002042 mAppliedProgram = programObject->getSerial();
2043 }
2044
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002045 programObject->applyUniforms();
2046}
2047
2048// Applies the textures and sampler states to the Direct3D 9 device
2049void Context::applyTextures()
2050{
2051 IDirect3DDevice9 *device = getDevice();
2052 Program *programObject = getCurrentProgram();
2053
2054 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2055 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002056 int textureUnit = programObject->getSamplerMapping(sampler);
2057 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002059 SamplerType textureType = programObject->getSamplerType(sampler);
2060
2061 Texture *texture = getSamplerTexture(textureUnit, textureType);
2062
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002063 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002064 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002065 if (texture->isComplete())
2066 {
2067 GLenum wrapS = texture->getWrapS();
2068 GLenum wrapT = texture->getWrapT();
2069 GLenum minFilter = texture->getMinFilter();
2070 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002072 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2073 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002075 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2076 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2077 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2078 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2079 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002081 device->SetTexture(sampler, texture->getTexture());
2082 }
2083 else
2084 {
2085 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2086 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002087 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002088
2089 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002091 else
2092 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002093 if (programObject->isSamplerDirty(sampler))
2094 {
2095 device->SetTexture(sampler, NULL);
2096 programObject->setSamplerDirty(sampler, false);
2097 }
2098 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002099 }
2100}
2101
2102void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2103{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002104 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002105
2106 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2107 {
2108 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2109 }
2110
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002111 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2112 {
2113 return error(GL_INVALID_OPERATION);
2114 }
2115
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002117
2118 if (!renderTarget)
2119 {
2120 return; // Context must be lost, return silently
2121 }
2122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 IDirect3DDevice9 *device = getDevice();
2124
2125 D3DSURFACE_DESC desc;
2126 renderTarget->GetDesc(&desc);
2127
2128 IDirect3DSurface9 *systemSurface;
2129 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2130
2131 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2132 {
2133 return error(GL_OUT_OF_MEMORY);
2134 }
2135
2136 ASSERT(SUCCEEDED(result));
2137
2138 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2139 {
2140 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2141 }
2142
2143 result = device->GetRenderTargetData(renderTarget, systemSurface);
2144
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145 if (FAILED(result))
2146 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147 systemSurface->Release();
2148
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002149 switch (result)
2150 {
2151 case D3DERR_DRIVERINTERNALERROR:
2152 case D3DERR_DEVICELOST:
2153 return error(GL_OUT_OF_MEMORY);
2154 default:
2155 UNREACHABLE();
2156 return; // No sensible error to generate
2157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 }
2159
2160 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002161 RECT rect = {std::max(x, 0),
2162 std::max(y, 0),
2163 std::min(x + width, (int)desc.Width),
2164 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002165
2166 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2167
2168 if (FAILED(result))
2169 {
2170 UNREACHABLE();
2171 systemSurface->Release();
2172
2173 return; // No sensible error to generate
2174 }
2175
2176 unsigned char *source = (unsigned char*)lock.pBits;
2177 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002178 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002180 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002181
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002182 for (int j = 0; j < rect.bottom - rect.top; j++)
2183 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002184 if (desc.Format == D3DFMT_A8R8G8B8 &&
2185 format == GL_BGRA_EXT &&
2186 type == GL_UNSIGNED_BYTE)
2187 {
2188 // Fast path for EXT_read_format_bgra, given
2189 // an RGBA source buffer. Note that buffers with no
2190 // alpha go through the slow path below.
2191 memcpy(dest + j * outputPitch,
2192 source + j * lock.Pitch,
2193 (rect.right - rect.left) * 4);
2194 continue;
2195 }
2196
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197 for (int i = 0; i < rect.right - rect.left; i++)
2198 {
2199 float r;
2200 float g;
2201 float b;
2202 float a;
2203
2204 switch (desc.Format)
2205 {
2206 case D3DFMT_R5G6B5:
2207 {
2208 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2209
2210 a = 1.0f;
2211 b = (rgb & 0x001F) * (1.0f / 0x001F);
2212 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2213 r = (rgb & 0xF800) * (1.0f / 0xF800);
2214 }
2215 break;
2216 case D3DFMT_X1R5G5B5:
2217 {
2218 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2219
2220 a = 1.0f;
2221 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2222 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2223 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2224 }
2225 break;
2226 case D3DFMT_A1R5G5B5:
2227 {
2228 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2229
2230 a = (argb & 0x8000) ? 1.0f : 0.0f;
2231 b = (argb & 0x001F) * (1.0f / 0x001F);
2232 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2233 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2234 }
2235 break;
2236 case D3DFMT_A8R8G8B8:
2237 {
2238 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2239
2240 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2241 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2242 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2243 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2244 }
2245 break;
2246 case D3DFMT_X8R8G8B8:
2247 {
2248 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2249
2250 a = 1.0f;
2251 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2252 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2253 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2254 }
2255 break;
2256 case D3DFMT_A2R10G10B10:
2257 {
2258 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2259
2260 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2261 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2262 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2263 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2264 }
2265 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002266 case D3DFMT_A32B32G32R32F:
2267 {
2268 // float formats in D3D are stored rgba, rather than the other way round
2269 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2270 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2271 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2272 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2273 }
2274 break;
2275 case D3DFMT_A16B16G16R16F:
2276 {
2277 // float formats in D3D are stored rgba, rather than the other way round
2278 float abgr[4];
2279
2280 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2281
2282 a = abgr[3];
2283 b = abgr[2];
2284 g = abgr[1];
2285 r = abgr[0];
2286 }
2287 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 default:
2289 UNIMPLEMENTED(); // FIXME
2290 UNREACHABLE();
2291 }
2292
2293 switch (format)
2294 {
2295 case GL_RGBA:
2296 switch (type)
2297 {
2298 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002299 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2300 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2301 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2302 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 break;
2304 default: UNREACHABLE();
2305 }
2306 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002307 case GL_BGRA_EXT:
2308 switch (type)
2309 {
2310 case GL_UNSIGNED_BYTE:
2311 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2312 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2313 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2314 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2315 break;
2316 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2317 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2318 // this type is packed as follows:
2319 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2320 // --------------------------------------------------------------------------------
2321 // | 4th | 3rd | 2nd | 1st component |
2322 // --------------------------------------------------------------------------------
2323 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2324 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2325 ((unsigned short)(15 * a + 0.5f) << 12)|
2326 ((unsigned short)(15 * r + 0.5f) << 8) |
2327 ((unsigned short)(15 * g + 0.5f) << 4) |
2328 ((unsigned short)(15 * b + 0.5f) << 0);
2329 break;
2330 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2331 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2332 // this type is packed as follows:
2333 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2334 // --------------------------------------------------------------------------------
2335 // | 4th | 3rd | 2nd | 1st component |
2336 // --------------------------------------------------------------------------------
2337 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2338 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2339 ((unsigned short)( a + 0.5f) << 15) |
2340 ((unsigned short)(31 * r + 0.5f) << 10) |
2341 ((unsigned short)(31 * g + 0.5f) << 5) |
2342 ((unsigned short)(31 * b + 0.5f) << 0);
2343 break;
2344 default: UNREACHABLE();
2345 }
2346 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002347 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 switch (type)
2349 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002350 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002351 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2352 ((unsigned short)(31 * b + 0.5f) << 0) |
2353 ((unsigned short)(63 * g + 0.5f) << 5) |
2354 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 break;
2356 default: UNREACHABLE();
2357 }
2358 break;
2359 default: UNREACHABLE();
2360 }
2361 }
2362 }
2363
2364 systemSurface->UnlockRect();
2365
2366 systemSurface->Release();
2367}
2368
2369void Context::clear(GLbitfield mask)
2370{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002371 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002372
2373 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2374 {
2375 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2376
2377 return;
2378 }
2379
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002380 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 IDirect3DDevice9 *device = getDevice();
2382 DWORD flags = 0;
2383
2384 if (mask & GL_COLOR_BUFFER_BIT)
2385 {
2386 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002387
2388 if (framebufferObject->getColorbufferType() != GL_NONE)
2389 {
2390 flags |= D3DCLEAR_TARGET;
2391 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392 }
2393
2394 if (mask & GL_DEPTH_BUFFER_BIT)
2395 {
2396 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002397 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398 {
2399 flags |= D3DCLEAR_ZBUFFER;
2400 }
2401 }
2402
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 GLuint stencilUnmasked = 0x0;
2404
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002405 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002408 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002410 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2411 D3DSURFACE_DESC desc;
2412 depthStencil->GetDesc(&desc);
2413
2414 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2415 stencilUnmasked = (0x1 << stencilSize) - 1;
2416
2417 if (stencilUnmasked != 0x0)
2418 {
2419 flags |= D3DCLEAR_STENCIL;
2420 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 }
2422 }
2423
2424 if (mask != 0)
2425 {
2426 return error(GL_INVALID_VALUE);
2427 }
2428
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002429 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2430 {
2431 return;
2432 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002434 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2435 unorm<8>(mState.colorClearValue.red),
2436 unorm<8>(mState.colorClearValue.green),
2437 unorm<8>(mState.colorClearValue.blue));
2438 float depth = clamp01(mState.depthClearValue);
2439 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440
2441 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2442
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002443 if (!renderTarget)
2444 {
2445 return; // Context must be lost, return silently
2446 }
2447
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448 D3DSURFACE_DESC desc;
2449 renderTarget->GetDesc(&desc);
2450
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002451 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452
2453 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002454 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002455 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002456 !(mState.colorMaskRed && mState.colorMaskGreen &&
2457 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458
2459 if (needMaskedColorClear || needMaskedStencilClear)
2460 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002461 // State which is altered in all paths from this point to the clear call is saved.
2462 // State which is altered in only some paths will be flagged dirty in the case that
2463 // that path is taken.
2464 HRESULT hr;
2465 if (mMaskedClearSavedState == NULL)
2466 {
2467 hr = device->BeginStateBlock();
2468 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2469
2470 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2471 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2472 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2473 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2474 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2475 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2476 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2477 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2478 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2479 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2480 device->SetPixelShader(NULL);
2481 device->SetVertexShader(NULL);
2482 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2483 device->SetStreamSourceFreq(0, 1);
2484
2485 hr = device->EndStateBlock(&mMaskedClearSavedState);
2486 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2487 }
2488
2489 ASSERT(mMaskedClearSavedState != NULL);
2490
2491 if (mMaskedClearSavedState != NULL)
2492 {
2493 hr = mMaskedClearSavedState->Capture();
2494 ASSERT(SUCCEEDED(hr));
2495 }
2496
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2498 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2499 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2500 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2501 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2502 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2503 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2504 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2505
2506 if (flags & D3DCLEAR_TARGET)
2507 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002508 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2509 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2510 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2511 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 }
2513 else
2514 {
2515 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2516 }
2517
2518 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2519 {
2520 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2521 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2522 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2523 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002524 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002525 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002526 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2527 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002528 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002529 }
2530 else
2531 {
2532 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2533 }
2534
2535 device->SetPixelShader(NULL);
2536 device->SetVertexShader(NULL);
2537 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002538 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002540 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541 {
2542 float x, y, z, w;
2543 D3DCOLOR diffuse;
2544 };
2545
2546 Vertex quad[4];
2547 quad[0].x = 0.0f;
2548 quad[0].y = (float)desc.Height;
2549 quad[0].z = 0.0f;
2550 quad[0].w = 1.0f;
2551 quad[0].diffuse = color;
2552
2553 quad[1].x = (float)desc.Width;
2554 quad[1].y = (float)desc.Height;
2555 quad[1].z = 0.0f;
2556 quad[1].w = 1.0f;
2557 quad[1].diffuse = color;
2558
2559 quad[2].x = 0.0f;
2560 quad[2].y = 0.0f;
2561 quad[2].z = 0.0f;
2562 quad[2].w = 1.0f;
2563 quad[2].diffuse = color;
2564
2565 quad[3].x = (float)desc.Width;
2566 quad[3].y = 0.0f;
2567 quad[3].z = 0.0f;
2568 quad[3].w = 1.0f;
2569 quad[3].diffuse = color;
2570
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002571 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002572 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002573
2574 if (flags & D3DCLEAR_ZBUFFER)
2575 {
2576 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2577 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2578 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2579 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002580
2581 if (mMaskedClearSavedState != NULL)
2582 {
2583 mMaskedClearSavedState->Apply();
2584 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002585 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002586 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002587 {
2588 device->Clear(0, NULL, flags, color, depth, stencil);
2589 }
2590}
2591
2592void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2593{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002594 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595 {
2596 return error(GL_INVALID_OPERATION);
2597 }
2598
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002599 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600 IDirect3DDevice9 *device = getDevice();
2601 D3DPRIMITIVETYPE primitiveType;
2602 int primitiveCount;
2603
2604 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2605 return error(GL_INVALID_ENUM);
2606
2607 if (primitiveCount <= 0)
2608 {
2609 return;
2610 }
2611
2612 if (!applyRenderTarget(false))
2613 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002614 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615 }
2616
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002617 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002618
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002619 TranslatedIndexData indexInfo;
2620 bool useIndexing;
2621 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002622 if (err != GL_NO_ERROR)
2623 {
2624 return error(err);
2625 }
2626
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627 applyShaders();
2628 applyTextures();
2629
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002630 if (!getCurrentProgram()->validateSamplers())
2631 {
2632 return error(GL_INVALID_OPERATION);
2633 }
2634
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002635 if (!cullSkipsDraw(mode))
2636 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002637 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002638 if (useIndexing)
2639 {
2640 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2641 }
2642 else
2643 {
2644 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2645 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002647}
2648
2649void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2650{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002651 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002652 {
2653 return error(GL_INVALID_OPERATION);
2654 }
2655
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002656 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002657 {
2658 return error(GL_INVALID_OPERATION);
2659 }
2660
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002661 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002662 IDirect3DDevice9 *device = getDevice();
2663 D3DPRIMITIVETYPE primitiveType;
2664 int primitiveCount;
2665
2666 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2667 return error(GL_INVALID_ENUM);
2668
2669 if (primitiveCount <= 0)
2670 {
2671 return;
2672 }
2673
2674 if (!applyRenderTarget(false))
2675 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002676 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677 }
2678
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002679 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002680
2681 TranslatedIndexData indexInfo;
2682 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2683 if (err != GL_NO_ERROR)
2684 {
2685 return error(err);
2686 }
2687
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002688 err = applyVertexBuffer(indexInfo);
2689 if (err != GL_NO_ERROR)
2690 {
2691 return error(err);
2692 }
2693
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694 applyShaders();
2695 applyTextures();
2696
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002697 if (!getCurrentProgram()->validateSamplers())
2698 {
2699 return error(GL_INVALID_OPERATION);
2700 }
2701
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002702 if (!cullSkipsDraw(mode))
2703 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002704 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002705 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002706 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707}
2708
2709void Context::finish()
2710{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002711 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002712 IDirect3DDevice9 *device = getDevice();
2713 IDirect3DQuery9 *occlusionQuery = NULL;
2714
2715 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2716
2717 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2718 {
2719 return error(GL_OUT_OF_MEMORY);
2720 }
2721
2722 ASSERT(SUCCEEDED(result));
2723
2724 if (occlusionQuery)
2725 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002726 IDirect3DStateBlock9 *savedState = NULL;
2727 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2728
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002729 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2730 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731
2732 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002733 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734 device->SetPixelShader(NULL);
2735 device->SetVertexShader(NULL);
2736 device->SetFVF(D3DFVF_XYZRHW);
2737 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002738 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002739 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002741 result = occlusionQuery->Issue(D3DISSUE_END);
2742 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743
2744 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2745 {
2746 // Keep polling, but allow other threads to do something useful first
2747 Sleep(0);
2748 }
2749
2750 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002751
2752 if (savedState)
2753 {
2754 savedState->Apply();
2755 savedState->Release();
2756 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002757 }
2758}
2759
2760void Context::flush()
2761{
2762 IDirect3DDevice9 *device = getDevice();
2763 IDirect3DQuery9 *eventQuery = NULL;
2764
2765 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2766
2767 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2768 {
2769 return error(GL_OUT_OF_MEMORY);
2770 }
2771
2772 ASSERT(SUCCEEDED(result));
2773
2774 if (eventQuery)
2775 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002776 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2777 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002779 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002780 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002781
2782 if (result == D3DERR_DEVICELOST)
2783 {
2784 error(GL_OUT_OF_MEMORY);
2785 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786 }
2787}
2788
2789void Context::recordInvalidEnum()
2790{
2791 mInvalidEnum = true;
2792}
2793
2794void Context::recordInvalidValue()
2795{
2796 mInvalidValue = true;
2797}
2798
2799void Context::recordInvalidOperation()
2800{
2801 mInvalidOperation = true;
2802}
2803
2804void Context::recordOutOfMemory()
2805{
2806 mOutOfMemory = true;
2807}
2808
2809void Context::recordInvalidFramebufferOperation()
2810{
2811 mInvalidFramebufferOperation = true;
2812}
2813
2814// Get one of the recorded errors and clear its flag, if any.
2815// [OpenGL ES 2.0.24] section 2.5 page 13.
2816GLenum Context::getError()
2817{
2818 if (mInvalidEnum)
2819 {
2820 mInvalidEnum = false;
2821
2822 return GL_INVALID_ENUM;
2823 }
2824
2825 if (mInvalidValue)
2826 {
2827 mInvalidValue = false;
2828
2829 return GL_INVALID_VALUE;
2830 }
2831
2832 if (mInvalidOperation)
2833 {
2834 mInvalidOperation = false;
2835
2836 return GL_INVALID_OPERATION;
2837 }
2838
2839 if (mOutOfMemory)
2840 {
2841 mOutOfMemory = false;
2842
2843 return GL_OUT_OF_MEMORY;
2844 }
2845
2846 if (mInvalidFramebufferOperation)
2847 {
2848 mInvalidFramebufferOperation = false;
2849
2850 return GL_INVALID_FRAMEBUFFER_OPERATION;
2851 }
2852
2853 return GL_NO_ERROR;
2854}
2855
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002856bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002857{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002858 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002859}
2860
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002861int Context::getMaxSupportedSamples() const
2862{
2863 return mMaxSupportedSamples;
2864}
2865
2866int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2867{
2868 if (requested == 0)
2869 {
2870 return requested;
2871 }
2872
2873 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2874 if (itr == mMultiSampleSupport.end())
2875 {
2876 return -1;
2877 }
2878
2879 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2880 {
2881 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2882 {
2883 return i;
2884 }
2885 }
2886
2887 return -1;
2888}
2889
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002890bool Context::supportsEventQueries() const
2891{
2892 return mSupportsEventQueries;
2893}
2894
daniel@transgaming.com01868132010-08-24 19:21:17 +00002895bool Context::supportsCompressedTextures() const
2896{
2897 return mSupportsCompressedTextures;
2898}
2899
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002900bool Context::supportsFloatTextures() const
2901{
2902 return mSupportsFloatTextures;
2903}
2904
2905bool Context::supportsFloatLinearFilter() const
2906{
2907 return mSupportsFloatLinearFilter;
2908}
2909
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002910bool Context::supportsFloatRenderableTextures() const
2911{
2912 return mSupportsFloatRenderableTextures;
2913}
2914
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002915bool Context::supportsHalfFloatTextures() const
2916{
2917 return mSupportsHalfFloatTextures;
2918}
2919
2920bool Context::supportsHalfFloatLinearFilter() const
2921{
2922 return mSupportsHalfFloatLinearFilter;
2923}
2924
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002925bool Context::supportsHalfFloatRenderableTextures() const
2926{
2927 return mSupportsHalfFloatRenderableTextures;
2928}
2929
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002930void Context::detachBuffer(GLuint buffer)
2931{
2932 // [OpenGL ES 2.0.24] section 2.9 page 22:
2933 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2934 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2935
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002936 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002937 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002938 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002939 }
2940
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002941 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002942 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002943 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002944 }
2945
2946 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2947 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002948 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002949 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002950 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002951 }
2952 }
2953}
2954
2955void Context::detachTexture(GLuint texture)
2956{
2957 // [OpenGL ES 2.0.24] section 3.8 page 84:
2958 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2959 // rebound to texture object zero
2960
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002961 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002962 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002963 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002964 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002965 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002966 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002967 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002968 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002969 }
2970 }
2971
2972 // [OpenGL ES 2.0.24] section 4.4 page 112:
2973 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2974 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2975 // image was attached in the currently bound framebuffer.
2976
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002977 Framebuffer *readFramebuffer = getReadFramebuffer();
2978 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002979
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002980 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002981 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002982 readFramebuffer->detachTexture(texture);
2983 }
2984
2985 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2986 {
2987 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002988 }
2989}
2990
2991void Context::detachFramebuffer(GLuint framebuffer)
2992{
2993 // [OpenGL ES 2.0.24] section 4.4 page 107:
2994 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2995 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2996
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00002997 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002998 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002999 bindReadFramebuffer(0);
3000 }
3001
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003002 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003003 {
3004 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003005 }
3006}
3007
3008void Context::detachRenderbuffer(GLuint renderbuffer)
3009{
3010 // [OpenGL ES 2.0.24] section 4.4 page 109:
3011 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3012 // had been executed with the target RENDERBUFFER and name of zero.
3013
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003014 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003015 {
3016 bindRenderbuffer(0);
3017 }
3018
3019 // [OpenGL ES 2.0.24] section 4.4 page 111:
3020 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3021 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3022 // point to which this image was attached in the currently bound framebuffer.
3023
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003024 Framebuffer *readFramebuffer = getReadFramebuffer();
3025 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003026
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003027 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003028 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003029 readFramebuffer->detachRenderbuffer(renderbuffer);
3030 }
3031
3032 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3033 {
3034 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003035 }
3036}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003037
3038Texture *Context::getIncompleteTexture(SamplerType type)
3039{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003040 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003041
3042 if (t == NULL)
3043 {
3044 static const GLubyte color[] = { 0, 0, 0, 255 };
3045
3046 switch (type)
3047 {
3048 default:
3049 UNREACHABLE();
3050 // default falls through to SAMPLER_2D
3051
3052 case SAMPLER_2D:
3053 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003054 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003055 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003056 t = incomplete2d;
3057 }
3058 break;
3059
3060 case SAMPLER_CUBE:
3061 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003062 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003063
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003064 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3065 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3066 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3067 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3068 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3069 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003070
3071 t = incompleteCube;
3072 }
3073 break;
3074 }
3075
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003076 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003077 }
3078
3079 return t;
3080}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003081
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003082bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003083{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003084 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003085}
3086
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003087bool Context::isTriangleMode(GLenum drawMode)
3088{
3089 switch (drawMode)
3090 {
3091 case GL_TRIANGLES:
3092 case GL_TRIANGLE_FAN:
3093 case GL_TRIANGLE_STRIP:
3094 return true;
3095 case GL_POINTS:
3096 case GL_LINES:
3097 case GL_LINE_LOOP:
3098 case GL_LINE_STRIP:
3099 return false;
3100 default: UNREACHABLE();
3101 }
3102
3103 return false;
3104}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003105
3106void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3107{
3108 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3109
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003110 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3111 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3112 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3113 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003114
3115 mVertexDataManager->dirtyCurrentValues();
3116}
3117
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003118void Context::initExtensionString()
3119{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003120 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003121 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3122 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003123 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003124 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003125 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003126
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003127 if (supportsEventQueries())
3128 {
3129 mExtensionString += "GL_NV_fence ";
3130 }
3131
daniel@transgaming.com01868132010-08-24 19:21:17 +00003132 if (supportsCompressedTextures())
3133 {
3134 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3135 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003136
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003137 if (supportsFloatTextures())
3138 {
3139 mExtensionString += "GL_OES_texture_float ";
3140 }
3141
3142 if (supportsHalfFloatTextures())
3143 {
3144 mExtensionString += "GL_OES_texture_half_float ";
3145 }
3146
3147 if (supportsFloatLinearFilter())
3148 {
3149 mExtensionString += "GL_OES_texture_float_linear ";
3150 }
3151
3152 if (supportsHalfFloatLinearFilter())
3153 {
3154 mExtensionString += "GL_OES_texture_half_float_linear ";
3155 }
3156
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003157 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003158 {
3159 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3160 }
3161
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003162 if (mBufferBackEnd->supportIntIndices())
3163 {
3164 mExtensionString += "GL_OES_element_index_uint ";
3165 }
3166
3167 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3168 if (end != std::string::npos)
3169 {
3170 mExtensionString.resize(end+1);
3171 }
3172}
3173
3174const char *Context::getExtensionString() const
3175{
3176 return mExtensionString.c_str();
3177}
3178
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003179void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3180 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3181 GLbitfield mask)
3182{
3183 IDirect3DDevice9 *device = getDevice();
3184
3185 Framebuffer *readFramebuffer = getReadFramebuffer();
3186 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3187
3188 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3189 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3190 {
3191 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3192 }
3193
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003194 if (drawFramebuffer->getSamples() != 0)
3195 {
3196 return error(GL_INVALID_OPERATION);
3197 }
3198
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003199 RECT sourceRect;
3200 RECT destRect;
3201
3202 if (srcX0 < srcX1)
3203 {
3204 sourceRect.left = srcX0;
3205 sourceRect.right = srcX1;
3206 destRect.left = dstX0;
3207 destRect.right = dstX1;
3208 }
3209 else
3210 {
3211 sourceRect.left = srcX1;
3212 destRect.left = dstX1;
3213 sourceRect.right = srcX0;
3214 destRect.right = dstX0;
3215 }
3216
3217 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3218 // flip our Y-values here
3219 if (srcY0 < srcY1)
3220 {
3221 sourceRect.bottom = srcY1;
3222 destRect.bottom = dstY1;
3223 sourceRect.top = srcY0;
3224 destRect.top = dstY0;
3225 }
3226 else
3227 {
3228 sourceRect.bottom = srcY0;
3229 destRect.bottom = dstY0;
3230 sourceRect.top = srcY1;
3231 destRect.top = dstY1;
3232 }
3233
3234 RECT sourceScissoredRect = sourceRect;
3235 RECT destScissoredRect = destRect;
3236
3237 if (mState.scissorTest)
3238 {
3239 // Only write to parts of the destination framebuffer which pass the scissor test
3240 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3241 // rect will be checked against scissorY, rather than the bottom.
3242 if (destRect.left < mState.scissorX)
3243 {
3244 int xDiff = mState.scissorX - destRect.left;
3245 destScissoredRect.left = mState.scissorX;
3246 sourceScissoredRect.left += xDiff;
3247 }
3248
3249 if (destRect.right > mState.scissorX + mState.scissorWidth)
3250 {
3251 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3252 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3253 sourceScissoredRect.right -= xDiff;
3254 }
3255
3256 if (destRect.top < mState.scissorY)
3257 {
3258 int yDiff = mState.scissorY - destRect.top;
3259 destScissoredRect.top = mState.scissorY;
3260 sourceScissoredRect.top += yDiff;
3261 }
3262
3263 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3264 {
3265 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3266 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3267 sourceScissoredRect.bottom -= yDiff;
3268 }
3269 }
3270
3271 bool blitRenderTarget = false;
3272 bool blitDepthStencil = false;
3273
3274 RECT sourceTrimmedRect = sourceScissoredRect;
3275 RECT destTrimmedRect = destScissoredRect;
3276
3277 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3278 // the actual draw and read surfaces.
3279 if (sourceTrimmedRect.left < 0)
3280 {
3281 int xDiff = 0 - sourceTrimmedRect.left;
3282 sourceTrimmedRect.left = 0;
3283 destTrimmedRect.left += xDiff;
3284 }
3285
3286 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3287 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3288 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3289 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3290
3291 if (sourceTrimmedRect.right > readBufferWidth)
3292 {
3293 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3294 sourceTrimmedRect.right = readBufferWidth;
3295 destTrimmedRect.right -= xDiff;
3296 }
3297
3298 if (sourceTrimmedRect.top < 0)
3299 {
3300 int yDiff = 0 - sourceTrimmedRect.top;
3301 sourceTrimmedRect.top = 0;
3302 destTrimmedRect.top += yDiff;
3303 }
3304
3305 if (sourceTrimmedRect.bottom > readBufferHeight)
3306 {
3307 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3308 sourceTrimmedRect.bottom = readBufferHeight;
3309 destTrimmedRect.bottom -= yDiff;
3310 }
3311
3312 if (destTrimmedRect.left < 0)
3313 {
3314 int xDiff = 0 - destTrimmedRect.left;
3315 destTrimmedRect.left = 0;
3316 sourceTrimmedRect.left += xDiff;
3317 }
3318
3319 if (destTrimmedRect.right > drawBufferWidth)
3320 {
3321 int xDiff = destTrimmedRect.right - drawBufferWidth;
3322 destTrimmedRect.right = drawBufferWidth;
3323 sourceTrimmedRect.right -= xDiff;
3324 }
3325
3326 if (destTrimmedRect.top < 0)
3327 {
3328 int yDiff = 0 - destTrimmedRect.top;
3329 destTrimmedRect.top = 0;
3330 sourceTrimmedRect.top += yDiff;
3331 }
3332
3333 if (destTrimmedRect.bottom > drawBufferHeight)
3334 {
3335 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3336 destTrimmedRect.bottom = drawBufferHeight;
3337 sourceTrimmedRect.bottom -= yDiff;
3338 }
3339
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003340 bool partialBufferCopy = false;
3341 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3342 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3343 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3344 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3345 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3346 {
3347 partialBufferCopy = true;
3348 }
3349
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003350 if (mask & GL_COLOR_BUFFER_BIT)
3351 {
3352 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3353 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3354 {
3355 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3356 return error(GL_INVALID_OPERATION);
3357 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003358
3359 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3360 {
3361 return error(GL_INVALID_OPERATION);
3362 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003363
3364 blitRenderTarget = true;
3365
3366 }
3367
3368 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3369 {
3370 DepthStencilbuffer *readDSBuffer = NULL;
3371 DepthStencilbuffer *drawDSBuffer = NULL;
3372
3373 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3374 // both a depth and stencil buffer, it will be the same buffer.
3375
3376 if (mask & GL_DEPTH_BUFFER_BIT)
3377 {
3378 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3379 {
3380 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3381 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3382 {
3383 return error(GL_INVALID_OPERATION);
3384 }
3385
3386 blitDepthStencil = true;
3387 readDSBuffer = readFramebuffer->getDepthbuffer();
3388 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3389 }
3390 }
3391
3392 if (mask & GL_STENCIL_BUFFER_BIT)
3393 {
3394 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3395 {
3396 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3397 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3398 {
3399 return error(GL_INVALID_OPERATION);
3400 }
3401
3402 blitDepthStencil = true;
3403 readDSBuffer = readFramebuffer->getStencilbuffer();
3404 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3405 }
3406 }
3407
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003408 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003409 {
3410 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3411 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3412 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003413
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003414 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3415 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003416 {
3417 return error(GL_INVALID_OPERATION);
3418 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003419 }
3420
3421 if (blitRenderTarget || blitDepthStencil)
3422 {
3423 egl::Display *display = getDisplay();
3424 display->endScene();
3425
3426 if (blitRenderTarget)
3427 {
3428 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3429 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3430
3431 if (FAILED(result))
3432 {
3433 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3434 return;
3435 }
3436 }
3437
3438 if (blitDepthStencil)
3439 {
3440 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3441
3442 if (FAILED(result))
3443 {
3444 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3445 return;
3446 }
3447 }
3448 }
3449}
3450
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003451}
3452
3453extern "C"
3454{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003455gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003456{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003457 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003458}
3459
3460void glDestroyContext(gl::Context *context)
3461{
3462 delete context;
3463
3464 if (context == gl::getContext())
3465 {
3466 gl::makeCurrent(NULL, NULL, NULL);
3467 }
3468}
3469
3470void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3471{
3472 gl::makeCurrent(context, display, surface);
3473}
3474
3475gl::Context *glGetCurrentContext()
3476{
3477 return gl::getContext();
3478}
3479}