blob: 763794e020aba971c21932ed0ae042beb6371e47 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
28#include "libGLESv2/geometry/backend.h"
29#include "libGLESv2/geometry/VertexDataManager.h"
30#include "libGLESv2/geometry/IndexDataManager.h"
31#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.com86487c22010-03-11 19:41:43 +000033#undef near
34#undef far
35
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036namespace gl
37{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000038Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040{
41 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000042
daniel@transgaming.com428d1582010-05-04 03:35:25 +000043 mState.depthClearValue = 1.0f;
44 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045
daniel@transgaming.com428d1582010-05-04 03:35:25 +000046 mState.cullFace = false;
47 mState.cullMode = GL_BACK;
48 mState.frontFace = GL_CCW;
49 mState.depthTest = false;
50 mState.depthFunc = GL_LESS;
51 mState.blend = false;
52 mState.sourceBlendRGB = GL_ONE;
53 mState.sourceBlendAlpha = GL_ONE;
54 mState.destBlendRGB = GL_ZERO;
55 mState.destBlendAlpha = GL_ZERO;
56 mState.blendEquationRGB = GL_FUNC_ADD;
57 mState.blendEquationAlpha = GL_FUNC_ADD;
58 mState.blendColor.red = 0;
59 mState.blendColor.green = 0;
60 mState.blendColor.blue = 0;
61 mState.blendColor.alpha = 0;
62 mState.stencilTest = false;
63 mState.stencilFunc = GL_ALWAYS;
64 mState.stencilRef = 0;
65 mState.stencilMask = -1;
66 mState.stencilWritemask = -1;
67 mState.stencilBackFunc = GL_ALWAYS;
68 mState.stencilBackRef = 0;
69 mState.stencilBackMask = - 1;
70 mState.stencilBackWritemask = -1;
71 mState.stencilFail = GL_KEEP;
72 mState.stencilPassDepthFail = GL_KEEP;
73 mState.stencilPassDepthPass = GL_KEEP;
74 mState.stencilBackFail = GL_KEEP;
75 mState.stencilBackPassDepthFail = GL_KEEP;
76 mState.stencilBackPassDepthPass = GL_KEEP;
77 mState.polygonOffsetFill = false;
78 mState.polygonOffsetFactor = 0.0f;
79 mState.polygonOffsetUnits = 0.0f;
80 mState.sampleAlphaToCoverage = false;
81 mState.sampleCoverage = false;
82 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000083 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000084 mState.scissorTest = false;
85 mState.dither = true;
86 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000087 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000090
daniel@transgaming.com428d1582010-05-04 03:35:25 +000091 mState.viewportX = 0;
92 mState.viewportY = 0;
93 mState.viewportWidth = config->mDisplayMode.Width;
94 mState.viewportHeight = config->mDisplayMode.Height;
95 mState.zNear = 0.0f;
96 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097
daniel@transgaming.com428d1582010-05-04 03:35:25 +000098 mState.scissorX = 0;
99 mState.scissorY = 0;
100 mState.scissorWidth = config->mDisplayMode.Width;
101 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000103 mState.colorMaskRed = true;
104 mState.colorMaskGreen = true;
105 mState.colorMaskBlue = true;
106 mState.colorMaskAlpha = true;
107 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000109 if (shareContext != NULL)
110 {
111 mResourceManager = shareContext->mResourceManager;
112 mResourceManager->addRef();
113 }
114 else
115 {
116 mResourceManager = new ResourceManager();
117 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 // [OpenGL ES 2.0.24] section 3.7 page 83:
120 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
121 // and cube map texture state vectors respectively associated with them.
122 // In order that access to these initial textures not be lost, they are treated as texture
123 // objects all of whose names are 0.
124
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000125 mTexture2DZero.set(new Texture2D(0));
126 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000128 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000129 bindArrayBuffer(0);
130 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 bindTextureCubeMap(0);
132 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000133 bindReadFramebuffer(0);
134 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindRenderbuffer(0);
136
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000137 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000139 mState.packAlignment = 4;
140 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000141
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000142 mBufferBackEnd = NULL;
143 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000144 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000145 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147 mInvalidEnum = false;
148 mInvalidValue = false;
149 mInvalidOperation = false;
150 mOutOfMemory = false;
151 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000152
153 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000154
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000155 mSupportsCompressedTextures = false;
156 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000157 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000158 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000159 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
162Context::~Context()
163{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000164 if (mState.currentProgram != 0)
165 {
166 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
167 if (programObject)
168 {
169 programObject->release();
170 }
171 mState.currentProgram = 0;
172 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000174 while (!mFramebufferMap.empty())
175 {
176 deleteFramebuffer(mFramebufferMap.begin()->first);
177 }
178
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000179 while (!mFenceMap.empty())
180 {
181 deleteFence(mFenceMap.begin()->first);
182 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000183
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000184 while (!mMultiSampleSupport.empty())
185 {
186 delete [] mMultiSampleSupport.begin()->second;
187 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
188 }
189
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000190 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
191 {
192 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
193 {
194 mState.samplerTexture[type][sampler].set(NULL);
195 }
196 }
197
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000198 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
199 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000200 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000201 }
202
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000203 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
204 {
205 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
206 }
207
208 mState.arrayBuffer.set(NULL);
209 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 mState.renderbuffer.set(NULL);
211
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000212 mTexture2DZero.set(NULL);
213 mTextureCubeMapZero.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.com5d752f22010-10-07 13:37:20 +0000241 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
242
243 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
244 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
245 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
246 mMaxRenderbufferDimension = mMaxTextureDimension;
247 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
248 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
249 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
250
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000251 const D3DFORMAT renderBufferFormats[] =
252 {
253 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000254 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000255 D3DFMT_R5G6B5,
256 D3DFMT_D24S8
257 };
258
259 int max = 0;
260 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
261 {
262 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
263 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
264 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
265
266 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
267 {
268 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
269 {
270 max = j;
271 }
272 }
273 }
274
275 mMaxSupportedSamples = max;
276
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000277 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000278 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000279 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
280 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000281 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
282 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000283
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000284 initExtensionString();
285
286 mState.viewportX = 0;
287 mState.viewportY = 0;
288 mState.viewportWidth = surface->getWidth();
289 mState.viewportHeight = surface->getHeight();
290
291 mState.scissorX = 0;
292 mState.scissorY = 0;
293 mState.scissorWidth = surface->getWidth();
294 mState.scissorHeight = surface->getHeight();
295
296 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000297 }
298
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
300 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000301 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000304 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000305 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
307 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000309 if (defaultRenderTarget)
310 {
311 defaultRenderTarget->Release();
312 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000314 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000316 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000318
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000319 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320}
321
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000322// 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 +0000323void Context::markAllStateDirty()
324{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000325 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000326 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000327 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000328 mDepthStencilInitialized = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000329 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000330
331 mClearStateDirty = true;
332 mCullStateDirty = true;
333 mDepthStateDirty = true;
334 mMaskStateDirty = true;
335 mBlendStateDirty = true;
336 mStencilStateDirty = true;
337 mPolygonOffsetStateDirty = true;
338 mScissorStateDirty = true;
339 mSampleStateDirty = true;
340 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000341 mFrontFaceDirty = true;
342
343 if (mBufferBackEnd != NULL)
344 {
345 mBufferBackEnd->invalidate();
346 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000347}
348
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349void Context::setClearColor(float red, float green, float blue, float alpha)
350{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000351 mState.colorClearValue.red = red;
352 mState.colorClearValue.green = green;
353 mState.colorClearValue.blue = blue;
354 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355}
356
357void Context::setClearDepth(float depth)
358{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000359 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
362void Context::setClearStencil(int stencil)
363{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000364 mState.stencilClearValue = stencil;
365}
366
367void Context::setCullFace(bool enabled)
368{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000369 if (mState.cullFace != enabled)
370 {
371 mState.cullFace = enabled;
372 mCullStateDirty = true;
373 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000374}
375
376bool Context::isCullFaceEnabled() const
377{
378 return mState.cullFace;
379}
380
381void Context::setCullMode(GLenum mode)
382{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000383 if (mState.cullMode != mode)
384 {
385 mState.cullMode = mode;
386 mCullStateDirty = true;
387 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000388}
389
390void Context::setFrontFace(GLenum front)
391{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000392 if (mState.frontFace != front)
393 {
394 mState.frontFace = front;
395 mFrontFaceDirty = true;
396 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000397}
398
399void Context::setDepthTest(bool enabled)
400{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000401 if (mState.depthTest != enabled)
402 {
403 mState.depthTest = enabled;
404 mDepthStateDirty = true;
405 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000406}
407
408bool Context::isDepthTestEnabled() const
409{
410 return mState.depthTest;
411}
412
413void Context::setDepthFunc(GLenum depthFunc)
414{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000415 if (mState.depthFunc != depthFunc)
416 {
417 mState.depthFunc = depthFunc;
418 mDepthStateDirty = true;
419 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000420}
421
422void Context::setDepthRange(float zNear, float zFar)
423{
424 mState.zNear = zNear;
425 mState.zFar = zFar;
426}
427
428void Context::setBlend(bool enabled)
429{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000430 if (mState.blend != enabled)
431 {
432 mState.blend = enabled;
433 mBlendStateDirty = true;
434 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435}
436
437bool Context::isBlendEnabled() const
438{
439 return mState.blend;
440}
441
442void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
443{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000444 if (mState.sourceBlendRGB != sourceRGB ||
445 mState.sourceBlendAlpha != sourceAlpha ||
446 mState.destBlendRGB != destRGB ||
447 mState.destBlendAlpha != destAlpha)
448 {
449 mState.sourceBlendRGB = sourceRGB;
450 mState.destBlendRGB = destRGB;
451 mState.sourceBlendAlpha = sourceAlpha;
452 mState.destBlendAlpha = destAlpha;
453 mBlendStateDirty = true;
454 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000455}
456
457void Context::setBlendColor(float red, float green, float blue, float alpha)
458{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000459 if (mState.blendColor.red != red ||
460 mState.blendColor.green != green ||
461 mState.blendColor.blue != blue ||
462 mState.blendColor.alpha != alpha)
463 {
464 mState.blendColor.red = red;
465 mState.blendColor.green = green;
466 mState.blendColor.blue = blue;
467 mState.blendColor.alpha = alpha;
468 mBlendStateDirty = true;
469 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000470}
471
472void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
473{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000474 if (mState.blendEquationRGB != rgbEquation ||
475 mState.blendEquationAlpha != alphaEquation)
476 {
477 mState.blendEquationRGB = rgbEquation;
478 mState.blendEquationAlpha = alphaEquation;
479 mBlendStateDirty = true;
480 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000481}
482
483void Context::setStencilTest(bool enabled)
484{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000485 if (mState.stencilTest != enabled)
486 {
487 mState.stencilTest = enabled;
488 mStencilStateDirty = true;
489 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000490}
491
492bool Context::isStencilTestEnabled() const
493{
494 return mState.stencilTest;
495}
496
497void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
498{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000499 if (mState.stencilFunc != stencilFunc ||
500 mState.stencilRef != stencilRef ||
501 mState.stencilMask != stencilMask)
502 {
503 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000504 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000505 mState.stencilMask = stencilMask;
506 mStencilStateDirty = true;
507 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000508}
509
510void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
511{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000512 if (mState.stencilBackFunc != stencilBackFunc ||
513 mState.stencilBackRef != stencilBackRef ||
514 mState.stencilBackMask != stencilBackMask)
515 {
516 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000517 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000518 mState.stencilBackMask = stencilBackMask;
519 mStencilStateDirty = true;
520 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000521}
522
523void Context::setStencilWritemask(GLuint stencilWritemask)
524{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000525 if (mState.stencilWritemask != stencilWritemask)
526 {
527 mState.stencilWritemask = stencilWritemask;
528 mStencilStateDirty = true;
529 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000530}
531
532void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
533{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000534 if (mState.stencilBackWritemask != stencilBackWritemask)
535 {
536 mState.stencilBackWritemask = stencilBackWritemask;
537 mStencilStateDirty = true;
538 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000539}
540
541void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
542{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000543 if (mState.stencilFail != stencilFail ||
544 mState.stencilPassDepthFail != stencilPassDepthFail ||
545 mState.stencilPassDepthPass != stencilPassDepthPass)
546 {
547 mState.stencilFail = stencilFail;
548 mState.stencilPassDepthFail = stencilPassDepthFail;
549 mState.stencilPassDepthPass = stencilPassDepthPass;
550 mStencilStateDirty = true;
551 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000552}
553
554void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
555{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000556 if (mState.stencilBackFail != stencilBackFail ||
557 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
558 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
559 {
560 mState.stencilBackFail = stencilBackFail;
561 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
562 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
563 mStencilStateDirty = true;
564 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000565}
566
567void Context::setPolygonOffsetFill(bool enabled)
568{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000569 if (mState.polygonOffsetFill != enabled)
570 {
571 mState.polygonOffsetFill = enabled;
572 mPolygonOffsetStateDirty = true;
573 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000574}
575
576bool Context::isPolygonOffsetFillEnabled() const
577{
578 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000579
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000580}
581
582void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
583{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000584 if (mState.polygonOffsetFactor != factor ||
585 mState.polygonOffsetUnits != units)
586 {
587 mState.polygonOffsetFactor = factor;
588 mState.polygonOffsetUnits = units;
589 mPolygonOffsetStateDirty = true;
590 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setSampleAlphaToCoverage(bool enabled)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.sampleAlphaToCoverage != enabled)
596 {
597 mState.sampleAlphaToCoverage = enabled;
598 mSampleStateDirty = true;
599 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000600}
601
602bool Context::isSampleAlphaToCoverageEnabled() const
603{
604 return mState.sampleAlphaToCoverage;
605}
606
607void Context::setSampleCoverage(bool enabled)
608{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000609 if (mState.sampleCoverage != enabled)
610 {
611 mState.sampleCoverage = enabled;
612 mSampleStateDirty = true;
613 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000614}
615
616bool Context::isSampleCoverageEnabled() const
617{
618 return mState.sampleCoverage;
619}
620
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000621void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000623 if (mState.sampleCoverageValue != value ||
624 mState.sampleCoverageInvert != invert)
625 {
626 mState.sampleCoverageValue = value;
627 mState.sampleCoverageInvert = invert;
628 mSampleStateDirty = true;
629 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000630}
631
632void Context::setScissorTest(bool enabled)
633{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000634 if (mState.scissorTest != enabled)
635 {
636 mState.scissorTest = enabled;
637 mScissorStateDirty = true;
638 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000639}
640
641bool Context::isScissorTestEnabled() const
642{
643 return mState.scissorTest;
644}
645
646void Context::setDither(bool enabled)
647{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000648 if (mState.dither != enabled)
649 {
650 mState.dither = enabled;
651 mDitherStateDirty = true;
652 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000653}
654
655bool Context::isDitherEnabled() const
656{
657 return mState.dither;
658}
659
660void Context::setLineWidth(GLfloat width)
661{
662 mState.lineWidth = width;
663}
664
665void Context::setGenerateMipmapHint(GLenum hint)
666{
667 mState.generateMipmapHint = hint;
668}
669
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000670void Context::setFragmentShaderDerivativeHint(GLenum hint)
671{
672 mState.fragmentShaderDerivativeHint = hint;
673 // TODO: Propagate the hint to shader translator so we can write
674 // ddx, ddx_coarse, or ddx_fine depending on the hint.
675 // Ignore for now. It is valid for implementations to ignore hint.
676}
677
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000678void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
679{
680 mState.viewportX = x;
681 mState.viewportY = y;
682 mState.viewportWidth = width;
683 mState.viewportHeight = height;
684}
685
686void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
687{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000688 if (mState.scissorX != x || mState.scissorY != y ||
689 mState.scissorWidth != width || mState.scissorHeight != height)
690 {
691 mState.scissorX = x;
692 mState.scissorY = y;
693 mState.scissorWidth = width;
694 mState.scissorHeight = height;
695 mScissorStateDirty = true;
696 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000697}
698
699void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
700{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000701 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
702 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
703 {
704 mState.colorMaskRed = red;
705 mState.colorMaskGreen = green;
706 mState.colorMaskBlue = blue;
707 mState.colorMaskAlpha = alpha;
708 mMaskStateDirty = true;
709 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000710}
711
712void Context::setDepthMask(bool mask)
713{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000714 if (mState.depthMask != mask)
715 {
716 mState.depthMask = mask;
717 mMaskStateDirty = true;
718 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000719}
720
721void Context::setActiveSampler(int active)
722{
723 mState.activeSampler = active;
724}
725
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000726GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000727{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000728 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000729}
730
731GLuint Context::getDrawFramebufferHandle() const
732{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000733 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734}
735
736GLuint Context::getRenderbufferHandle() const
737{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000738 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739}
740
741GLuint Context::getArrayBufferHandle() const
742{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000743 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000744}
745
746void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
747{
748 mState.vertexAttribute[attribNum].mEnabled = enabled;
749}
750
751const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
752{
753 return mState.vertexAttribute[attribNum];
754}
755
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000756void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000757 GLsizei stride, const void *pointer)
758{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000759 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000760 mState.vertexAttribute[attribNum].mSize = size;
761 mState.vertexAttribute[attribNum].mType = type;
762 mState.vertexAttribute[attribNum].mNormalized = normalized;
763 mState.vertexAttribute[attribNum].mStride = stride;
764 mState.vertexAttribute[attribNum].mPointer = pointer;
765}
766
767const void *Context::getVertexAttribPointer(unsigned int attribNum) const
768{
769 return mState.vertexAttribute[attribNum].mPointer;
770}
771
772// returns entire set of attributes as a block
773const AttributeState *Context::getVertexAttribBlock()
774{
775 return mState.vertexAttribute;
776}
777
778void Context::setPackAlignment(GLint alignment)
779{
780 mState.packAlignment = alignment;
781}
782
783GLint Context::getPackAlignment() const
784{
785 return mState.packAlignment;
786}
787
788void Context::setUnpackAlignment(GLint alignment)
789{
790 mState.unpackAlignment = alignment;
791}
792
793GLint Context::getUnpackAlignment() const
794{
795 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798GLuint Context::createBuffer()
799{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000800 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801}
802
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803GLuint Context::createProgram()
804{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000805 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806}
807
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000808GLuint Context::createShader(GLenum type)
809{
810 return mResourceManager->createShader(type);
811}
812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813GLuint Context::createTexture()
814{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000815 return mResourceManager->createTexture();
816}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000818GLuint Context::createRenderbuffer()
819{
820 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821}
822
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000823// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824GLuint Context::createFramebuffer()
825{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000826 unsigned int handle = 1;
827
828 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
829 {
830 handle++;
831 }
832
833 mFramebufferMap[handle] = NULL;
834
835 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836}
837
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000838GLuint Context::createFence()
839{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000840 unsigned int handle = 0;
841
842 while (mFenceMap.find(handle) != mFenceMap.end())
843 {
844 handle++;
845 }
846
847 mFenceMap[handle] = new Fence;
848
849 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000850}
851
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852void Context::deleteBuffer(GLuint buffer)
853{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000854 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855 {
856 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000858
859 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862void Context::deleteShader(GLuint shader)
863{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000864 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
867void Context::deleteProgram(GLuint program)
868{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870}
871
872void Context::deleteTexture(GLuint texture)
873{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000874 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 {
876 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000878
879 mResourceManager->deleteTexture(texture);
880}
881
882void Context::deleteRenderbuffer(GLuint renderbuffer)
883{
884 if (mResourceManager->getRenderbuffer(renderbuffer))
885 {
886 detachRenderbuffer(renderbuffer);
887 }
888
889 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890}
891
892void Context::deleteFramebuffer(GLuint framebuffer)
893{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000894 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
895
896 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897 {
898 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000899
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000900 delete framebufferObject->second;
901 mFramebufferMap.erase(framebufferObject);
902 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000904
905void Context::deleteFence(GLuint fence)
906{
907 FenceMap::iterator fenceObject = mFenceMap.find(fence);
908
909 if (fenceObject != mFenceMap.end())
910 {
911 delete fenceObject->second;
912 mFenceMap.erase(fenceObject);
913 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000914}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000916Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918 return mResourceManager->getBuffer(handle);
919}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000921Shader *Context::getShader(GLuint handle)
922{
923 return mResourceManager->getShader(handle);
924}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000926Program *Context::getProgram(GLuint handle)
927{
928 return mResourceManager->getProgram(handle);
929}
930
931Texture *Context::getTexture(GLuint handle)
932{
933 return mResourceManager->getTexture(handle);
934}
935
936Renderbuffer *Context::getRenderbuffer(GLuint handle)
937{
938 return mResourceManager->getRenderbuffer(handle);
939}
940
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000941Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000942{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000943 return getFramebuffer(mState.readFramebuffer);
944}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000945
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000946Framebuffer *Context::getDrawFramebuffer()
947{
948 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949}
950
951void Context::bindArrayBuffer(unsigned int buffer)
952{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000953 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000955 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
958void Context::bindElementArrayBuffer(unsigned int buffer)
959{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000960 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000962 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963}
964
965void Context::bindTexture2D(GLuint texture)
966{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000967 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000969 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970}
971
972void Context::bindTextureCubeMap(GLuint texture)
973{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000974 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000976 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000979void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000981 if (!getFramebuffer(framebuffer))
982 {
983 mFramebufferMap[framebuffer] = new Framebuffer();
984 }
985
986 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000987}
988
989void Context::bindDrawFramebuffer(GLuint framebuffer)
990{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000991 if (!getFramebuffer(framebuffer))
992 {
993 mFramebufferMap[framebuffer] = new Framebuffer();
994 }
995
996 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
999void Context::bindRenderbuffer(GLuint renderbuffer)
1000{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001001 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1002
1003 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004}
1005
1006void Context::useProgram(GLuint program)
1007{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001008 GLuint priorProgram = mState.currentProgram;
1009 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001010
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001011 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001013 Program *newProgram = mResourceManager->getProgram(program);
1014 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1015
1016 if (newProgram)
1017 {
1018 newProgram->addRef();
1019 }
1020
1021 if (oldProgram)
1022 {
1023 oldProgram->release();
1024 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026}
1027
1028void Context::setFramebufferZero(Framebuffer *buffer)
1029{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001030 delete mFramebufferMap[0];
1031 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032}
1033
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001034void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001036 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1037 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038}
1039
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001040Framebuffer *Context::getFramebuffer(unsigned int handle)
1041{
1042 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1043
1044 if (framebuffer == mFramebufferMap.end())
1045 {
1046 return NULL;
1047 }
1048 else
1049 {
1050 return framebuffer->second;
1051 }
1052}
1053
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001054Fence *Context::getFence(unsigned int handle)
1055{
1056 FenceMap::iterator fence = mFenceMap.find(handle);
1057
1058 if (fence == mFenceMap.end())
1059 {
1060 return NULL;
1061 }
1062 else
1063 {
1064 return fence->second;
1065 }
1066}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001067
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068Buffer *Context::getArrayBuffer()
1069{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001070 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071}
1072
1073Buffer *Context::getElementArrayBuffer()
1074{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001075 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076}
1077
1078Program *Context::getCurrentProgram()
1079{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001080 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081}
1082
1083Texture2D *Context::getTexture2D()
1084{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001085 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, SAMPLER_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
1088TextureCubeMap *Context::getTextureCubeMap()
1089{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001090 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, SAMPLER_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091}
1092
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001093Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001095 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001096
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001097 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001098 {
1099 switch (type)
1100 {
1101 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001102 case SAMPLER_2D: return mTexture2DZero.get();
1103 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001104 }
1105 }
1106
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001107 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108}
1109
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001110bool Context::getBooleanv(GLenum pname, GLboolean *params)
1111{
1112 switch (pname)
1113 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001114 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1115 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1116 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001117 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001118 params[0] = mState.colorMaskRed;
1119 params[1] = mState.colorMaskGreen;
1120 params[2] = mState.colorMaskBlue;
1121 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001122 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001123 case GL_CULL_FACE: *params = mState.cullFace; break;
1124 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1125 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1126 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1127 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1128 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1129 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1130 case GL_BLEND: *params = mState.blend; break;
1131 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001132 default:
1133 return false;
1134 }
1135
1136 return true;
1137}
1138
1139bool Context::getFloatv(GLenum pname, GLfloat *params)
1140{
1141 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1142 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1143 // GetIntegerv as its native query function. As it would require conversion in any
1144 // case, this should make no difference to the calling application.
1145 switch (pname)
1146 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001147 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1148 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1149 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1150 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1151 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001152 case GL_ALIASED_LINE_WIDTH_RANGE:
1153 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1154 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1155 break;
1156 case GL_ALIASED_POINT_SIZE_RANGE:
1157 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001158 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 +00001159 break;
1160 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 params[0] = mState.zNear;
1162 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001163 break;
1164 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001165 params[0] = mState.colorClearValue.red;
1166 params[1] = mState.colorClearValue.green;
1167 params[2] = mState.colorClearValue.blue;
1168 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001169 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001170 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001171 params[0] = mState.blendColor.red;
1172 params[1] = mState.blendColor.green;
1173 params[2] = mState.blendColor.blue;
1174 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001175 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001176 default:
1177 return false;
1178 }
1179
1180 return true;
1181}
1182
1183bool Context::getIntegerv(GLenum pname, GLint *params)
1184{
1185 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1186 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1187 // GetIntegerv as its native query function. As it would require conversion in any
1188 // case, this should make no difference to the calling application. You may find it in
1189 // Context::getFloatv.
1190 switch (pname)
1191 {
1192 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1193 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001194 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001195 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1196 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1197 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001198 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001199 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001200 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001201 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001202 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1203 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001204 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1205 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1206 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001207 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001208 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1209 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1210 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1211 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001212 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001213 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1214 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1215 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1216 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1217 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1218 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1219 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1220 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1221 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1222 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1223 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1224 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1225 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1226 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1227 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1228 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1229 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1230 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1231 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1232 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1233 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1234 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1235 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1236 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001237 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1238 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001239 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1240 {
1241 if (supportsCompressedTextures())
1242 {
1243 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1244 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1245 *params = 2;
1246 }
1247 else
1248 {
1249 *params = 0;
1250 }
1251 }
1252 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001253 case GL_MAX_SAMPLES_ANGLE:
1254 {
1255 GLsizei maxSamples = getMaxSupportedSamples();
1256 if (maxSamples != 0)
1257 {
1258 *params = maxSamples;
1259 }
1260 else
1261 {
1262 return false;
1263 }
1264
1265 break;
1266 }
1267 case GL_SAMPLE_BUFFERS:
1268 case GL_SAMPLES:
1269 {
1270 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1271 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1272 {
1273 switch (pname)
1274 {
1275 case GL_SAMPLE_BUFFERS:
1276 if (framebuffer->getSamples() != 0)
1277 {
1278 *params = 1;
1279 }
1280 else
1281 {
1282 *params = 0;
1283 }
1284 break;
1285 case GL_SAMPLES:
1286 *params = framebuffer->getSamples();
1287 break;
1288 }
1289 }
1290 else
1291 {
1292 *params = 0;
1293 }
1294 }
1295 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001296 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1297 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1298 case GL_MAX_VIEWPORT_DIMS:
1299 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001300 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001301 params[0] = maxDimension;
1302 params[1] = maxDimension;
1303 }
1304 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001305 case GL_COMPRESSED_TEXTURE_FORMATS:
1306 {
1307 if (supportsCompressedTextures())
1308 {
1309 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1310 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1311 }
1312 }
1313 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001314 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001315 params[0] = mState.viewportX;
1316 params[1] = mState.viewportY;
1317 params[2] = mState.viewportWidth;
1318 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001319 break;
1320 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001321 params[0] = mState.scissorX;
1322 params[1] = mState.scissorY;
1323 params[2] = mState.scissorWidth;
1324 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001325 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001326 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1327 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001328 case GL_RED_BITS:
1329 case GL_GREEN_BITS:
1330 case GL_BLUE_BITS:
1331 case GL_ALPHA_BITS:
1332 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001333 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001334 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1335
1336 if (colorbuffer)
1337 {
1338 switch (pname)
1339 {
1340 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1341 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1342 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1343 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1344 }
1345 }
1346 else
1347 {
1348 *params = 0;
1349 }
1350 }
1351 break;
1352 case GL_DEPTH_BITS:
1353 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001354 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001355 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001356
1357 if (depthbuffer)
1358 {
1359 *params = depthbuffer->getDepthSize();
1360 }
1361 else
1362 {
1363 *params = 0;
1364 }
1365 }
1366 break;
1367 case GL_STENCIL_BITS:
1368 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001369 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001370 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001371
1372 if (stencilbuffer)
1373 {
1374 *params = stencilbuffer->getStencilSize();
1375 }
1376 else
1377 {
1378 *params = 0;
1379 }
1380 }
1381 break;
1382 case GL_TEXTURE_BINDING_2D:
1383 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001384 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001385 {
1386 error(GL_INVALID_OPERATION);
1387 return false;
1388 }
1389
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001390 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 }
1392 break;
1393 case GL_TEXTURE_BINDING_CUBE_MAP:
1394 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001395 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001396 {
1397 error(GL_INVALID_OPERATION);
1398 return false;
1399 }
1400
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001401 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001402 }
1403 break;
1404 default:
1405 return false;
1406 }
1407
1408 return true;
1409}
1410
1411bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1412{
1413 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1414 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1415 // to the fact that it is stored internally as a float, and so would require conversion
1416 // if returned from Context::getIntegerv. Since this conversion is already implemented
1417 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1418 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1419 // application.
1420 switch (pname)
1421 {
1422 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1423 case GL_SHADER_BINARY_FORMATS:
1424 {
1425 *type = GL_INT;
1426 *numParams = 0;
1427 }
1428 break;
1429 case GL_MAX_VERTEX_ATTRIBS:
1430 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1431 case GL_MAX_VARYING_VECTORS:
1432 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1433 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1434 case GL_MAX_TEXTURE_IMAGE_UNITS:
1435 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1436 case GL_MAX_RENDERBUFFER_SIZE:
1437 case GL_NUM_SHADER_BINARY_FORMATS:
1438 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1439 case GL_ARRAY_BUFFER_BINDING:
1440 case GL_FRAMEBUFFER_BINDING:
1441 case GL_RENDERBUFFER_BINDING:
1442 case GL_CURRENT_PROGRAM:
1443 case GL_PACK_ALIGNMENT:
1444 case GL_UNPACK_ALIGNMENT:
1445 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001446 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001447 case GL_RED_BITS:
1448 case GL_GREEN_BITS:
1449 case GL_BLUE_BITS:
1450 case GL_ALPHA_BITS:
1451 case GL_DEPTH_BITS:
1452 case GL_STENCIL_BITS:
1453 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1454 case GL_CULL_FACE_MODE:
1455 case GL_FRONT_FACE:
1456 case GL_ACTIVE_TEXTURE:
1457 case GL_STENCIL_FUNC:
1458 case GL_STENCIL_VALUE_MASK:
1459 case GL_STENCIL_REF:
1460 case GL_STENCIL_FAIL:
1461 case GL_STENCIL_PASS_DEPTH_FAIL:
1462 case GL_STENCIL_PASS_DEPTH_PASS:
1463 case GL_STENCIL_BACK_FUNC:
1464 case GL_STENCIL_BACK_VALUE_MASK:
1465 case GL_STENCIL_BACK_REF:
1466 case GL_STENCIL_BACK_FAIL:
1467 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1468 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1469 case GL_DEPTH_FUNC:
1470 case GL_BLEND_SRC_RGB:
1471 case GL_BLEND_SRC_ALPHA:
1472 case GL_BLEND_DST_RGB:
1473 case GL_BLEND_DST_ALPHA:
1474 case GL_BLEND_EQUATION_RGB:
1475 case GL_BLEND_EQUATION_ALPHA:
1476 case GL_STENCIL_WRITEMASK:
1477 case GL_STENCIL_BACK_WRITEMASK:
1478 case GL_STENCIL_CLEAR_VALUE:
1479 case GL_SUBPIXEL_BITS:
1480 case GL_MAX_TEXTURE_SIZE:
1481 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1482 case GL_SAMPLE_BUFFERS:
1483 case GL_SAMPLES:
1484 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1485 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1486 case GL_TEXTURE_BINDING_2D:
1487 case GL_TEXTURE_BINDING_CUBE_MAP:
1488 {
1489 *type = GL_INT;
1490 *numParams = 1;
1491 }
1492 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001493 case GL_MAX_SAMPLES_ANGLE:
1494 {
1495 if (getMaxSupportedSamples() != 0)
1496 {
1497 *type = GL_INT;
1498 *numParams = 1;
1499 }
1500 else
1501 {
1502 return false;
1503 }
1504 }
1505 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001506 case GL_MAX_VIEWPORT_DIMS:
1507 {
1508 *type = GL_INT;
1509 *numParams = 2;
1510 }
1511 break;
1512 case GL_VIEWPORT:
1513 case GL_SCISSOR_BOX:
1514 {
1515 *type = GL_INT;
1516 *numParams = 4;
1517 }
1518 break;
1519 case GL_SHADER_COMPILER:
1520 case GL_SAMPLE_COVERAGE_INVERT:
1521 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001522 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1523 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1524 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1525 case GL_SAMPLE_COVERAGE:
1526 case GL_SCISSOR_TEST:
1527 case GL_STENCIL_TEST:
1528 case GL_DEPTH_TEST:
1529 case GL_BLEND:
1530 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001531 {
1532 *type = GL_BOOL;
1533 *numParams = 1;
1534 }
1535 break;
1536 case GL_COLOR_WRITEMASK:
1537 {
1538 *type = GL_BOOL;
1539 *numParams = 4;
1540 }
1541 break;
1542 case GL_POLYGON_OFFSET_FACTOR:
1543 case GL_POLYGON_OFFSET_UNITS:
1544 case GL_SAMPLE_COVERAGE_VALUE:
1545 case GL_DEPTH_CLEAR_VALUE:
1546 case GL_LINE_WIDTH:
1547 {
1548 *type = GL_FLOAT;
1549 *numParams = 1;
1550 }
1551 break;
1552 case GL_ALIASED_LINE_WIDTH_RANGE:
1553 case GL_ALIASED_POINT_SIZE_RANGE:
1554 case GL_DEPTH_RANGE:
1555 {
1556 *type = GL_FLOAT;
1557 *numParams = 2;
1558 }
1559 break;
1560 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001561 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001562 {
1563 *type = GL_FLOAT;
1564 *numParams = 4;
1565 }
1566 break;
1567 default:
1568 return false;
1569 }
1570
1571 return true;
1572}
1573
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574// Applies the render target surface, depth stencil surface, viewport rectangle and
1575// scissor rectangle to the Direct3D 9 device
1576bool Context::applyRenderTarget(bool ignoreViewport)
1577{
1578 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001579
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001580 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001581
1582 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1583 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001584 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1585
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001586 return false;
1587 }
1588
1589 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001590
1591 if (!renderTarget)
1592 {
1593 return false; // Context must be lost
1594 }
1595
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001596 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001598 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1599 if (renderTargetSerial != mAppliedRenderTargetSerial)
1600 {
1601 device->SetRenderTarget(0, renderTarget);
1602 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001603 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 +00001604 }
1605
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001606 unsigned int depthbufferSerial = 0;
1607 unsigned int stencilbufferSerial = 0;
1608 if (framebufferObject->getDepthbufferType() != GL_NONE)
1609 {
1610 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001611 if (!depthStencil)
1612 {
1613 ERR("Depth stencil pointer unexpectedly null.");
1614 return false;
1615 }
1616
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001617 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1618 }
1619 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1620 {
1621 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001622 if (!depthStencil)
1623 {
1624 ERR("Depth stencil pointer unexpectedly null.");
1625 return false;
1626 }
1627
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001628 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1629 }
1630
1631 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001632 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001633 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001634 {
1635 device->SetDepthStencilSurface(depthStencil);
1636 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001637 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001638 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001639 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640
1641 D3DVIEWPORT9 viewport;
1642 D3DSURFACE_DESC desc;
1643 renderTarget->GetDesc(&desc);
1644
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001645 float zNear = clamp01(mState.zNear);
1646 float zFar = clamp01(mState.zFar);
1647
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648 if (ignoreViewport)
1649 {
1650 viewport.X = 0;
1651 viewport.Y = 0;
1652 viewport.Width = desc.Width;
1653 viewport.Height = desc.Height;
1654 viewport.MinZ = 0.0f;
1655 viewport.MaxZ = 1.0f;
1656 }
1657 else
1658 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001659 viewport.X = std::max(mState.viewportX, 0);
1660 viewport.Y = std::max(mState.viewportY, 0);
1661 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1662 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001663 viewport.MinZ = zNear;
1664 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001665 }
1666
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001667 if (viewport.Width <= 0 || viewport.Height <= 0)
1668 {
1669 return false; // Nothing to render
1670 }
1671
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 device->SetViewport(&viewport);
1673
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001674 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001676 if (mState.scissorTest)
1677 {
1678 RECT rect = {mState.scissorX,
1679 mState.scissorY,
1680 mState.scissorX + mState.scissorWidth,
1681 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001682 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1683 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001684 device->SetScissorRect(&rect);
1685 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1686 }
1687 else
1688 {
1689 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1690 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001691
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001692 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693 }
1694
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 Program *programObject = getCurrentProgram();
1698
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001699 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001700 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001701 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001702
daniel@transgaming.com31754962010-11-28 02:02:52 +00001703 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001704 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1705 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1706 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001707 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001708
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001709 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001710 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001711 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001712
daniel@transgaming.com31754962010-11-28 02:02:52 +00001713 GLint depthRange = programObject->getDxDepthRangeLocation();
1714 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1715 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716 }
1717
1718 return true;
1719}
1720
1721// 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 +00001722void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723{
1724 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001725 Program *programObject = getCurrentProgram();
1726
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001727 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001728 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001729 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001730
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001731 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001732 GLint alwaysFront = !isTriangleMode(drawMode);
1733 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1734
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001735 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001736
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001737 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001739 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 }
1743 else
1744 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001745 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746 }
1747
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 mCullStateDirty = false;
1749 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 if (mDepthStateDirty)
1752 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001753 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001755 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1756 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 }
1758 else
1759 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001760 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001762
1763 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 }
1765
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001766 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001768 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001769 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001770 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1771
1772 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1773 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1774 {
1775 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1776 }
1777 else
1778 {
1779 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1780 unorm<8>(mState.blendColor.alpha),
1781 unorm<8>(mState.blendColor.alpha),
1782 unorm<8>(mState.blendColor.alpha)));
1783 }
1784
1785 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1786 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1787 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1788
1789 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1790 mState.destBlendRGB != mState.destBlendAlpha ||
1791 mState.blendEquationRGB != mState.blendEquationAlpha)
1792 {
1793 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1794
1795 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1796 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1797 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1798
1799 }
1800 else
1801 {
1802 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1803 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001804 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001805 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001806 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001807 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001808 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001809
1810 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811 }
1812
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001813 if (mStencilStateDirty || mFrontFaceDirty)
1814 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001815 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001816 {
1817 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1818 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1819
1820 // FIXME: Unsupported by D3D9
1821 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1822 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1823 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1824 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1825 mState.stencilRef != mState.stencilBackRef ||
1826 mState.stencilMask != mState.stencilBackMask)
1827 {
1828 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1829 return error(GL_INVALID_OPERATION);
1830 }
1831
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001832 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001833 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001834 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1835
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001836 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1837 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1838 es2dx::ConvertComparison(mState.stencilFunc));
1839
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001840 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 +00001841 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1842
1843 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1844 es2dx::ConvertStencilOp(mState.stencilFail));
1845 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1846 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1847 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1848 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1849
1850 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1851 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1852 es2dx::ConvertComparison(mState.stencilBackFunc));
1853
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001854 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 +00001855 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1856
1857 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1858 es2dx::ConvertStencilOp(mState.stencilBackFail));
1859 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1860 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1861 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1862 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1863 }
1864 else
1865 {
1866 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1867 }
1868
1869 mStencilStateDirty = false;
1870 }
1871
1872 if (mMaskStateDirty)
1873 {
1874 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1875 mState.colorMaskBlue, mState.colorMaskAlpha));
1876 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1877
1878 mMaskStateDirty = false;
1879 }
1880
1881 if (mPolygonOffsetStateDirty)
1882 {
1883 if (mState.polygonOffsetFill)
1884 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001885 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001886 if (depthbuffer)
1887 {
1888 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1889 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1890 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1891 }
1892 }
1893 else
1894 {
1895 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1896 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1897 }
1898
1899 mPolygonOffsetStateDirty = false;
1900 }
1901
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001902 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001904 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001905 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001906 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001907 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001908 FIXME("Sample alpha to coverage is unimplemented.");
1909 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001910
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001911 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1912 if (mState.sampleCoverage)
1913 {
1914 unsigned int mask = 0;
1915 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001916 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001917 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001918
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001919 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001920 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001921 mask <<= 1;
1922
1923 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1924 {
1925 threshold += 1.0f;
1926 mask |= 1;
1927 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001928 }
1929 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001930
1931 if (mState.sampleCoverageInvert)
1932 {
1933 mask = ~mask;
1934 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001935
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001936 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1937 }
1938 else
1939 {
1940 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1941 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001942 }
1943 else
1944 {
1945 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001946 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001947
1948 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949 }
1950
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001951 if (mDitherStateDirty)
1952 {
1953 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1954
1955 mDitherStateDirty = false;
1956 }
1957
1958 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959}
1960
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001961// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001962void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001963{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001964 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001966 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001968 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001969 }
1970 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001971}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001973GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001974{
1975 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1976
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001977 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001978 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001979 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001980 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001981 }
1982
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001983 lookupAttributeMapping(translated);
1984
1985 mBufferBackEnd->setupAttributesPreDraw(translated);
1986
1987 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1988 {
1989 if (translated[i].enabled && translated[i].nonArray)
1990 {
1991 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1992 if (err != GL_NO_ERROR)
1993 {
1994 return err;
1995 }
1996
1997 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1998
1999 *useIndexing = true;
2000 return GL_NO_ERROR;
2001 }
2002 }
2003
2004 *useIndexing = false;
2005 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002006}
2007
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002008GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002009{
2010 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
2011
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002012 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002013
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002014 if (err == GL_NO_ERROR)
2015 {
2016 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002017
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002018 mBufferBackEnd->setupAttributesPreDraw(translated);
2019 }
2020
2021 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002022}
2023
2024// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002025GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002026{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002027 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002028
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002029 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002030 {
2031 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2032 }
2033
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002034 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035}
2036
2037// Applies the shaders and shader constants to the Direct3D 9 device
2038void Context::applyShaders()
2039{
2040 IDirect3DDevice9 *device = getDevice();
2041 Program *programObject = getCurrentProgram();
2042 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2043 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2044
2045 device->SetVertexShader(vertexShader);
2046 device->SetPixelShader(pixelShader);
2047
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002048 if (programObject->getSerial() != mAppliedProgram)
2049 {
2050 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002051 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002052 mAppliedProgram = programObject->getSerial();
2053 }
2054
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055 programObject->applyUniforms();
2056}
2057
2058// Applies the textures and sampler states to the Direct3D 9 device
2059void Context::applyTextures()
2060{
2061 IDirect3DDevice9 *device = getDevice();
2062 Program *programObject = getCurrentProgram();
2063
2064 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2065 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002066 int textureUnit = programObject->getSamplerMapping(sampler);
2067 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002069 SamplerType textureType = programObject->getSamplerType(sampler);
2070
2071 Texture *texture = getSamplerTexture(textureUnit, textureType);
2072
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002073 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002074 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002075 if (texture->isComplete())
2076 {
2077 GLenum wrapS = texture->getWrapS();
2078 GLenum wrapT = texture->getWrapT();
2079 GLenum minFilter = texture->getMinFilter();
2080 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002082 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2083 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002085 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2086 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2087 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2088 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2089 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002091 device->SetTexture(sampler, texture->getTexture());
2092 }
2093 else
2094 {
2095 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2096 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002097 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002098
2099 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002101 else
2102 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002103 if (programObject->isSamplerDirty(sampler))
2104 {
2105 device->SetTexture(sampler, NULL);
2106 programObject->setSamplerDirty(sampler, false);
2107 }
2108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 }
2110}
2111
2112void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2113{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002114 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002115
2116 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2117 {
2118 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2119 }
2120
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002121 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2122 {
2123 return error(GL_INVALID_OPERATION);
2124 }
2125
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002127
2128 if (!renderTarget)
2129 {
2130 return; // Context must be lost, return silently
2131 }
2132
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 IDirect3DDevice9 *device = getDevice();
2134
2135 D3DSURFACE_DESC desc;
2136 renderTarget->GetDesc(&desc);
2137
2138 IDirect3DSurface9 *systemSurface;
2139 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2140
2141 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2142 {
2143 return error(GL_OUT_OF_MEMORY);
2144 }
2145
2146 ASSERT(SUCCEEDED(result));
2147
2148 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2149 {
2150 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2151 }
2152
2153 result = device->GetRenderTargetData(renderTarget, systemSurface);
2154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 if (FAILED(result))
2156 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157 systemSurface->Release();
2158
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002159 switch (result)
2160 {
2161 case D3DERR_DRIVERINTERNALERROR:
2162 case D3DERR_DEVICELOST:
2163 return error(GL_OUT_OF_MEMORY);
2164 default:
2165 UNREACHABLE();
2166 return; // No sensible error to generate
2167 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 }
2169
2170 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002171 RECT rect = {std::max(x, 0),
2172 std::max(y, 0),
2173 std::min(x + width, (int)desc.Width),
2174 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175
2176 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2177
2178 if (FAILED(result))
2179 {
2180 UNREACHABLE();
2181 systemSurface->Release();
2182
2183 return; // No sensible error to generate
2184 }
2185
2186 unsigned char *source = (unsigned char*)lock.pBits;
2187 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002188 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002190 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002191
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192 for (int j = 0; j < rect.bottom - rect.top; j++)
2193 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002194 if (desc.Format == D3DFMT_A8R8G8B8 &&
2195 format == GL_BGRA_EXT &&
2196 type == GL_UNSIGNED_BYTE)
2197 {
2198 // Fast path for EXT_read_format_bgra, given
2199 // an RGBA source buffer. Note that buffers with no
2200 // alpha go through the slow path below.
2201 memcpy(dest + j * outputPitch,
2202 source + j * lock.Pitch,
2203 (rect.right - rect.left) * 4);
2204 continue;
2205 }
2206
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002207 for (int i = 0; i < rect.right - rect.left; i++)
2208 {
2209 float r;
2210 float g;
2211 float b;
2212 float a;
2213
2214 switch (desc.Format)
2215 {
2216 case D3DFMT_R5G6B5:
2217 {
2218 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2219
2220 a = 1.0f;
2221 b = (rgb & 0x001F) * (1.0f / 0x001F);
2222 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2223 r = (rgb & 0xF800) * (1.0f / 0xF800);
2224 }
2225 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002226 case D3DFMT_A1R5G5B5:
2227 {
2228 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2229
2230 a = (argb & 0x8000) ? 1.0f : 0.0f;
2231 b = (argb & 0x001F) * (1.0f / 0x001F);
2232 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2233 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2234 }
2235 break;
2236 case D3DFMT_A8R8G8B8:
2237 {
2238 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2239
2240 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2241 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2242 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2243 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2244 }
2245 break;
2246 case D3DFMT_X8R8G8B8:
2247 {
2248 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2249
2250 a = 1.0f;
2251 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2252 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2253 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2254 }
2255 break;
2256 case D3DFMT_A2R10G10B10:
2257 {
2258 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2259
2260 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2261 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2262 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2263 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2264 }
2265 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002266 case D3DFMT_A32B32G32R32F:
2267 {
2268 // float formats in D3D are stored rgba, rather than the other way round
2269 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2270 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2271 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2272 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2273 }
2274 break;
2275 case D3DFMT_A16B16G16R16F:
2276 {
2277 // float formats in D3D are stored rgba, rather than the other way round
2278 float abgr[4];
2279
2280 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2281
2282 a = abgr[3];
2283 b = abgr[2];
2284 g = abgr[1];
2285 r = abgr[0];
2286 }
2287 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 default:
2289 UNIMPLEMENTED(); // FIXME
2290 UNREACHABLE();
2291 }
2292
2293 switch (format)
2294 {
2295 case GL_RGBA:
2296 switch (type)
2297 {
2298 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002299 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2300 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2301 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2302 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 break;
2304 default: UNREACHABLE();
2305 }
2306 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002307 case GL_BGRA_EXT:
2308 switch (type)
2309 {
2310 case GL_UNSIGNED_BYTE:
2311 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2312 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2313 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2314 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2315 break;
2316 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2317 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2318 // this type is packed as follows:
2319 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2320 // --------------------------------------------------------------------------------
2321 // | 4th | 3rd | 2nd | 1st component |
2322 // --------------------------------------------------------------------------------
2323 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2324 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2325 ((unsigned short)(15 * a + 0.5f) << 12)|
2326 ((unsigned short)(15 * r + 0.5f) << 8) |
2327 ((unsigned short)(15 * g + 0.5f) << 4) |
2328 ((unsigned short)(15 * b + 0.5f) << 0);
2329 break;
2330 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2331 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2332 // this type is packed as follows:
2333 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2334 // --------------------------------------------------------------------------------
2335 // | 4th | 3rd | 2nd | 1st component |
2336 // --------------------------------------------------------------------------------
2337 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2338 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2339 ((unsigned short)( a + 0.5f) << 15) |
2340 ((unsigned short)(31 * r + 0.5f) << 10) |
2341 ((unsigned short)(31 * g + 0.5f) << 5) |
2342 ((unsigned short)(31 * b + 0.5f) << 0);
2343 break;
2344 default: UNREACHABLE();
2345 }
2346 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002347 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 switch (type)
2349 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002350 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002351 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2352 ((unsigned short)(31 * b + 0.5f) << 0) |
2353 ((unsigned short)(63 * g + 0.5f) << 5) |
2354 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 break;
2356 default: UNREACHABLE();
2357 }
2358 break;
2359 default: UNREACHABLE();
2360 }
2361 }
2362 }
2363
2364 systemSurface->UnlockRect();
2365
2366 systemSurface->Release();
2367}
2368
2369void Context::clear(GLbitfield mask)
2370{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002371 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002372
2373 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2374 {
2375 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2376
2377 return;
2378 }
2379
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002380 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381 IDirect3DDevice9 *device = getDevice();
2382 DWORD flags = 0;
2383
2384 if (mask & GL_COLOR_BUFFER_BIT)
2385 {
2386 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002387
2388 if (framebufferObject->getColorbufferType() != GL_NONE)
2389 {
2390 flags |= D3DCLEAR_TARGET;
2391 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392 }
2393
2394 if (mask & GL_DEPTH_BUFFER_BIT)
2395 {
2396 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002397 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398 {
2399 flags |= D3DCLEAR_ZBUFFER;
2400 }
2401 }
2402
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 GLuint stencilUnmasked = 0x0;
2404
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002405 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002408 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002410 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002411 if (!depthStencil)
2412 {
2413 ERR("Depth stencil pointer unexpectedly null.");
2414 return;
2415 }
2416
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002417 D3DSURFACE_DESC desc;
2418 depthStencil->GetDesc(&desc);
2419
2420 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2421 stencilUnmasked = (0x1 << stencilSize) - 1;
2422
2423 if (stencilUnmasked != 0x0)
2424 {
2425 flags |= D3DCLEAR_STENCIL;
2426 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 }
2428 }
2429
2430 if (mask != 0)
2431 {
2432 return error(GL_INVALID_VALUE);
2433 }
2434
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002435 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2436 {
2437 return;
2438 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002440 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2441 unorm<8>(mState.colorClearValue.red),
2442 unorm<8>(mState.colorClearValue.green),
2443 unorm<8>(mState.colorClearValue.blue));
2444 float depth = clamp01(mState.depthClearValue);
2445 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446
2447 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2448
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002449 if (!renderTarget)
2450 {
2451 return; // Context must be lost, return silently
2452 }
2453
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 D3DSURFACE_DESC desc;
2455 renderTarget->GetDesc(&desc);
2456
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002457 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458
2459 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002460 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002462 !(mState.colorMaskRed && mState.colorMaskGreen &&
2463 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464
2465 if (needMaskedColorClear || needMaskedStencilClear)
2466 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002467 // State which is altered in all paths from this point to the clear call is saved.
2468 // State which is altered in only some paths will be flagged dirty in the case that
2469 // that path is taken.
2470 HRESULT hr;
2471 if (mMaskedClearSavedState == NULL)
2472 {
2473 hr = device->BeginStateBlock();
2474 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2475
2476 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2477 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2478 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2479 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2480 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2481 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2482 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2483 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2484 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2485 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2486 device->SetPixelShader(NULL);
2487 device->SetVertexShader(NULL);
2488 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2489 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002490 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002491
2492 hr = device->EndStateBlock(&mMaskedClearSavedState);
2493 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2494 }
2495
2496 ASSERT(mMaskedClearSavedState != NULL);
2497
2498 if (mMaskedClearSavedState != NULL)
2499 {
2500 hr = mMaskedClearSavedState->Capture();
2501 ASSERT(SUCCEEDED(hr));
2502 }
2503
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2505 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2506 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2507 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2508 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2509 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2510 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2511 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2512
2513 if (flags & D3DCLEAR_TARGET)
2514 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002515 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2516 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2517 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2518 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002519 }
2520 else
2521 {
2522 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2523 }
2524
2525 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2526 {
2527 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2528 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2529 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2530 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002531 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002532 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002533 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2534 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002535 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002536 }
2537 else
2538 {
2539 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2540 }
2541
2542 device->SetPixelShader(NULL);
2543 device->SetVertexShader(NULL);
2544 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002545 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002546 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002547
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002548 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002549 {
2550 float x, y, z, w;
2551 D3DCOLOR diffuse;
2552 };
2553
2554 Vertex quad[4];
2555 quad[0].x = 0.0f;
2556 quad[0].y = (float)desc.Height;
2557 quad[0].z = 0.0f;
2558 quad[0].w = 1.0f;
2559 quad[0].diffuse = color;
2560
2561 quad[1].x = (float)desc.Width;
2562 quad[1].y = (float)desc.Height;
2563 quad[1].z = 0.0f;
2564 quad[1].w = 1.0f;
2565 quad[1].diffuse = color;
2566
2567 quad[2].x = 0.0f;
2568 quad[2].y = 0.0f;
2569 quad[2].z = 0.0f;
2570 quad[2].w = 1.0f;
2571 quad[2].diffuse = color;
2572
2573 quad[3].x = (float)desc.Width;
2574 quad[3].y = 0.0f;
2575 quad[3].z = 0.0f;
2576 quad[3].w = 1.0f;
2577 quad[3].diffuse = color;
2578
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002579 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002581
2582 if (flags & D3DCLEAR_ZBUFFER)
2583 {
2584 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2585 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2586 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2587 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002588
2589 if (mMaskedClearSavedState != NULL)
2590 {
2591 mMaskedClearSavedState->Apply();
2592 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002593 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002594 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595 {
2596 device->Clear(0, NULL, flags, color, depth, stencil);
2597 }
2598}
2599
2600void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2601{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002602 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603 {
2604 return error(GL_INVALID_OPERATION);
2605 }
2606
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002607 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608 IDirect3DDevice9 *device = getDevice();
2609 D3DPRIMITIVETYPE primitiveType;
2610 int primitiveCount;
2611
2612 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2613 return error(GL_INVALID_ENUM);
2614
2615 if (primitiveCount <= 0)
2616 {
2617 return;
2618 }
2619
2620 if (!applyRenderTarget(false))
2621 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002622 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002623 }
2624
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002625 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002626
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002627 TranslatedIndexData indexInfo;
2628 bool useIndexing;
2629 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002630 if (err != GL_NO_ERROR)
2631 {
2632 return error(err);
2633 }
2634
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635 applyShaders();
2636 applyTextures();
2637
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002638 if (!getCurrentProgram()->validateSamplers())
2639 {
2640 return error(GL_INVALID_OPERATION);
2641 }
2642
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002643 if (!cullSkipsDraw(mode))
2644 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002645 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002646 if (useIndexing)
2647 {
2648 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2649 }
2650 else
2651 {
2652 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2653 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002654 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655}
2656
2657void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2658{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002659 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002660 {
2661 return error(GL_INVALID_OPERATION);
2662 }
2663
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002664 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002665 {
2666 return error(GL_INVALID_OPERATION);
2667 }
2668
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002669 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002670 IDirect3DDevice9 *device = getDevice();
2671 D3DPRIMITIVETYPE primitiveType;
2672 int primitiveCount;
2673
2674 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2675 return error(GL_INVALID_ENUM);
2676
2677 if (primitiveCount <= 0)
2678 {
2679 return;
2680 }
2681
2682 if (!applyRenderTarget(false))
2683 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002684 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002685 }
2686
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002687 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002688
2689 TranslatedIndexData indexInfo;
2690 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2691 if (err != GL_NO_ERROR)
2692 {
2693 return error(err);
2694 }
2695
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002696 err = applyVertexBuffer(indexInfo);
2697 if (err != GL_NO_ERROR)
2698 {
2699 return error(err);
2700 }
2701
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702 applyShaders();
2703 applyTextures();
2704
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002705 if (!getCurrentProgram()->validateSamplers())
2706 {
2707 return error(GL_INVALID_OPERATION);
2708 }
2709
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002710 if (!cullSkipsDraw(mode))
2711 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002712 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002713 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 +00002714 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715}
2716
2717void Context::finish()
2718{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002719 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720 IDirect3DDevice9 *device = getDevice();
2721 IDirect3DQuery9 *occlusionQuery = NULL;
2722
2723 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2724
2725 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2726 {
2727 return error(GL_OUT_OF_MEMORY);
2728 }
2729
2730 ASSERT(SUCCEEDED(result));
2731
2732 if (occlusionQuery)
2733 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002734 IDirect3DStateBlock9 *savedState = NULL;
2735 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2736
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002737 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2738 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002739
2740 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002741 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742 device->SetPixelShader(NULL);
2743 device->SetVertexShader(NULL);
2744 device->SetFVF(D3DFVF_XYZRHW);
2745 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002746 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002747 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002749 result = occlusionQuery->Issue(D3DISSUE_END);
2750 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002751
2752 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2753 {
2754 // Keep polling, but allow other threads to do something useful first
2755 Sleep(0);
2756 }
2757
2758 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002759
2760 if (savedState)
2761 {
2762 savedState->Apply();
2763 savedState->Release();
2764 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002765 }
2766}
2767
2768void Context::flush()
2769{
2770 IDirect3DDevice9 *device = getDevice();
2771 IDirect3DQuery9 *eventQuery = NULL;
2772
2773 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2774
2775 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2776 {
2777 return error(GL_OUT_OF_MEMORY);
2778 }
2779
2780 ASSERT(SUCCEEDED(result));
2781
2782 if (eventQuery)
2783 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002784 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2785 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002787 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002789
2790 if (result == D3DERR_DEVICELOST)
2791 {
2792 error(GL_OUT_OF_MEMORY);
2793 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002794 }
2795}
2796
2797void Context::recordInvalidEnum()
2798{
2799 mInvalidEnum = true;
2800}
2801
2802void Context::recordInvalidValue()
2803{
2804 mInvalidValue = true;
2805}
2806
2807void Context::recordInvalidOperation()
2808{
2809 mInvalidOperation = true;
2810}
2811
2812void Context::recordOutOfMemory()
2813{
2814 mOutOfMemory = true;
2815}
2816
2817void Context::recordInvalidFramebufferOperation()
2818{
2819 mInvalidFramebufferOperation = true;
2820}
2821
2822// Get one of the recorded errors and clear its flag, if any.
2823// [OpenGL ES 2.0.24] section 2.5 page 13.
2824GLenum Context::getError()
2825{
2826 if (mInvalidEnum)
2827 {
2828 mInvalidEnum = false;
2829
2830 return GL_INVALID_ENUM;
2831 }
2832
2833 if (mInvalidValue)
2834 {
2835 mInvalidValue = false;
2836
2837 return GL_INVALID_VALUE;
2838 }
2839
2840 if (mInvalidOperation)
2841 {
2842 mInvalidOperation = false;
2843
2844 return GL_INVALID_OPERATION;
2845 }
2846
2847 if (mOutOfMemory)
2848 {
2849 mOutOfMemory = false;
2850
2851 return GL_OUT_OF_MEMORY;
2852 }
2853
2854 if (mInvalidFramebufferOperation)
2855 {
2856 mInvalidFramebufferOperation = false;
2857
2858 return GL_INVALID_FRAMEBUFFER_OPERATION;
2859 }
2860
2861 return GL_NO_ERROR;
2862}
2863
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002864bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002865{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002866 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002867}
2868
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002869int Context::getMaximumVaryingVectors() const
2870{
2871 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2872}
2873
daniel@transgaming.com458da142010-11-28 02:03:02 +00002874int Context::getMaximumFragmentUniformVectors() const
2875{
2876 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2877}
2878
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002879int Context::getMaxSupportedSamples() const
2880{
2881 return mMaxSupportedSamples;
2882}
2883
2884int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2885{
2886 if (requested == 0)
2887 {
2888 return requested;
2889 }
2890
2891 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2892 if (itr == mMultiSampleSupport.end())
2893 {
2894 return -1;
2895 }
2896
2897 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2898 {
2899 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2900 {
2901 return i;
2902 }
2903 }
2904
2905 return -1;
2906}
2907
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002908bool Context::supportsEventQueries() const
2909{
2910 return mSupportsEventQueries;
2911}
2912
daniel@transgaming.com01868132010-08-24 19:21:17 +00002913bool Context::supportsCompressedTextures() const
2914{
2915 return mSupportsCompressedTextures;
2916}
2917
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002918bool Context::supportsFloatTextures() const
2919{
2920 return mSupportsFloatTextures;
2921}
2922
2923bool Context::supportsFloatLinearFilter() const
2924{
2925 return mSupportsFloatLinearFilter;
2926}
2927
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002928bool Context::supportsFloatRenderableTextures() const
2929{
2930 return mSupportsFloatRenderableTextures;
2931}
2932
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002933bool Context::supportsHalfFloatTextures() const
2934{
2935 return mSupportsHalfFloatTextures;
2936}
2937
2938bool Context::supportsHalfFloatLinearFilter() const
2939{
2940 return mSupportsHalfFloatLinearFilter;
2941}
2942
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002943bool Context::supportsHalfFloatRenderableTextures() const
2944{
2945 return mSupportsHalfFloatRenderableTextures;
2946}
2947
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00002948int Context::getMaximumRenderbufferDimension() const
2949{
2950 return mMaxRenderbufferDimension;
2951}
2952
2953int Context::getMaximumTextureDimension() const
2954{
2955 return mMaxTextureDimension;
2956}
2957
2958int Context::getMaximumCubeTextureDimension() const
2959{
2960 return mMaxCubeTextureDimension;
2961}
2962
2963int Context::getMaximumTextureLevel() const
2964{
2965 return mMaxTextureLevel;
2966}
2967
daniel@transgaming.comed828e52010-10-15 17:57:30 +00002968bool Context::supportsLuminanceTextures() const
2969{
2970 return mSupportsLuminanceTextures;
2971}
2972
2973bool Context::supportsLuminanceAlphaTextures() const
2974{
2975 return mSupportsLuminanceAlphaTextures;
2976}
2977
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002978void Context::detachBuffer(GLuint buffer)
2979{
2980 // [OpenGL ES 2.0.24] section 2.9 page 22:
2981 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2982 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2983
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002984 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002985 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002986 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002987 }
2988
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002989 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002990 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002991 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002992 }
2993
2994 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2995 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002996 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002997 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002998 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002999 }
3000 }
3001}
3002
3003void Context::detachTexture(GLuint texture)
3004{
3005 // [OpenGL ES 2.0.24] section 3.8 page 84:
3006 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3007 // rebound to texture object zero
3008
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003009 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003010 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003011 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003012 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003013 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003014 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003015 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003016 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003017 }
3018 }
3019
3020 // [OpenGL ES 2.0.24] section 4.4 page 112:
3021 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3022 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3023 // image was attached in the currently bound framebuffer.
3024
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003025 Framebuffer *readFramebuffer = getReadFramebuffer();
3026 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003027
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003028 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003029 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003030 readFramebuffer->detachTexture(texture);
3031 }
3032
3033 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3034 {
3035 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003036 }
3037}
3038
3039void Context::detachFramebuffer(GLuint framebuffer)
3040{
3041 // [OpenGL ES 2.0.24] section 4.4 page 107:
3042 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3043 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3044
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003045 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003046 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003047 bindReadFramebuffer(0);
3048 }
3049
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003050 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003051 {
3052 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003053 }
3054}
3055
3056void Context::detachRenderbuffer(GLuint renderbuffer)
3057{
3058 // [OpenGL ES 2.0.24] section 4.4 page 109:
3059 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3060 // had been executed with the target RENDERBUFFER and name of zero.
3061
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003062 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003063 {
3064 bindRenderbuffer(0);
3065 }
3066
3067 // [OpenGL ES 2.0.24] section 4.4 page 111:
3068 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3069 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3070 // point to which this image was attached in the currently bound framebuffer.
3071
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003072 Framebuffer *readFramebuffer = getReadFramebuffer();
3073 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003074
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003075 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003076 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003077 readFramebuffer->detachRenderbuffer(renderbuffer);
3078 }
3079
3080 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3081 {
3082 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003083 }
3084}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003085
3086Texture *Context::getIncompleteTexture(SamplerType type)
3087{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003088 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003089
3090 if (t == NULL)
3091 {
3092 static const GLubyte color[] = { 0, 0, 0, 255 };
3093
3094 switch (type)
3095 {
3096 default:
3097 UNREACHABLE();
3098 // default falls through to SAMPLER_2D
3099
3100 case SAMPLER_2D:
3101 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003102 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003103 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003104 t = incomplete2d;
3105 }
3106 break;
3107
3108 case SAMPLER_CUBE:
3109 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003110 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003111
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003112 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3113 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3114 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3115 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3116 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3117 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003118
3119 t = incompleteCube;
3120 }
3121 break;
3122 }
3123
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003124 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003125 }
3126
3127 return t;
3128}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003129
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003130bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003131{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003132 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003133}
3134
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003135bool Context::isTriangleMode(GLenum drawMode)
3136{
3137 switch (drawMode)
3138 {
3139 case GL_TRIANGLES:
3140 case GL_TRIANGLE_FAN:
3141 case GL_TRIANGLE_STRIP:
3142 return true;
3143 case GL_POINTS:
3144 case GL_LINES:
3145 case GL_LINE_LOOP:
3146 case GL_LINE_STRIP:
3147 return false;
3148 default: UNREACHABLE();
3149 }
3150
3151 return false;
3152}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003153
3154void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3155{
3156 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3157
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003158 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3159 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3160 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3161 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003162
3163 mVertexDataManager->dirtyCurrentValues();
3164}
3165
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003166void Context::initExtensionString()
3167{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003168 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003169 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3170 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003171 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003172 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003173 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003174
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003175 if (supportsEventQueries())
3176 {
3177 mExtensionString += "GL_NV_fence ";
3178 }
3179
daniel@transgaming.com01868132010-08-24 19:21:17 +00003180 if (supportsCompressedTextures())
3181 {
3182 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3183 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003184
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003185 if (supportsFloatTextures())
3186 {
3187 mExtensionString += "GL_OES_texture_float ";
3188 }
3189
3190 if (supportsHalfFloatTextures())
3191 {
3192 mExtensionString += "GL_OES_texture_half_float ";
3193 }
3194
3195 if (supportsFloatLinearFilter())
3196 {
3197 mExtensionString += "GL_OES_texture_float_linear ";
3198 }
3199
3200 if (supportsHalfFloatLinearFilter())
3201 {
3202 mExtensionString += "GL_OES_texture_half_float_linear ";
3203 }
3204
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003205 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003206 {
3207 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3208 }
3209
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003210 if (mBufferBackEnd->supportIntIndices())
3211 {
3212 mExtensionString += "GL_OES_element_index_uint ";
3213 }
3214
3215 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3216 if (end != std::string::npos)
3217 {
3218 mExtensionString.resize(end+1);
3219 }
3220}
3221
3222const char *Context::getExtensionString() const
3223{
3224 return mExtensionString.c_str();
3225}
3226
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003227void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3228 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3229 GLbitfield mask)
3230{
3231 IDirect3DDevice9 *device = getDevice();
3232
3233 Framebuffer *readFramebuffer = getReadFramebuffer();
3234 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3235
3236 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3237 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3238 {
3239 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3240 }
3241
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003242 if (drawFramebuffer->getSamples() != 0)
3243 {
3244 return error(GL_INVALID_OPERATION);
3245 }
3246
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003247 RECT sourceRect;
3248 RECT destRect;
3249
3250 if (srcX0 < srcX1)
3251 {
3252 sourceRect.left = srcX0;
3253 sourceRect.right = srcX1;
3254 destRect.left = dstX0;
3255 destRect.right = dstX1;
3256 }
3257 else
3258 {
3259 sourceRect.left = srcX1;
3260 destRect.left = dstX1;
3261 sourceRect.right = srcX0;
3262 destRect.right = dstX0;
3263 }
3264
3265 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3266 // flip our Y-values here
3267 if (srcY0 < srcY1)
3268 {
3269 sourceRect.bottom = srcY1;
3270 destRect.bottom = dstY1;
3271 sourceRect.top = srcY0;
3272 destRect.top = dstY0;
3273 }
3274 else
3275 {
3276 sourceRect.bottom = srcY0;
3277 destRect.bottom = dstY0;
3278 sourceRect.top = srcY1;
3279 destRect.top = dstY1;
3280 }
3281
3282 RECT sourceScissoredRect = sourceRect;
3283 RECT destScissoredRect = destRect;
3284
3285 if (mState.scissorTest)
3286 {
3287 // Only write to parts of the destination framebuffer which pass the scissor test
3288 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3289 // rect will be checked against scissorY, rather than the bottom.
3290 if (destRect.left < mState.scissorX)
3291 {
3292 int xDiff = mState.scissorX - destRect.left;
3293 destScissoredRect.left = mState.scissorX;
3294 sourceScissoredRect.left += xDiff;
3295 }
3296
3297 if (destRect.right > mState.scissorX + mState.scissorWidth)
3298 {
3299 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3300 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3301 sourceScissoredRect.right -= xDiff;
3302 }
3303
3304 if (destRect.top < mState.scissorY)
3305 {
3306 int yDiff = mState.scissorY - destRect.top;
3307 destScissoredRect.top = mState.scissorY;
3308 sourceScissoredRect.top += yDiff;
3309 }
3310
3311 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3312 {
3313 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3314 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3315 sourceScissoredRect.bottom -= yDiff;
3316 }
3317 }
3318
3319 bool blitRenderTarget = false;
3320 bool blitDepthStencil = false;
3321
3322 RECT sourceTrimmedRect = sourceScissoredRect;
3323 RECT destTrimmedRect = destScissoredRect;
3324
3325 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3326 // the actual draw and read surfaces.
3327 if (sourceTrimmedRect.left < 0)
3328 {
3329 int xDiff = 0 - sourceTrimmedRect.left;
3330 sourceTrimmedRect.left = 0;
3331 destTrimmedRect.left += xDiff;
3332 }
3333
3334 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3335 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3336 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3337 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3338
3339 if (sourceTrimmedRect.right > readBufferWidth)
3340 {
3341 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3342 sourceTrimmedRect.right = readBufferWidth;
3343 destTrimmedRect.right -= xDiff;
3344 }
3345
3346 if (sourceTrimmedRect.top < 0)
3347 {
3348 int yDiff = 0 - sourceTrimmedRect.top;
3349 sourceTrimmedRect.top = 0;
3350 destTrimmedRect.top += yDiff;
3351 }
3352
3353 if (sourceTrimmedRect.bottom > readBufferHeight)
3354 {
3355 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3356 sourceTrimmedRect.bottom = readBufferHeight;
3357 destTrimmedRect.bottom -= yDiff;
3358 }
3359
3360 if (destTrimmedRect.left < 0)
3361 {
3362 int xDiff = 0 - destTrimmedRect.left;
3363 destTrimmedRect.left = 0;
3364 sourceTrimmedRect.left += xDiff;
3365 }
3366
3367 if (destTrimmedRect.right > drawBufferWidth)
3368 {
3369 int xDiff = destTrimmedRect.right - drawBufferWidth;
3370 destTrimmedRect.right = drawBufferWidth;
3371 sourceTrimmedRect.right -= xDiff;
3372 }
3373
3374 if (destTrimmedRect.top < 0)
3375 {
3376 int yDiff = 0 - destTrimmedRect.top;
3377 destTrimmedRect.top = 0;
3378 sourceTrimmedRect.top += yDiff;
3379 }
3380
3381 if (destTrimmedRect.bottom > drawBufferHeight)
3382 {
3383 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3384 destTrimmedRect.bottom = drawBufferHeight;
3385 sourceTrimmedRect.bottom -= yDiff;
3386 }
3387
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003388 bool partialBufferCopy = false;
3389 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3390 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3391 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3392 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3393 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3394 {
3395 partialBufferCopy = true;
3396 }
3397
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003398 if (mask & GL_COLOR_BUFFER_BIT)
3399 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003400 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3401 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3402 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3403 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3404 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003405 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3406 {
3407 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3408 return error(GL_INVALID_OPERATION);
3409 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003410
3411 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3412 {
3413 return error(GL_INVALID_OPERATION);
3414 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003415
3416 blitRenderTarget = true;
3417
3418 }
3419
3420 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3421 {
3422 DepthStencilbuffer *readDSBuffer = NULL;
3423 DepthStencilbuffer *drawDSBuffer = NULL;
3424
3425 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3426 // both a depth and stencil buffer, it will be the same buffer.
3427
3428 if (mask & GL_DEPTH_BUFFER_BIT)
3429 {
3430 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3431 {
3432 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3433 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3434 {
3435 return error(GL_INVALID_OPERATION);
3436 }
3437
3438 blitDepthStencil = true;
3439 readDSBuffer = readFramebuffer->getDepthbuffer();
3440 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3441 }
3442 }
3443
3444 if (mask & GL_STENCIL_BUFFER_BIT)
3445 {
3446 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3447 {
3448 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3449 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3450 {
3451 return error(GL_INVALID_OPERATION);
3452 }
3453
3454 blitDepthStencil = true;
3455 readDSBuffer = readFramebuffer->getStencilbuffer();
3456 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3457 }
3458 }
3459
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003460 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003461 {
3462 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3463 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3464 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003465
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003466 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3467 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003468 {
3469 return error(GL_INVALID_OPERATION);
3470 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003471 }
3472
3473 if (blitRenderTarget || blitDepthStencil)
3474 {
3475 egl::Display *display = getDisplay();
3476 display->endScene();
3477
3478 if (blitRenderTarget)
3479 {
3480 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3481 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3482
3483 if (FAILED(result))
3484 {
3485 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3486 return;
3487 }
3488 }
3489
3490 if (blitDepthStencil)
3491 {
3492 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3493
3494 if (FAILED(result))
3495 {
3496 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3497 return;
3498 }
3499 }
3500 }
3501}
3502
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003503}
3504
3505extern "C"
3506{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003507gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003508{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003509 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003510}
3511
3512void glDestroyContext(gl::Context *context)
3513{
3514 delete context;
3515
3516 if (context == gl::getContext())
3517 {
3518 gl::makeCurrent(NULL, NULL, NULL);
3519 }
3520}
3521
3522void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3523{
3524 gl::makeCurrent(context, display, surface);
3525}
3526
3527gl::Context *glGetCurrentContext()
3528{
3529 return gl::getContext();
3530}
3531}