blob: 353d4cd89e4e2659aefdd3d939c42620c9b1cae4 [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.org55255022010-09-11 02:12:47 +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
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000174 while (!mFenceMap.empty())
175 {
176 deleteFence(mFenceMap.begin()->first);
177 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000178
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000179 while (!mMultiSampleSupport.empty())
180 {
181 delete [] mMultiSampleSupport.begin()->second;
182 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
183 }
184
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000185 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
186 {
187 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
188 {
189 mState.samplerTexture[type][sampler].set(NULL);
190 }
191 }
192
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000193 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
194 {
apatrick@chromium.org55255022010-09-11 02:12:47 +0000195 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000196 }
197
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000198 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
199 {
200 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
201 }
202
203 mState.arrayBuffer.set(NULL);
204 mState.elementArrayBuffer.set(NULL);
205 mState.texture2D.set(NULL);
206 mState.textureCubeMap.set(NULL);
207 mState.renderbuffer.set(NULL);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000208 mState.readFramebuffer.set(NULL);
209 mState.drawFramebuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210
apatrick@chromium.org55255022010-09-11 02:12:47 +0000211 mTexture2DZero.set(NULL);
212 mTextureCubeMapZero.set(NULL);
213 mFramebufferZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000215 delete mBufferBackEnd;
216 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000217 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000218 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000219
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000220 if (mMaskedClearSavedState)
221 {
222 mMaskedClearSavedState->Release();
223 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000224
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000225 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226}
227
228void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
229{
230 IDirect3DDevice9 *device = display->getDevice();
231
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000232 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000233 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000234 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000235
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000236 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000237 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000238 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000239 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000240
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000241 const D3DFORMAT renderBufferFormats[] =
242 {
243 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000244 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000245 D3DFMT_R5G6B5,
246 D3DFMT_D24S8
247 };
248
249 int max = 0;
250 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
251 {
252 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
253 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
254 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
255
256 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
257 {
258 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
259 {
260 max = j;
261 }
262 }
263 }
264
265 mMaxSupportedSamples = max;
266
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000267 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000268 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000269 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
270 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.com01868132010-08-24 19:21:17 +0000271
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000272 initExtensionString();
273
274 mState.viewportX = 0;
275 mState.viewportY = 0;
276 mState.viewportWidth = surface->getWidth();
277 mState.viewportHeight = surface->getHeight();
278
279 mState.scissorX = 0;
280 mState.scissorY = 0;
281 mState.scissorWidth = surface->getWidth();
282 mState.scissorHeight = surface->getHeight();
283
284 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000285 }
286
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
288 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000289 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000292 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000293 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
295 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000297 if (defaultRenderTarget)
298 {
299 defaultRenderTarget->Release();
300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000302 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000304 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000306
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000307 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000308
309 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310}
311
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000312// 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 +0000313void Context::markAllStateDirty()
314{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000315 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000316 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000317 mAppliedStencilbufferSerial = 0;
318 mDepthStencilDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000319 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000320
321 mClearStateDirty = true;
322 mCullStateDirty = true;
323 mDepthStateDirty = true;
324 mMaskStateDirty = true;
325 mBlendStateDirty = true;
326 mStencilStateDirty = true;
327 mPolygonOffsetStateDirty = true;
328 mScissorStateDirty = true;
329 mSampleStateDirty = true;
330 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000331 mFrontFaceDirty = true;
332
333 if (mBufferBackEnd != NULL)
334 {
335 mBufferBackEnd->invalidate();
336 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000337}
338
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339void Context::setClearColor(float red, float green, float blue, float alpha)
340{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000341 mState.colorClearValue.red = red;
342 mState.colorClearValue.green = green;
343 mState.colorClearValue.blue = blue;
344 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345}
346
347void Context::setClearDepth(float depth)
348{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000349 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350}
351
352void Context::setClearStencil(int stencil)
353{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000354 mState.stencilClearValue = stencil;
355}
356
357void Context::setCullFace(bool enabled)
358{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000359 if (mState.cullFace != enabled)
360 {
361 mState.cullFace = enabled;
362 mCullStateDirty = true;
363 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000364}
365
366bool Context::isCullFaceEnabled() const
367{
368 return mState.cullFace;
369}
370
371void Context::setCullMode(GLenum mode)
372{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000373 if (mState.cullMode != mode)
374 {
375 mState.cullMode = mode;
376 mCullStateDirty = true;
377 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000378}
379
380void Context::setFrontFace(GLenum front)
381{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000382 if (mState.frontFace != front)
383 {
384 mState.frontFace = front;
385 mFrontFaceDirty = true;
386 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000387}
388
389void Context::setDepthTest(bool enabled)
390{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000391 if (mState.depthTest != enabled)
392 {
393 mState.depthTest = enabled;
394 mDepthStateDirty = true;
395 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000396}
397
398bool Context::isDepthTestEnabled() const
399{
400 return mState.depthTest;
401}
402
403void Context::setDepthFunc(GLenum depthFunc)
404{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000405 if (mState.depthFunc != depthFunc)
406 {
407 mState.depthFunc = depthFunc;
408 mDepthStateDirty = true;
409 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000410}
411
412void Context::setDepthRange(float zNear, float zFar)
413{
414 mState.zNear = zNear;
415 mState.zFar = zFar;
416}
417
418void Context::setBlend(bool enabled)
419{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000420 if (mState.blend != enabled)
421 {
422 mState.blend = enabled;
423 mBlendStateDirty = true;
424 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000425}
426
427bool Context::isBlendEnabled() const
428{
429 return mState.blend;
430}
431
432void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
433{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000434 if (mState.sourceBlendRGB != sourceRGB ||
435 mState.sourceBlendAlpha != sourceAlpha ||
436 mState.destBlendRGB != destRGB ||
437 mState.destBlendAlpha != destAlpha)
438 {
439 mState.sourceBlendRGB = sourceRGB;
440 mState.destBlendRGB = destRGB;
441 mState.sourceBlendAlpha = sourceAlpha;
442 mState.destBlendAlpha = destAlpha;
443 mBlendStateDirty = true;
444 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000445}
446
447void Context::setBlendColor(float red, float green, float blue, float alpha)
448{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000449 if (mState.blendColor.red != red ||
450 mState.blendColor.green != green ||
451 mState.blendColor.blue != blue ||
452 mState.blendColor.alpha != alpha)
453 {
454 mState.blendColor.red = red;
455 mState.blendColor.green = green;
456 mState.blendColor.blue = blue;
457 mState.blendColor.alpha = alpha;
458 mBlendStateDirty = true;
459 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000460}
461
462void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
463{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000464 if (mState.blendEquationRGB != rgbEquation ||
465 mState.blendEquationAlpha != alphaEquation)
466 {
467 mState.blendEquationRGB = rgbEquation;
468 mState.blendEquationAlpha = alphaEquation;
469 mBlendStateDirty = true;
470 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000471}
472
473void Context::setStencilTest(bool enabled)
474{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000475 if (mState.stencilTest != enabled)
476 {
477 mState.stencilTest = enabled;
478 mStencilStateDirty = true;
479 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000480}
481
482bool Context::isStencilTestEnabled() const
483{
484 return mState.stencilTest;
485}
486
487void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
488{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000489 if (mState.stencilFunc != stencilFunc ||
490 mState.stencilRef != stencilRef ||
491 mState.stencilMask != stencilMask)
492 {
493 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000494 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000495 mState.stencilMask = stencilMask;
496 mStencilStateDirty = true;
497 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000498}
499
500void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
501{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000502 if (mState.stencilBackFunc != stencilBackFunc ||
503 mState.stencilBackRef != stencilBackRef ||
504 mState.stencilBackMask != stencilBackMask)
505 {
506 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000507 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000508 mState.stencilBackMask = stencilBackMask;
509 mStencilStateDirty = true;
510 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000511}
512
513void Context::setStencilWritemask(GLuint stencilWritemask)
514{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000515 if (mState.stencilWritemask != stencilWritemask)
516 {
517 mState.stencilWritemask = stencilWritemask;
518 mStencilStateDirty = true;
519 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000520}
521
522void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
523{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000524 if (mState.stencilBackWritemask != stencilBackWritemask)
525 {
526 mState.stencilBackWritemask = stencilBackWritemask;
527 mStencilStateDirty = true;
528 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000529}
530
531void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
532{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000533 if (mState.stencilFail != stencilFail ||
534 mState.stencilPassDepthFail != stencilPassDepthFail ||
535 mState.stencilPassDepthPass != stencilPassDepthPass)
536 {
537 mState.stencilFail = stencilFail;
538 mState.stencilPassDepthFail = stencilPassDepthFail;
539 mState.stencilPassDepthPass = stencilPassDepthPass;
540 mStencilStateDirty = true;
541 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000542}
543
544void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
545{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000546 if (mState.stencilBackFail != stencilBackFail ||
547 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
548 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
549 {
550 mState.stencilBackFail = stencilBackFail;
551 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
552 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
553 mStencilStateDirty = true;
554 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000555}
556
557void Context::setPolygonOffsetFill(bool enabled)
558{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000559 if (mState.polygonOffsetFill != enabled)
560 {
561 mState.polygonOffsetFill = enabled;
562 mPolygonOffsetStateDirty = true;
563 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000564}
565
566bool Context::isPolygonOffsetFillEnabled() const
567{
568 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000569
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000570}
571
572void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
573{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000574 if (mState.polygonOffsetFactor != factor ||
575 mState.polygonOffsetUnits != units)
576 {
577 mState.polygonOffsetFactor = factor;
578 mState.polygonOffsetUnits = units;
579 mPolygonOffsetStateDirty = true;
580 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000581}
582
583void Context::setSampleAlphaToCoverage(bool enabled)
584{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000585 if (mState.sampleAlphaToCoverage != enabled)
586 {
587 mState.sampleAlphaToCoverage = enabled;
588 mSampleStateDirty = true;
589 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000590}
591
592bool Context::isSampleAlphaToCoverageEnabled() const
593{
594 return mState.sampleAlphaToCoverage;
595}
596
597void Context::setSampleCoverage(bool enabled)
598{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000599 if (mState.sampleCoverage != enabled)
600 {
601 mState.sampleCoverage = enabled;
602 mSampleStateDirty = true;
603 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000604}
605
606bool Context::isSampleCoverageEnabled() const
607{
608 return mState.sampleCoverage;
609}
610
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000611void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000612{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000613 if (mState.sampleCoverageValue != value ||
614 mState.sampleCoverageInvert != invert)
615 {
616 mState.sampleCoverageValue = value;
617 mState.sampleCoverageInvert = invert;
618 mSampleStateDirty = true;
619 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000620}
621
622void Context::setScissorTest(bool enabled)
623{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000624 if (mState.scissorTest != enabled)
625 {
626 mState.scissorTest = enabled;
627 mScissorStateDirty = true;
628 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000629}
630
631bool Context::isScissorTestEnabled() const
632{
633 return mState.scissorTest;
634}
635
636void Context::setDither(bool enabled)
637{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000638 if (mState.dither != enabled)
639 {
640 mState.dither = enabled;
641 mDitherStateDirty = true;
642 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000643}
644
645bool Context::isDitherEnabled() const
646{
647 return mState.dither;
648}
649
650void Context::setLineWidth(GLfloat width)
651{
652 mState.lineWidth = width;
653}
654
655void Context::setGenerateMipmapHint(GLenum hint)
656{
657 mState.generateMipmapHint = hint;
658}
659
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000660void Context::setFragmentShaderDerivativeHint(GLenum hint)
661{
662 mState.fragmentShaderDerivativeHint = hint;
663 // TODO: Propagate the hint to shader translator so we can write
664 // ddx, ddx_coarse, or ddx_fine depending on the hint.
665 // Ignore for now. It is valid for implementations to ignore hint.
666}
667
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000668void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
669{
670 mState.viewportX = x;
671 mState.viewportY = y;
672 mState.viewportWidth = width;
673 mState.viewportHeight = height;
674}
675
676void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
677{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000678 if (mState.scissorX != x || mState.scissorY != y ||
679 mState.scissorWidth != width || mState.scissorHeight != height)
680 {
681 mState.scissorX = x;
682 mState.scissorY = y;
683 mState.scissorWidth = width;
684 mState.scissorHeight = height;
685 mScissorStateDirty = true;
686 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000687}
688
689void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
690{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000691 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
692 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
693 {
694 mState.colorMaskRed = red;
695 mState.colorMaskGreen = green;
696 mState.colorMaskBlue = blue;
697 mState.colorMaskAlpha = alpha;
698 mMaskStateDirty = true;
699 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000700}
701
702void Context::setDepthMask(bool mask)
703{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000704 if (mState.depthMask != mask)
705 {
706 mState.depthMask = mask;
707 mMaskStateDirty = true;
708 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000709}
710
711void Context::setActiveSampler(int active)
712{
713 mState.activeSampler = active;
714}
715
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000716GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000717{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000718 return mState.readFramebuffer.id();
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000719}
720
721GLuint Context::getDrawFramebufferHandle() const
722{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000723 return mState.drawFramebuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000724}
725
726GLuint Context::getRenderbufferHandle() const
727{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000728 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000729}
730
731GLuint Context::getArrayBufferHandle() const
732{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000733 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734}
735
736void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
737{
738 mState.vertexAttribute[attribNum].mEnabled = enabled;
739}
740
741const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
742{
743 return mState.vertexAttribute[attribNum];
744}
745
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000746void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000747 GLsizei stride, const void *pointer)
748{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000749 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000750 mState.vertexAttribute[attribNum].mSize = size;
751 mState.vertexAttribute[attribNum].mType = type;
752 mState.vertexAttribute[attribNum].mNormalized = normalized;
753 mState.vertexAttribute[attribNum].mStride = stride;
754 mState.vertexAttribute[attribNum].mPointer = pointer;
755}
756
757const void *Context::getVertexAttribPointer(unsigned int attribNum) const
758{
759 return mState.vertexAttribute[attribNum].mPointer;
760}
761
762// returns entire set of attributes as a block
763const AttributeState *Context::getVertexAttribBlock()
764{
765 return mState.vertexAttribute;
766}
767
768void Context::setPackAlignment(GLint alignment)
769{
770 mState.packAlignment = alignment;
771}
772
773GLint Context::getPackAlignment() const
774{
775 return mState.packAlignment;
776}
777
778void Context::setUnpackAlignment(GLint alignment)
779{
780 mState.unpackAlignment = alignment;
781}
782
783GLint Context::getUnpackAlignment() const
784{
785 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786}
787
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788GLuint Context::createBuffer()
789{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000790 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791}
792
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793GLuint Context::createProgram()
794{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000795 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000798GLuint Context::createShader(GLenum type)
799{
800 return mResourceManager->createShader(type);
801}
802
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803GLuint Context::createTexture()
804{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000805 return mResourceManager->createTexture();
806}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000808GLuint Context::createRenderbuffer()
809{
810 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811}
812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813GLuint Context::createFramebuffer()
814{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000815 return mResourceManager->createFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816}
817
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000818GLuint Context::createFence()
819{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000820 unsigned int handle = 0;
821
822 while (mFenceMap.find(handle) != mFenceMap.end())
823 {
824 handle++;
825 }
826
827 mFenceMap[handle] = new Fence;
828
829 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000830}
831
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832void Context::deleteBuffer(GLuint buffer)
833{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000834 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835 {
836 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000838
839 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840}
841
842void Context::deleteShader(GLuint shader)
843{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000844 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845}
846
847void Context::deleteProgram(GLuint program)
848{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000849 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850}
851
852void Context::deleteTexture(GLuint texture)
853{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000854 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855 {
856 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000858
859 mResourceManager->deleteTexture(texture);
860}
861
862void Context::deleteRenderbuffer(GLuint renderbuffer)
863{
864 if (mResourceManager->getRenderbuffer(renderbuffer))
865 {
866 detachRenderbuffer(renderbuffer);
867 }
868
869 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870}
871
872void Context::deleteFramebuffer(GLuint framebuffer)
873{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000874 if (mResourceManager->getFramebuffer(framebuffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 {
876 detachFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 }
apatrick@chromium.org55255022010-09-11 02:12:47 +0000878
879 mResourceManager->deleteFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000881
882void Context::deleteFence(GLuint fence)
883{
884 FenceMap::iterator fenceObject = mFenceMap.find(fence);
885
886 if (fenceObject != mFenceMap.end())
887 {
888 delete fenceObject->second;
889 mFenceMap.erase(fenceObject);
890 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000891}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000893Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000895 return mResourceManager->getBuffer(handle);
896}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000898Shader *Context::getShader(GLuint handle)
899{
900 return mResourceManager->getShader(handle);
901}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000903Program *Context::getProgram(GLuint handle)
904{
905 return mResourceManager->getProgram(handle);
906}
907
908Texture *Context::getTexture(GLuint handle)
909{
910 return mResourceManager->getTexture(handle);
911}
912
913Renderbuffer *Context::getRenderbuffer(GLuint handle)
914{
915 return mResourceManager->getRenderbuffer(handle);
916}
917
apatrick@chromium.org55255022010-09-11 02:12:47 +0000918Framebuffer *Context::getFramebuffer(GLuint handle)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000919{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000920 if (handle == 0)
921 {
922 return mFramebufferZero.get();
923 }
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000924
apatrick@chromium.org55255022010-09-11 02:12:47 +0000925 return mResourceManager->getFramebuffer(handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926}
927
928void Context::bindArrayBuffer(unsigned int buffer)
929{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000930 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000932 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933}
934
935void Context::bindElementArrayBuffer(unsigned int buffer)
936{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000937 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000939 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940}
941
942void Context::bindTexture2D(GLuint texture)
943{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000944 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000946 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000948 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949}
950
951void Context::bindTextureCubeMap(GLuint texture)
952{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000953 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000955 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000957 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958}
959
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000960void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000962 mResourceManager->checkFramebufferAllocation(framebuffer);
963
964 mState.readFramebuffer.set(getFramebuffer(framebuffer));
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000965}
966
967void Context::bindDrawFramebuffer(GLuint framebuffer)
968{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000969 mResourceManager->checkFramebufferAllocation(framebuffer);
970
971 mState.drawFramebuffer.set(getFramebuffer(framebuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
974void Context::bindRenderbuffer(GLuint renderbuffer)
975{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000976 mResourceManager->checkRenderbufferAllocation(renderbuffer);
977
978 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979}
980
981void Context::useProgram(GLuint program)
982{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000983 GLuint priorProgram = mState.currentProgram;
984 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000985
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000986 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000988 Program *newProgram = mResourceManager->getProgram(program);
989 Program *oldProgram = mResourceManager->getProgram(priorProgram);
990
991 if (newProgram)
992 {
993 newProgram->addRef();
994 }
995
996 if (oldProgram)
997 {
998 oldProgram->release();
999 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001}
1002
1003void Context::setFramebufferZero(Framebuffer *buffer)
1004{
apatrick@chromium.org55255022010-09-11 02:12:47 +00001005 if (mState.readFramebuffer.get() == mFramebufferZero.get())
1006 {
1007 mState.readFramebuffer.set(buffer);
1008 }
1009
1010 if (mState.drawFramebuffer.get() == mFramebufferZero.get())
1011 {
1012 mState.drawFramebuffer.set(buffer);
1013 }
1014
1015 mFramebufferZero.set(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016}
1017
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001018void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001019{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001020 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1021 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022}
1023
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001024Fence *Context::getFence(unsigned int handle)
1025{
1026 FenceMap::iterator fence = mFenceMap.find(handle);
1027
1028 if (fence == mFenceMap.end())
1029 {
1030 return NULL;
1031 }
1032 else
1033 {
1034 return fence->second;
1035 }
1036}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001037
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038Buffer *Context::getArrayBuffer()
1039{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001040 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041}
1042
1043Buffer *Context::getElementArrayBuffer()
1044{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001045 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046}
1047
1048Program *Context::getCurrentProgram()
1049{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001050 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001051}
1052
1053Texture2D *Context::getTexture2D()
1054{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001055 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 +00001056 {
apatrick@chromium.org55255022010-09-11 02:12:47 +00001057 return mTexture2DZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058 }
1059
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001060 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061}
1062
1063TextureCubeMap *Context::getTextureCubeMap()
1064{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001065 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 +00001066 {
apatrick@chromium.org55255022010-09-11 02:12:47 +00001067 return mTextureCubeMapZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068 }
1069
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001070 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071}
1072
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001073Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001075 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001076
1077 if (texid == 0)
1078 {
1079 switch (type)
1080 {
1081 default: UNREACHABLE();
apatrick@chromium.org55255022010-09-11 02:12:47 +00001082 case SAMPLER_2D: return mTexture2DZero.get();
1083 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001084 }
1085 }
1086
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001087 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088}
1089
apatrick@chromium.org55255022010-09-11 02:12:47 +00001090Framebuffer *Context::getReadFramebuffer()
1091{
1092 return mState.readFramebuffer.get();
1093}
1094
1095Framebuffer *Context::getDrawFramebuffer()
1096{
1097 return mState.drawFramebuffer.get();
1098}
1099
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001100bool Context::getBooleanv(GLenum pname, GLboolean *params)
1101{
1102 switch (pname)
1103 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001104 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1105 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1106 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001107 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001108 params[0] = mState.colorMaskRed;
1109 params[1] = mState.colorMaskGreen;
1110 params[2] = mState.colorMaskBlue;
1111 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001112 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001113 case GL_CULL_FACE: *params = mState.cullFace;
1114 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1115 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1116 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1117 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1118 case GL_STENCIL_TEST: *params = mState.stencilTest;
1119 case GL_DEPTH_TEST: *params = mState.depthTest;
1120 case GL_BLEND: *params = mState.blend;
1121 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001122 default:
1123 return false;
1124 }
1125
1126 return true;
1127}
1128
1129bool Context::getFloatv(GLenum pname, GLfloat *params)
1130{
1131 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1132 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1133 // GetIntegerv as its native query function. As it would require conversion in any
1134 // case, this should make no difference to the calling application.
1135 switch (pname)
1136 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001137 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1138 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1139 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1140 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1141 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001142 case GL_ALIASED_LINE_WIDTH_RANGE:
1143 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1144 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1145 break;
1146 case GL_ALIASED_POINT_SIZE_RANGE:
1147 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001148 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 +00001149 break;
1150 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001151 params[0] = mState.zNear;
1152 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001153 break;
1154 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001155 params[0] = mState.colorClearValue.red;
1156 params[1] = mState.colorClearValue.green;
1157 params[2] = mState.colorClearValue.blue;
1158 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001159 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001160 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 params[0] = mState.blendColor.red;
1162 params[1] = mState.blendColor.green;
1163 params[2] = mState.blendColor.blue;
1164 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001165 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001166 default:
1167 return false;
1168 }
1169
1170 return true;
1171}
1172
1173bool Context::getIntegerv(GLenum pname, GLint *params)
1174{
1175 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1176 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1177 // GetIntegerv as its native query function. As it would require conversion in any
1178 // case, this should make no difference to the calling application. You may find it in
1179 // Context::getFloatv.
1180 switch (pname)
1181 {
1182 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1183 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1184 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1185 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1186 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1187 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1188 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1189 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001190 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001191 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001192 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1193 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001194 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
apatrick@chromium.org55255022010-09-11 02:12:47 +00001195 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer.id(); break;
1196 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer.id(); break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001197 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001198 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1199 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1200 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1201 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001202 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001203 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1204 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1205 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1206 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1207 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1208 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1209 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1210 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1211 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1212 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1213 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1214 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1215 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1216 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1217 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1218 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1219 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1220 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1221 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1222 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1223 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1224 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1225 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1226 case GL_SUBPIXEL_BITS: *params = 4; break;
1227 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1228 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001229 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1230 {
1231 if (supportsCompressedTextures())
1232 {
1233 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1234 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1235 *params = 2;
1236 }
1237 else
1238 {
1239 *params = 0;
1240 }
1241 }
1242 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001243 case GL_MAX_SAMPLES_ANGLE:
1244 {
1245 GLsizei maxSamples = getMaxSupportedSamples();
1246 if (maxSamples != 0)
1247 {
1248 *params = maxSamples;
1249 }
1250 else
1251 {
1252 return false;
1253 }
1254
1255 break;
1256 }
1257 case GL_SAMPLE_BUFFERS:
1258 case GL_SAMPLES:
1259 {
1260 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1261 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1262 {
1263 switch (pname)
1264 {
1265 case GL_SAMPLE_BUFFERS:
1266 if (framebuffer->getSamples() != 0)
1267 {
1268 *params = 1;
1269 }
1270 else
1271 {
1272 *params = 0;
1273 }
1274 break;
1275 case GL_SAMPLES:
1276 *params = framebuffer->getSamples();
1277 break;
1278 }
1279 }
1280 else
1281 {
1282 *params = 0;
1283 }
1284 }
1285 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001286 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1287 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1288 case GL_MAX_VIEWPORT_DIMS:
1289 {
1290 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1291 params[0] = maxDimension;
1292 params[1] = maxDimension;
1293 }
1294 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001295 case GL_COMPRESSED_TEXTURE_FORMATS:
1296 {
1297 if (supportsCompressedTextures())
1298 {
1299 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1300 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1301 }
1302 }
1303 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001304 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001305 params[0] = mState.viewportX;
1306 params[1] = mState.viewportY;
1307 params[2] = mState.viewportWidth;
1308 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001309 break;
1310 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001311 params[0] = mState.scissorX;
1312 params[1] = mState.scissorY;
1313 params[2] = mState.scissorWidth;
1314 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001315 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001316 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1317 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001318 case GL_RED_BITS:
1319 case GL_GREEN_BITS:
1320 case GL_BLUE_BITS:
1321 case GL_ALPHA_BITS:
1322 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001323 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001324 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1325
1326 if (colorbuffer)
1327 {
1328 switch (pname)
1329 {
1330 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1331 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1332 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1333 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1334 }
1335 }
1336 else
1337 {
1338 *params = 0;
1339 }
1340 }
1341 break;
1342 case GL_DEPTH_BITS:
1343 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001344 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001345 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001346
1347 if (depthbuffer)
1348 {
1349 *params = depthbuffer->getDepthSize();
1350 }
1351 else
1352 {
1353 *params = 0;
1354 }
1355 }
1356 break;
1357 case GL_STENCIL_BITS:
1358 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001359 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001360 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001361
1362 if (stencilbuffer)
1363 {
1364 *params = stencilbuffer->getStencilSize();
1365 }
1366 else
1367 {
1368 *params = 0;
1369 }
1370 }
1371 break;
1372 case GL_TEXTURE_BINDING_2D:
1373 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001374 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001375 {
1376 error(GL_INVALID_OPERATION);
1377 return false;
1378 }
1379
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001380 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001381 }
1382 break;
1383 case GL_TEXTURE_BINDING_CUBE_MAP:
1384 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001385 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001386 {
1387 error(GL_INVALID_OPERATION);
1388 return false;
1389 }
1390
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001391 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001392 }
1393 break;
1394 default:
1395 return false;
1396 }
1397
1398 return true;
1399}
1400
1401bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1402{
1403 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1404 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1405 // to the fact that it is stored internally as a float, and so would require conversion
1406 // if returned from Context::getIntegerv. Since this conversion is already implemented
1407 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1408 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1409 // application.
1410 switch (pname)
1411 {
1412 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1413 case GL_SHADER_BINARY_FORMATS:
1414 {
1415 *type = GL_INT;
1416 *numParams = 0;
1417 }
1418 break;
1419 case GL_MAX_VERTEX_ATTRIBS:
1420 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1421 case GL_MAX_VARYING_VECTORS:
1422 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1423 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1424 case GL_MAX_TEXTURE_IMAGE_UNITS:
1425 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1426 case GL_MAX_RENDERBUFFER_SIZE:
1427 case GL_NUM_SHADER_BINARY_FORMATS:
1428 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1429 case GL_ARRAY_BUFFER_BINDING:
1430 case GL_FRAMEBUFFER_BINDING:
1431 case GL_RENDERBUFFER_BINDING:
1432 case GL_CURRENT_PROGRAM:
1433 case GL_PACK_ALIGNMENT:
1434 case GL_UNPACK_ALIGNMENT:
1435 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001436 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001437 case GL_RED_BITS:
1438 case GL_GREEN_BITS:
1439 case GL_BLUE_BITS:
1440 case GL_ALPHA_BITS:
1441 case GL_DEPTH_BITS:
1442 case GL_STENCIL_BITS:
1443 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1444 case GL_CULL_FACE_MODE:
1445 case GL_FRONT_FACE:
1446 case GL_ACTIVE_TEXTURE:
1447 case GL_STENCIL_FUNC:
1448 case GL_STENCIL_VALUE_MASK:
1449 case GL_STENCIL_REF:
1450 case GL_STENCIL_FAIL:
1451 case GL_STENCIL_PASS_DEPTH_FAIL:
1452 case GL_STENCIL_PASS_DEPTH_PASS:
1453 case GL_STENCIL_BACK_FUNC:
1454 case GL_STENCIL_BACK_VALUE_MASK:
1455 case GL_STENCIL_BACK_REF:
1456 case GL_STENCIL_BACK_FAIL:
1457 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1458 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1459 case GL_DEPTH_FUNC:
1460 case GL_BLEND_SRC_RGB:
1461 case GL_BLEND_SRC_ALPHA:
1462 case GL_BLEND_DST_RGB:
1463 case GL_BLEND_DST_ALPHA:
1464 case GL_BLEND_EQUATION_RGB:
1465 case GL_BLEND_EQUATION_ALPHA:
1466 case GL_STENCIL_WRITEMASK:
1467 case GL_STENCIL_BACK_WRITEMASK:
1468 case GL_STENCIL_CLEAR_VALUE:
1469 case GL_SUBPIXEL_BITS:
1470 case GL_MAX_TEXTURE_SIZE:
1471 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1472 case GL_SAMPLE_BUFFERS:
1473 case GL_SAMPLES:
1474 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1475 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1476 case GL_TEXTURE_BINDING_2D:
1477 case GL_TEXTURE_BINDING_CUBE_MAP:
1478 {
1479 *type = GL_INT;
1480 *numParams = 1;
1481 }
1482 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001483 case GL_MAX_SAMPLES_ANGLE:
1484 {
1485 if (getMaxSupportedSamples() != 0)
1486 {
1487 *type = GL_INT;
1488 *numParams = 1;
1489 }
1490 else
1491 {
1492 return false;
1493 }
1494 }
1495 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001496 case GL_MAX_VIEWPORT_DIMS:
1497 {
1498 *type = GL_INT;
1499 *numParams = 2;
1500 }
1501 break;
1502 case GL_VIEWPORT:
1503 case GL_SCISSOR_BOX:
1504 {
1505 *type = GL_INT;
1506 *numParams = 4;
1507 }
1508 break;
1509 case GL_SHADER_COMPILER:
1510 case GL_SAMPLE_COVERAGE_INVERT:
1511 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001512 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1513 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1514 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1515 case GL_SAMPLE_COVERAGE:
1516 case GL_SCISSOR_TEST:
1517 case GL_STENCIL_TEST:
1518 case GL_DEPTH_TEST:
1519 case GL_BLEND:
1520 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001521 {
1522 *type = GL_BOOL;
1523 *numParams = 1;
1524 }
1525 break;
1526 case GL_COLOR_WRITEMASK:
1527 {
1528 *type = GL_BOOL;
1529 *numParams = 4;
1530 }
1531 break;
1532 case GL_POLYGON_OFFSET_FACTOR:
1533 case GL_POLYGON_OFFSET_UNITS:
1534 case GL_SAMPLE_COVERAGE_VALUE:
1535 case GL_DEPTH_CLEAR_VALUE:
1536 case GL_LINE_WIDTH:
1537 {
1538 *type = GL_FLOAT;
1539 *numParams = 1;
1540 }
1541 break;
1542 case GL_ALIASED_LINE_WIDTH_RANGE:
1543 case GL_ALIASED_POINT_SIZE_RANGE:
1544 case GL_DEPTH_RANGE:
1545 {
1546 *type = GL_FLOAT;
1547 *numParams = 2;
1548 }
1549 break;
1550 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001551 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001552 {
1553 *type = GL_FLOAT;
1554 *numParams = 4;
1555 }
1556 break;
1557 default:
1558 return false;
1559 }
1560
1561 return true;
1562}
1563
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001564// Applies the render target surface, depth stencil surface, viewport rectangle and
1565// scissor rectangle to the Direct3D 9 device
1566bool Context::applyRenderTarget(bool ignoreViewport)
1567{
1568 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001569
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001570 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001571
1572 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1573 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001574 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1575
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001576 return false;
1577 }
1578
1579 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001580
1581 if (!renderTarget)
1582 {
1583 return false; // Context must be lost
1584 }
1585
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001586 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001588 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1589 if (renderTargetSerial != mAppliedRenderTargetSerial)
1590 {
1591 device->SetRenderTarget(0, renderTarget);
1592 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001593 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 +00001594 }
1595
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001596 unsigned int depthbufferSerial = 0;
1597 unsigned int stencilbufferSerial = 0;
1598 if (framebufferObject->getDepthbufferType() != GL_NONE)
1599 {
1600 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1601 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1602 }
1603 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1604 {
1605 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1606 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1607 }
1608
1609 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001610 stencilbufferSerial != mAppliedStencilbufferSerial ||
1611 mDepthStencilDirty)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001612 {
1613 device->SetDepthStencilSurface(depthStencil);
1614 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001615 mAppliedStencilbufferSerial = stencilbufferSerial;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001616 mDepthStencilDirty = false;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001617 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618
1619 D3DVIEWPORT9 viewport;
1620 D3DSURFACE_DESC desc;
1621 renderTarget->GetDesc(&desc);
1622
1623 if (ignoreViewport)
1624 {
1625 viewport.X = 0;
1626 viewport.Y = 0;
1627 viewport.Width = desc.Width;
1628 viewport.Height = desc.Height;
1629 viewport.MinZ = 0.0f;
1630 viewport.MaxZ = 1.0f;
1631 }
1632 else
1633 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001634 viewport.X = std::max(mState.viewportX, 0);
1635 viewport.Y = std::max(mState.viewportY, 0);
1636 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1637 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1638 viewport.MinZ = clamp01(mState.zNear);
1639 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640 }
1641
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001642 if (viewport.Width <= 0 || viewport.Height <= 0)
1643 {
1644 return false; // Nothing to render
1645 }
1646
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647 device->SetViewport(&viewport);
1648
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001649 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001651 if (mState.scissorTest)
1652 {
1653 RECT rect = {mState.scissorX,
1654 mState.scissorY,
1655 mState.scissorX + mState.scissorWidth,
1656 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001657 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1658 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001659 device->SetScissorRect(&rect);
1660 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1661 }
1662 else
1663 {
1664 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1665 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001666
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001667 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 }
1669
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001670 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 Program *programObject = getCurrentProgram();
1673
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001674 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001675 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001677
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001678 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001679 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1680 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1681 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001682 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1683
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001684 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001685 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001686 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1687
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001688 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001689 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001690
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001691 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001692 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001693
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001694 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001696 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 }
1698
1699 return true;
1700}
1701
1702// 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 +00001703void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704{
1705 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001706 Program *programObject = getCurrentProgram();
1707
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001708 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001709 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001710 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001712 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001713 GLint alwaysFront = !isTriangleMode(drawMode);
1714 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1715
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001716 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001717
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001718 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001720 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001722 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723 }
1724 else
1725 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001726 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 }
1728
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001729 mCullStateDirty = false;
1730 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001732 if (mDepthStateDirty)
1733 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001734 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001736 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1737 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 }
1739 else
1740 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001743
1744 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745 }
1746
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001749 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001750 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1752
1753 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1754 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1755 {
1756 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1757 }
1758 else
1759 {
1760 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1761 unorm<8>(mState.blendColor.alpha),
1762 unorm<8>(mState.blendColor.alpha),
1763 unorm<8>(mState.blendColor.alpha)));
1764 }
1765
1766 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1767 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1768 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1769
1770 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1771 mState.destBlendRGB != mState.destBlendAlpha ||
1772 mState.blendEquationRGB != mState.blendEquationAlpha)
1773 {
1774 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1775
1776 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1777 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1778 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1779
1780 }
1781 else
1782 {
1783 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1784 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001785 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001786 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001787 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001788 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001789 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001790
1791 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001792 }
1793
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001794 if (mStencilStateDirty || mFrontFaceDirty)
1795 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001796 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001797 {
1798 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1799 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1800
1801 // FIXME: Unsupported by D3D9
1802 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1803 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1804 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1805 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1806 mState.stencilRef != mState.stencilBackRef ||
1807 mState.stencilMask != mState.stencilBackMask)
1808 {
1809 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1810 return error(GL_INVALID_OPERATION);
1811 }
1812
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001813 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001814 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001815 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1816
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001817 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1818 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1819 es2dx::ConvertComparison(mState.stencilFunc));
1820
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001821 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 +00001822 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1823
1824 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1825 es2dx::ConvertStencilOp(mState.stencilFail));
1826 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1827 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1828 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1829 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1830
1831 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1832 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1833 es2dx::ConvertComparison(mState.stencilBackFunc));
1834
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001835 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 +00001836 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1837
1838 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1839 es2dx::ConvertStencilOp(mState.stencilBackFail));
1840 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1841 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1842 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1843 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1844 }
1845 else
1846 {
1847 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1848 }
1849
1850 mStencilStateDirty = false;
1851 }
1852
1853 if (mMaskStateDirty)
1854 {
1855 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1856 mState.colorMaskBlue, mState.colorMaskAlpha));
1857 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1858
1859 mMaskStateDirty = false;
1860 }
1861
1862 if (mPolygonOffsetStateDirty)
1863 {
1864 if (mState.polygonOffsetFill)
1865 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001866 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001867 if (depthbuffer)
1868 {
1869 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1870 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1871 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1872 }
1873 }
1874 else
1875 {
1876 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1877 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1878 }
1879
1880 mPolygonOffsetStateDirty = false;
1881 }
1882
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001883 if (framebufferObject->isMultisample() && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001884 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001885 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001886 {
1887 FIXME("Sample alpha to coverage is unimplemented.");
1888 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001890 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001891 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001892 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1893 unsigned int mask = 0;
1894 if (mState.sampleCoverageValue != 0)
1895 {
1896 float threshold = 0.5f;
1897
1898 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1899 {
1900 mask <<= 1;
1901
1902 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1903 {
1904 threshold += 1.0f;
1905 mask |= 1;
1906 }
1907 }
1908 }
1909
1910 if (mState.sampleCoverageInvert)
1911 {
1912 mask = ~mask;
1913 }
1914
1915 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1916 }
1917 else
1918 {
1919 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001920 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001921
1922 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923 }
1924
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001925 if (mDitherStateDirty)
1926 {
1927 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1928
1929 mDitherStateDirty = false;
1930 }
1931
1932 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933}
1934
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001935// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001936void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001938 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001940 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001942 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943 }
1944 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001945}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001947GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001948{
1949 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1950
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001951 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001952 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001953 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001954 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001955 }
1956
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001957 lookupAttributeMapping(translated);
1958
1959 mBufferBackEnd->setupAttributesPreDraw(translated);
1960
1961 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1962 {
1963 if (translated[i].enabled && translated[i].nonArray)
1964 {
1965 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1966 if (err != GL_NO_ERROR)
1967 {
1968 return err;
1969 }
1970
1971 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1972
1973 *useIndexing = true;
1974 return GL_NO_ERROR;
1975 }
1976 }
1977
1978 *useIndexing = false;
1979 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001980}
1981
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001982GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001983{
1984 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1985
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001986 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001987
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001988 if (err == GL_NO_ERROR)
1989 {
1990 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001991
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001992 mBufferBackEnd->setupAttributesPreDraw(translated);
1993 }
1994
1995 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001996}
1997
1998// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001999GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002000{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002001 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002002
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002003 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002004 {
2005 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2006 }
2007
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002008 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009}
2010
2011// Applies the shaders and shader constants to the Direct3D 9 device
2012void Context::applyShaders()
2013{
2014 IDirect3DDevice9 *device = getDevice();
2015 Program *programObject = getCurrentProgram();
2016 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2017 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2018
2019 device->SetVertexShader(vertexShader);
2020 device->SetPixelShader(pixelShader);
2021
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002022 if (programObject->getSerial() != mAppliedProgram)
2023 {
2024 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002025 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002026 mAppliedProgram = programObject->getSerial();
2027 }
2028
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029 programObject->applyUniforms();
2030}
2031
2032// Applies the textures and sampler states to the Direct3D 9 device
2033void Context::applyTextures()
2034{
2035 IDirect3DDevice9 *device = getDevice();
2036 Program *programObject = getCurrentProgram();
2037
2038 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2039 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002040 int textureUnit = programObject->getSamplerMapping(sampler);
2041 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002042 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002043 SamplerType textureType = programObject->getSamplerType(sampler);
2044
2045 Texture *texture = getSamplerTexture(textureUnit, textureType);
2046
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002047 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002048 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002049 if (texture->isComplete())
2050 {
2051 GLenum wrapS = texture->getWrapS();
2052 GLenum wrapT = texture->getWrapT();
2053 GLenum minFilter = texture->getMinFilter();
2054 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002056 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2057 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002059 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2060 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2061 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2062 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2063 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002065 device->SetTexture(sampler, texture->getTexture());
2066 }
2067 else
2068 {
2069 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2070 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002071 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002072
2073 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002075 else
2076 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002077 if (programObject->isSamplerDirty(sampler))
2078 {
2079 device->SetTexture(sampler, NULL);
2080 programObject->setSamplerDirty(sampler, false);
2081 }
2082 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 }
2084}
2085
2086void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2087{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002088 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002089
2090 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2091 {
2092 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2093 }
2094
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002095 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2096 {
2097 return error(GL_INVALID_OPERATION);
2098 }
2099
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002101
2102 if (!renderTarget)
2103 {
2104 return; // Context must be lost, return silently
2105 }
2106
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 IDirect3DDevice9 *device = getDevice();
2108
2109 D3DSURFACE_DESC desc;
2110 renderTarget->GetDesc(&desc);
2111
2112 IDirect3DSurface9 *systemSurface;
2113 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2114
2115 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2116 {
2117 return error(GL_OUT_OF_MEMORY);
2118 }
2119
2120 ASSERT(SUCCEEDED(result));
2121
2122 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2123 {
2124 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2125 }
2126
2127 result = device->GetRenderTargetData(renderTarget, systemSurface);
2128
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002129 if (FAILED(result))
2130 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131 systemSurface->Release();
2132
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002133 switch (result)
2134 {
2135 case D3DERR_DRIVERINTERNALERROR:
2136 case D3DERR_DEVICELOST:
2137 return error(GL_OUT_OF_MEMORY);
2138 default:
2139 UNREACHABLE();
2140 return; // No sensible error to generate
2141 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142 }
2143
2144 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002145 RECT rect = {std::max(x, 0),
2146 std::max(y, 0),
2147 std::min(x + width, (int)desc.Width),
2148 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149
2150 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2151
2152 if (FAILED(result))
2153 {
2154 UNREACHABLE();
2155 systemSurface->Release();
2156
2157 return; // No sensible error to generate
2158 }
2159
2160 unsigned char *source = (unsigned char*)lock.pBits;
2161 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002162 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002164 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002165
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 for (int j = 0; j < rect.bottom - rect.top; j++)
2167 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002168 if (desc.Format == D3DFMT_A8R8G8B8 &&
2169 format == GL_BGRA_EXT &&
2170 type == GL_UNSIGNED_BYTE)
2171 {
2172 // Fast path for EXT_read_format_bgra, given
2173 // an RGBA source buffer. Note that buffers with no
2174 // alpha go through the slow path below.
2175 memcpy(dest + j * outputPitch,
2176 source + j * lock.Pitch,
2177 (rect.right - rect.left) * 4);
2178 continue;
2179 }
2180
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 for (int i = 0; i < rect.right - rect.left; i++)
2182 {
2183 float r;
2184 float g;
2185 float b;
2186 float a;
2187
2188 switch (desc.Format)
2189 {
2190 case D3DFMT_R5G6B5:
2191 {
2192 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2193
2194 a = 1.0f;
2195 b = (rgb & 0x001F) * (1.0f / 0x001F);
2196 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2197 r = (rgb & 0xF800) * (1.0f / 0xF800);
2198 }
2199 break;
2200 case D3DFMT_X1R5G5B5:
2201 {
2202 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2203
2204 a = 1.0f;
2205 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2206 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2207 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2208 }
2209 break;
2210 case D3DFMT_A1R5G5B5:
2211 {
2212 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2213
2214 a = (argb & 0x8000) ? 1.0f : 0.0f;
2215 b = (argb & 0x001F) * (1.0f / 0x001F);
2216 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2217 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2218 }
2219 break;
2220 case D3DFMT_A8R8G8B8:
2221 {
2222 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2223
2224 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2225 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2226 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2227 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2228 }
2229 break;
2230 case D3DFMT_X8R8G8B8:
2231 {
2232 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2233
2234 a = 1.0f;
2235 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2236 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2237 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2238 }
2239 break;
2240 case D3DFMT_A2R10G10B10:
2241 {
2242 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2243
2244 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2245 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2246 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2247 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2248 }
2249 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002250 case D3DFMT_A32B32G32R32F:
2251 {
2252 // float formats in D3D are stored rgba, rather than the other way round
2253 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2254 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2255 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2256 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2257 }
2258 break;
2259 case D3DFMT_A16B16G16R16F:
2260 {
2261 // float formats in D3D are stored rgba, rather than the other way round
2262 float abgr[4];
2263
2264 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2265
2266 a = abgr[3];
2267 b = abgr[2];
2268 g = abgr[1];
2269 r = abgr[0];
2270 }
2271 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 default:
2273 UNIMPLEMENTED(); // FIXME
2274 UNREACHABLE();
2275 }
2276
2277 switch (format)
2278 {
2279 case GL_RGBA:
2280 switch (type)
2281 {
2282 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002283 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2284 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2285 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2286 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 break;
2288 default: UNREACHABLE();
2289 }
2290 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002291 case GL_BGRA_EXT:
2292 switch (type)
2293 {
2294 case GL_UNSIGNED_BYTE:
2295 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2296 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2297 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2298 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2299 break;
2300 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2301 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2302 // this type is packed as follows:
2303 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2304 // --------------------------------------------------------------------------------
2305 // | 4th | 3rd | 2nd | 1st component |
2306 // --------------------------------------------------------------------------------
2307 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2308 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2309 ((unsigned short)(15 * a + 0.5f) << 12)|
2310 ((unsigned short)(15 * r + 0.5f) << 8) |
2311 ((unsigned short)(15 * g + 0.5f) << 4) |
2312 ((unsigned short)(15 * b + 0.5f) << 0);
2313 break;
2314 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2315 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2316 // this type is packed as follows:
2317 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2318 // --------------------------------------------------------------------------------
2319 // | 4th | 3rd | 2nd | 1st component |
2320 // --------------------------------------------------------------------------------
2321 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2322 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2323 ((unsigned short)( a + 0.5f) << 15) |
2324 ((unsigned short)(31 * r + 0.5f) << 10) |
2325 ((unsigned short)(31 * g + 0.5f) << 5) |
2326 ((unsigned short)(31 * b + 0.5f) << 0);
2327 break;
2328 default: UNREACHABLE();
2329 }
2330 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002331 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 switch (type)
2333 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002334 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002335 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2336 ((unsigned short)(31 * b + 0.5f) << 0) |
2337 ((unsigned short)(63 * g + 0.5f) << 5) |
2338 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339 break;
2340 default: UNREACHABLE();
2341 }
2342 break;
2343 default: UNREACHABLE();
2344 }
2345 }
2346 }
2347
2348 systemSurface->UnlockRect();
2349
2350 systemSurface->Release();
2351}
2352
2353void Context::clear(GLbitfield mask)
2354{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002355 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002356
2357 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2358 {
2359 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2360
2361 return;
2362 }
2363
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002364 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002365 IDirect3DDevice9 *device = getDevice();
2366 DWORD flags = 0;
2367
2368 if (mask & GL_COLOR_BUFFER_BIT)
2369 {
2370 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002371
2372 if (framebufferObject->getColorbufferType() != GL_NONE)
2373 {
2374 flags |= D3DCLEAR_TARGET;
2375 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 }
2377
2378 if (mask & GL_DEPTH_BUFFER_BIT)
2379 {
2380 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002381 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002382 {
2383 flags |= D3DCLEAR_ZBUFFER;
2384 }
2385 }
2386
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 GLuint stencilUnmasked = 0x0;
2388
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002389 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002390 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002392 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002394 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2395 D3DSURFACE_DESC desc;
2396 depthStencil->GetDesc(&desc);
2397
2398 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2399 stencilUnmasked = (0x1 << stencilSize) - 1;
2400
2401 if (stencilUnmasked != 0x0)
2402 {
2403 flags |= D3DCLEAR_STENCIL;
2404 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 }
2406 }
2407
2408 if (mask != 0)
2409 {
2410 return error(GL_INVALID_VALUE);
2411 }
2412
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002413 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2414 {
2415 return;
2416 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002418 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2419 unorm<8>(mState.colorClearValue.red),
2420 unorm<8>(mState.colorClearValue.green),
2421 unorm<8>(mState.colorClearValue.blue));
2422 float depth = clamp01(mState.depthClearValue);
2423 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424
2425 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2426
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002427 if (!renderTarget)
2428 {
2429 return; // Context must be lost, return silently
2430 }
2431
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 D3DSURFACE_DESC desc;
2433 renderTarget->GetDesc(&desc);
2434
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002435 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436
2437 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002438 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002440 !(mState.colorMaskRed && mState.colorMaskGreen &&
2441 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442
2443 if (needMaskedColorClear || needMaskedStencilClear)
2444 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002445 // State which is altered in all paths from this point to the clear call is saved.
2446 // State which is altered in only some paths will be flagged dirty in the case that
2447 // that path is taken.
2448 HRESULT hr;
2449 if (mMaskedClearSavedState == NULL)
2450 {
2451 hr = device->BeginStateBlock();
2452 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2453
2454 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2455 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2456 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2457 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2458 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2459 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2460 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2461 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2462 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2463 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2464 device->SetPixelShader(NULL);
2465 device->SetVertexShader(NULL);
2466 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2467 device->SetStreamSourceFreq(0, 1);
2468
2469 hr = device->EndStateBlock(&mMaskedClearSavedState);
2470 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2471 }
2472
2473 ASSERT(mMaskedClearSavedState != NULL);
2474
2475 if (mMaskedClearSavedState != NULL)
2476 {
2477 hr = mMaskedClearSavedState->Capture();
2478 ASSERT(SUCCEEDED(hr));
2479 }
2480
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2482 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2483 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2484 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2485 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2486 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2487 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2488 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2489
2490 if (flags & D3DCLEAR_TARGET)
2491 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002492 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2493 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2494 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2495 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496 }
2497 else
2498 {
2499 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2500 }
2501
2502 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2503 {
2504 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2505 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2506 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2507 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002508 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002509 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002510 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2511 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002512 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002513 }
2514 else
2515 {
2516 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2517 }
2518
2519 device->SetPixelShader(NULL);
2520 device->SetVertexShader(NULL);
2521 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002522 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002524 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525 {
2526 float x, y, z, w;
2527 D3DCOLOR diffuse;
2528 };
2529
2530 Vertex quad[4];
2531 quad[0].x = 0.0f;
2532 quad[0].y = (float)desc.Height;
2533 quad[0].z = 0.0f;
2534 quad[0].w = 1.0f;
2535 quad[0].diffuse = color;
2536
2537 quad[1].x = (float)desc.Width;
2538 quad[1].y = (float)desc.Height;
2539 quad[1].z = 0.0f;
2540 quad[1].w = 1.0f;
2541 quad[1].diffuse = color;
2542
2543 quad[2].x = 0.0f;
2544 quad[2].y = 0.0f;
2545 quad[2].z = 0.0f;
2546 quad[2].w = 1.0f;
2547 quad[2].diffuse = color;
2548
2549 quad[3].x = (float)desc.Width;
2550 quad[3].y = 0.0f;
2551 quad[3].z = 0.0f;
2552 quad[3].w = 1.0f;
2553 quad[3].diffuse = color;
2554
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002555 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557
2558 if (flags & D3DCLEAR_ZBUFFER)
2559 {
2560 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2561 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2562 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2563 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002564
2565 if (mMaskedClearSavedState != NULL)
2566 {
2567 mMaskedClearSavedState->Apply();
2568 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002570 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002571 {
2572 device->Clear(0, NULL, flags, color, depth, stencil);
2573 }
2574}
2575
2576void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2577{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002578 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579 {
2580 return error(GL_INVALID_OPERATION);
2581 }
2582
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002583 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584 IDirect3DDevice9 *device = getDevice();
2585 D3DPRIMITIVETYPE primitiveType;
2586 int primitiveCount;
2587
2588 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2589 return error(GL_INVALID_ENUM);
2590
2591 if (primitiveCount <= 0)
2592 {
2593 return;
2594 }
2595
2596 if (!applyRenderTarget(false))
2597 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002598 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599 }
2600
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002601 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002602
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002603 TranslatedIndexData indexInfo;
2604 bool useIndexing;
2605 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002606 if (err != GL_NO_ERROR)
2607 {
2608 return error(err);
2609 }
2610
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611 applyShaders();
2612 applyTextures();
2613
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002614 if (!getCurrentProgram()->validateSamplers())
2615 {
2616 return error(GL_INVALID_OPERATION);
2617 }
2618
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002619 if (!cullSkipsDraw(mode))
2620 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002621 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002622 if (useIndexing)
2623 {
2624 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2625 }
2626 else
2627 {
2628 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2629 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002630 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631}
2632
2633void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2634{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002635 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002636 {
2637 return error(GL_INVALID_OPERATION);
2638 }
2639
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002640 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641 {
2642 return error(GL_INVALID_OPERATION);
2643 }
2644
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002645 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646 IDirect3DDevice9 *device = getDevice();
2647 D3DPRIMITIVETYPE primitiveType;
2648 int primitiveCount;
2649
2650 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2651 return error(GL_INVALID_ENUM);
2652
2653 if (primitiveCount <= 0)
2654 {
2655 return;
2656 }
2657
2658 if (!applyRenderTarget(false))
2659 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002660 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661 }
2662
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002663 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002664
2665 TranslatedIndexData indexInfo;
2666 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2667 if (err != GL_NO_ERROR)
2668 {
2669 return error(err);
2670 }
2671
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002672 err = applyVertexBuffer(indexInfo);
2673 if (err != GL_NO_ERROR)
2674 {
2675 return error(err);
2676 }
2677
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002678 applyShaders();
2679 applyTextures();
2680
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002681 if (!getCurrentProgram()->validateSamplers())
2682 {
2683 return error(GL_INVALID_OPERATION);
2684 }
2685
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002686 if (!cullSkipsDraw(mode))
2687 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002688 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002689 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 +00002690 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002691}
2692
2693void Context::finish()
2694{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002695 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002696 IDirect3DDevice9 *device = getDevice();
2697 IDirect3DQuery9 *occlusionQuery = NULL;
2698
2699 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2700
2701 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2702 {
2703 return error(GL_OUT_OF_MEMORY);
2704 }
2705
2706 ASSERT(SUCCEEDED(result));
2707
2708 if (occlusionQuery)
2709 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002710 IDirect3DStateBlock9 *savedState = NULL;
2711 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2712
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002713 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2714 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715
2716 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002717 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002718 device->SetPixelShader(NULL);
2719 device->SetVertexShader(NULL);
2720 device->SetFVF(D3DFVF_XYZRHW);
2721 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002722 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002723 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002725 result = occlusionQuery->Issue(D3DISSUE_END);
2726 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002727
2728 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2729 {
2730 // Keep polling, but allow other threads to do something useful first
2731 Sleep(0);
2732 }
2733
2734 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002735
2736 if (savedState)
2737 {
2738 savedState->Apply();
2739 savedState->Release();
2740 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002741 }
2742}
2743
2744void Context::flush()
2745{
2746 IDirect3DDevice9 *device = getDevice();
2747 IDirect3DQuery9 *eventQuery = NULL;
2748
2749 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2750
2751 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2752 {
2753 return error(GL_OUT_OF_MEMORY);
2754 }
2755
2756 ASSERT(SUCCEEDED(result));
2757
2758 if (eventQuery)
2759 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002760 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2761 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002763 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002765
2766 if (result == D3DERR_DEVICELOST)
2767 {
2768 error(GL_OUT_OF_MEMORY);
2769 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002770 }
2771}
2772
2773void Context::recordInvalidEnum()
2774{
2775 mInvalidEnum = true;
2776}
2777
2778void Context::recordInvalidValue()
2779{
2780 mInvalidValue = true;
2781}
2782
2783void Context::recordInvalidOperation()
2784{
2785 mInvalidOperation = true;
2786}
2787
2788void Context::recordOutOfMemory()
2789{
2790 mOutOfMemory = true;
2791}
2792
2793void Context::recordInvalidFramebufferOperation()
2794{
2795 mInvalidFramebufferOperation = true;
2796}
2797
2798// Get one of the recorded errors and clear its flag, if any.
2799// [OpenGL ES 2.0.24] section 2.5 page 13.
2800GLenum Context::getError()
2801{
2802 if (mInvalidEnum)
2803 {
2804 mInvalidEnum = false;
2805
2806 return GL_INVALID_ENUM;
2807 }
2808
2809 if (mInvalidValue)
2810 {
2811 mInvalidValue = false;
2812
2813 return GL_INVALID_VALUE;
2814 }
2815
2816 if (mInvalidOperation)
2817 {
2818 mInvalidOperation = false;
2819
2820 return GL_INVALID_OPERATION;
2821 }
2822
2823 if (mOutOfMemory)
2824 {
2825 mOutOfMemory = false;
2826
2827 return GL_OUT_OF_MEMORY;
2828 }
2829
2830 if (mInvalidFramebufferOperation)
2831 {
2832 mInvalidFramebufferOperation = false;
2833
2834 return GL_INVALID_FRAMEBUFFER_OPERATION;
2835 }
2836
2837 return GL_NO_ERROR;
2838}
2839
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002840bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002841{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002842 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002843}
2844
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002845int Context::getMaxSupportedSamples() const
2846{
2847 return mMaxSupportedSamples;
2848}
2849
2850int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2851{
2852 if (requested == 0)
2853 {
2854 return requested;
2855 }
2856
2857 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2858 if (itr == mMultiSampleSupport.end())
2859 {
2860 return -1;
2861 }
2862
2863 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2864 {
2865 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2866 {
2867 return i;
2868 }
2869 }
2870
2871 return -1;
2872}
2873
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002874bool Context::supportsEventQueries() const
2875{
2876 return mSupportsEventQueries;
2877}
2878
daniel@transgaming.com01868132010-08-24 19:21:17 +00002879bool Context::supportsCompressedTextures() const
2880{
2881 return mSupportsCompressedTextures;
2882}
2883
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002884bool Context::supportsFloatTextures() const
2885{
2886 return mSupportsFloatTextures;
2887}
2888
2889bool Context::supportsFloatLinearFilter() const
2890{
2891 return mSupportsFloatLinearFilter;
2892}
2893
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002894bool Context::supportsFloatRenderableTextures() const
2895{
2896 return mSupportsFloatRenderableTextures;
2897}
2898
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002899bool Context::supportsHalfFloatTextures() const
2900{
2901 return mSupportsHalfFloatTextures;
2902}
2903
2904bool Context::supportsHalfFloatLinearFilter() const
2905{
2906 return mSupportsHalfFloatLinearFilter;
2907}
2908
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002909bool Context::supportsHalfFloatRenderableTextures() const
2910{
2911 return mSupportsHalfFloatRenderableTextures;
2912}
2913
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002914void Context::detachBuffer(GLuint buffer)
2915{
2916 // [OpenGL ES 2.0.24] section 2.9 page 22:
2917 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2918 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2919
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002920 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002921 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002922 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002923 }
2924
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002925 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002926 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002927 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002928 }
2929
2930 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2931 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002932 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002933 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002934 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002935 }
2936 }
2937}
2938
2939void Context::detachTexture(GLuint texture)
2940{
2941 // [OpenGL ES 2.0.24] section 3.8 page 84:
2942 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2943 // rebound to texture object zero
2944
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002945 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002946 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002947 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002948 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002949 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002950 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002951 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002952 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002953 }
2954 }
2955
2956 // [OpenGL ES 2.0.24] section 4.4 page 112:
2957 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2958 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2959 // image was attached in the currently bound framebuffer.
2960
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002961 Framebuffer *readFramebuffer = getReadFramebuffer();
2962 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002963
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002964 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002965 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002966 readFramebuffer->detachTexture(texture);
2967 }
2968
2969 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2970 {
2971 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002972 }
2973}
2974
2975void Context::detachFramebuffer(GLuint framebuffer)
2976{
2977 // [OpenGL ES 2.0.24] section 4.4 page 107:
2978 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2979 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2980
apatrick@chromium.org55255022010-09-11 02:12:47 +00002981 if (mState.readFramebuffer.id() == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002982 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002983 bindReadFramebuffer(0);
2984 }
2985
apatrick@chromium.org55255022010-09-11 02:12:47 +00002986 if (mState.drawFramebuffer.id() == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002987 {
2988 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002989 }
2990}
2991
2992void Context::detachRenderbuffer(GLuint renderbuffer)
2993{
2994 // [OpenGL ES 2.0.24] section 4.4 page 109:
2995 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2996 // had been executed with the target RENDERBUFFER and name of zero.
2997
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002998 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002999 {
3000 bindRenderbuffer(0);
3001 }
3002
3003 // [OpenGL ES 2.0.24] section 4.4 page 111:
3004 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3005 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3006 // point to which this image was attached in the currently bound framebuffer.
3007
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003008 Framebuffer *readFramebuffer = getReadFramebuffer();
3009 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003010
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003011 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003012 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003013 readFramebuffer->detachRenderbuffer(renderbuffer);
3014 }
3015
3016 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3017 {
3018 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003019 }
3020}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003021
3022Texture *Context::getIncompleteTexture(SamplerType type)
3023{
apatrick@chromium.org55255022010-09-11 02:12:47 +00003024 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003025
3026 if (t == NULL)
3027 {
3028 static const GLubyte color[] = { 0, 0, 0, 255 };
3029
3030 switch (type)
3031 {
3032 default:
3033 UNREACHABLE();
3034 // default falls through to SAMPLER_2D
3035
3036 case SAMPLER_2D:
3037 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003038 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003039 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003040 t = incomplete2d;
3041 }
3042 break;
3043
3044 case SAMPLER_CUBE:
3045 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003046 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003047
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003048 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3049 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3050 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3051 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3052 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3053 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003054
3055 t = incompleteCube;
3056 }
3057 break;
3058 }
3059
apatrick@chromium.org55255022010-09-11 02:12:47 +00003060 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003061 }
3062
3063 return t;
3064}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003065
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003066bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003067{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003068 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003069}
3070
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003071bool Context::isTriangleMode(GLenum drawMode)
3072{
3073 switch (drawMode)
3074 {
3075 case GL_TRIANGLES:
3076 case GL_TRIANGLE_FAN:
3077 case GL_TRIANGLE_STRIP:
3078 return true;
3079 case GL_POINTS:
3080 case GL_LINES:
3081 case GL_LINE_LOOP:
3082 case GL_LINE_STRIP:
3083 return false;
3084 default: UNREACHABLE();
3085 }
3086
3087 return false;
3088}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003089
3090void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3091{
3092 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3093
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003094 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3095 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3096 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3097 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003098
3099 mVertexDataManager->dirtyCurrentValues();
3100}
3101
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003102void Context::initExtensionString()
3103{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003104 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003105 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3106 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003107 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003108 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003109 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003110
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003111 if (supportsEventQueries())
3112 {
3113 mExtensionString += "GL_NV_fence ";
3114 }
3115
daniel@transgaming.com01868132010-08-24 19:21:17 +00003116 if (supportsCompressedTextures())
3117 {
3118 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3119 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003120
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003121 if (supportsFloatTextures())
3122 {
3123 mExtensionString += "GL_OES_texture_float ";
3124 }
3125
3126 if (supportsHalfFloatTextures())
3127 {
3128 mExtensionString += "GL_OES_texture_half_float ";
3129 }
3130
3131 if (supportsFloatLinearFilter())
3132 {
3133 mExtensionString += "GL_OES_texture_float_linear ";
3134 }
3135
3136 if (supportsHalfFloatLinearFilter())
3137 {
3138 mExtensionString += "GL_OES_texture_half_float_linear ";
3139 }
3140
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003141 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003142 {
3143 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3144 }
3145
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003146 if (mBufferBackEnd->supportIntIndices())
3147 {
3148 mExtensionString += "GL_OES_element_index_uint ";
3149 }
3150
3151 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3152 if (end != std::string::npos)
3153 {
3154 mExtensionString.resize(end+1);
3155 }
3156}
3157
3158const char *Context::getExtensionString() const
3159{
3160 return mExtensionString.c_str();
3161}
3162
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003163void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3164 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3165 GLbitfield mask)
3166{
3167 IDirect3DDevice9 *device = getDevice();
3168
3169 Framebuffer *readFramebuffer = getReadFramebuffer();
3170 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3171
3172 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3173 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3174 {
3175 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3176 }
3177
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003178 if (drawFramebuffer->getSamples() != 0)
3179 {
3180 return error(GL_INVALID_OPERATION);
3181 }
3182
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003183 RECT sourceRect;
3184 RECT destRect;
3185
3186 if (srcX0 < srcX1)
3187 {
3188 sourceRect.left = srcX0;
3189 sourceRect.right = srcX1;
3190 destRect.left = dstX0;
3191 destRect.right = dstX1;
3192 }
3193 else
3194 {
3195 sourceRect.left = srcX1;
3196 destRect.left = dstX1;
3197 sourceRect.right = srcX0;
3198 destRect.right = dstX0;
3199 }
3200
3201 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3202 // flip our Y-values here
3203 if (srcY0 < srcY1)
3204 {
3205 sourceRect.bottom = srcY1;
3206 destRect.bottom = dstY1;
3207 sourceRect.top = srcY0;
3208 destRect.top = dstY0;
3209 }
3210 else
3211 {
3212 sourceRect.bottom = srcY0;
3213 destRect.bottom = dstY0;
3214 sourceRect.top = srcY1;
3215 destRect.top = dstY1;
3216 }
3217
3218 RECT sourceScissoredRect = sourceRect;
3219 RECT destScissoredRect = destRect;
3220
3221 if (mState.scissorTest)
3222 {
3223 // Only write to parts of the destination framebuffer which pass the scissor test
3224 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3225 // rect will be checked against scissorY, rather than the bottom.
3226 if (destRect.left < mState.scissorX)
3227 {
3228 int xDiff = mState.scissorX - destRect.left;
3229 destScissoredRect.left = mState.scissorX;
3230 sourceScissoredRect.left += xDiff;
3231 }
3232
3233 if (destRect.right > mState.scissorX + mState.scissorWidth)
3234 {
3235 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3236 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3237 sourceScissoredRect.right -= xDiff;
3238 }
3239
3240 if (destRect.top < mState.scissorY)
3241 {
3242 int yDiff = mState.scissorY - destRect.top;
3243 destScissoredRect.top = mState.scissorY;
3244 sourceScissoredRect.top += yDiff;
3245 }
3246
3247 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3248 {
3249 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3250 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3251 sourceScissoredRect.bottom -= yDiff;
3252 }
3253 }
3254
3255 bool blitRenderTarget = false;
3256 bool blitDepthStencil = false;
3257
3258 RECT sourceTrimmedRect = sourceScissoredRect;
3259 RECT destTrimmedRect = destScissoredRect;
3260
3261 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3262 // the actual draw and read surfaces.
3263 if (sourceTrimmedRect.left < 0)
3264 {
3265 int xDiff = 0 - sourceTrimmedRect.left;
3266 sourceTrimmedRect.left = 0;
3267 destTrimmedRect.left += xDiff;
3268 }
3269
3270 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3271 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3272 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3273 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3274
3275 if (sourceTrimmedRect.right > readBufferWidth)
3276 {
3277 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3278 sourceTrimmedRect.right = readBufferWidth;
3279 destTrimmedRect.right -= xDiff;
3280 }
3281
3282 if (sourceTrimmedRect.top < 0)
3283 {
3284 int yDiff = 0 - sourceTrimmedRect.top;
3285 sourceTrimmedRect.top = 0;
3286 destTrimmedRect.top += yDiff;
3287 }
3288
3289 if (sourceTrimmedRect.bottom > readBufferHeight)
3290 {
3291 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3292 sourceTrimmedRect.bottom = readBufferHeight;
3293 destTrimmedRect.bottom -= yDiff;
3294 }
3295
3296 if (destTrimmedRect.left < 0)
3297 {
3298 int xDiff = 0 - destTrimmedRect.left;
3299 destTrimmedRect.left = 0;
3300 sourceTrimmedRect.left += xDiff;
3301 }
3302
3303 if (destTrimmedRect.right > drawBufferWidth)
3304 {
3305 int xDiff = destTrimmedRect.right - drawBufferWidth;
3306 destTrimmedRect.right = drawBufferWidth;
3307 sourceTrimmedRect.right -= xDiff;
3308 }
3309
3310 if (destTrimmedRect.top < 0)
3311 {
3312 int yDiff = 0 - destTrimmedRect.top;
3313 destTrimmedRect.top = 0;
3314 sourceTrimmedRect.top += yDiff;
3315 }
3316
3317 if (destTrimmedRect.bottom > drawBufferHeight)
3318 {
3319 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3320 destTrimmedRect.bottom = drawBufferHeight;
3321 sourceTrimmedRect.bottom -= yDiff;
3322 }
3323
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003324 bool partialBufferCopy = false;
3325 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3326 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3327 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3328 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3329 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3330 {
3331 partialBufferCopy = true;
3332 }
3333
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003334 if (mask & GL_COLOR_BUFFER_BIT)
3335 {
3336 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3337 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3338 {
3339 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3340 return error(GL_INVALID_OPERATION);
3341 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003342
3343 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3344 {
3345 return error(GL_INVALID_OPERATION);
3346 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003347
3348 blitRenderTarget = true;
3349
3350 }
3351
3352 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3353 {
3354 DepthStencilbuffer *readDSBuffer = NULL;
3355 DepthStencilbuffer *drawDSBuffer = NULL;
3356
3357 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3358 // both a depth and stencil buffer, it will be the same buffer.
3359
3360 if (mask & GL_DEPTH_BUFFER_BIT)
3361 {
3362 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3363 {
3364 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3365 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3366 {
3367 return error(GL_INVALID_OPERATION);
3368 }
3369
3370 blitDepthStencil = true;
3371 readDSBuffer = readFramebuffer->getDepthbuffer();
3372 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3373 }
3374 }
3375
3376 if (mask & GL_STENCIL_BUFFER_BIT)
3377 {
3378 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3379 {
3380 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3381 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3382 {
3383 return error(GL_INVALID_OPERATION);
3384 }
3385
3386 blitDepthStencil = true;
3387 readDSBuffer = readFramebuffer->getStencilbuffer();
3388 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3389 }
3390 }
3391
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003392 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003393 {
3394 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3395 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3396 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003397
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003398 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3399 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003400 {
3401 return error(GL_INVALID_OPERATION);
3402 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003403 }
3404
3405 if (blitRenderTarget || blitDepthStencil)
3406 {
3407 egl::Display *display = getDisplay();
3408 display->endScene();
3409
3410 if (blitRenderTarget)
3411 {
3412 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3413 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3414
3415 if (FAILED(result))
3416 {
3417 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3418 return;
3419 }
3420 }
3421
3422 if (blitDepthStencil)
3423 {
3424 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3425
3426 if (FAILED(result))
3427 {
3428 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3429 return;
3430 }
3431 }
3432 }
3433}
3434
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003435}
3436
3437extern "C"
3438{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003439gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003440{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003441 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003442}
3443
3444void glDestroyContext(gl::Context *context)
3445{
3446 delete context;
3447
3448 if (context == gl::getContext())
3449 {
3450 gl::makeCurrent(NULL, NULL, NULL);
3451 }
3452}
3453
3454void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3455{
3456 gl::makeCurrent(context, display, surface);
3457}
3458
3459gl::Context *glGetCurrentContext()
3460{
3461 return gl::getContext();
3462}
3463}